线性表(逆置)

链式存储方式

 

#include <stdio.h>
#include<stdlib.h>
#include <malloc.h>
struct node            //定义结构体
{
    int data;
    struct node * next;
};
//逆置链表 
struct node *revercelink(struct node *head)
{    
    struct node *p, *q, *r;
    r=p=head;
    q=p->next;
    p->next=NULL;
    while(q!=NULL)
    {
        p=q;
        q=q->next;
        p->next=r;
        r=p;
    }
    return p;
}
//创建链表 
struct node *createlink()
{   
    struct node *p, *q;
    int i=0, a;
    printf("请输入表(以0结束):\n");
       scanf("%d", &a);                    //接收输入的数字 
    p=(struct node *)malloc(sizeof(struct node));     //开辟链表空间 
       if(a==0)
       {
        p=(struct node *)malloc(sizeof(struct node));     //开辟链表空间 
           p->data=a;
           p->next=NULL;
      }
    while(a!=0)
    {
        p=(struct node *)malloc(sizeof(struct node));     //开辟链表空间 
        if(i==0)
        {    p->next=NULL;    }
            else
            {    p->next=q;    }
        p->data=a;        //向链表存数据 
        q=p;
        i++;
        scanf("%d", &a);        //接收输入的数字 
    }
    p=revercelink(p);
    return p;
}
//输出链表 
struct node *output(struct node *head)
{    
    struct node *p;
    p=head;
    while(p!=NULL)
    {
        printf("%3d", p->data);
        p=p->next;
    }
    printf("\n"); 
    return head;
}
//主函数 
int main()
{    struct node *head, *p;
    head=createlink();
    printf("原链表为:\n"); 
    head=output(head);
    head=revercelink(head); 
    printf("逆置后的表为:\n"); 
    head=output(head);
    return 0;        
}
/*输出示例:
请输入表(以0结束):
1 2 3 4 5 6 7 8 9 0
原链表为:
  1  2  3  4  5  6  7  8  9
逆置后的表为:
  9  8  7  6  5  4  3  2  1
请按任意键继续. . . 
*/

 

线性表顺序存储:

 

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<math.h>
typedef char ElemType;
typedef struct 
{
    ElemType a[100];//定义结构体 
    int last;
}list;
//创建线性表 
void creat(list *L)

    int n,i;
    ElemType x;
    printf("请输入数据元素个数(在 0到 100之间):");
    scanf("%d",&n);
    
    for(i=0;i<n;i++)
    {
        printf("a[%d]=",i);
        fflush(stdin);//清除键盘缓冲区 
        scanf("%c",&x);//接收输入的字符 
        L->a[i]=x;
    }
    L->last=n;
    printf("\n");
}
//逆置线性表  
list *reverse(list *L)
{
    ElemType t;
    int i;
    for(i=0;i<L->last/2;i++)//前一半与后一半交换 
    {
        t=L->a[i];
        L->a[i]=L->a[L->last-i-1];
        L->a[L->last-i-1]=t;
    }
    return L;    
}
//输出线性表 
void out(list *L)
{
    int i;
    for(i=0;i<L->last;i++)
    {
        printf("a[%d]=",i);
        printf("%c\n",L->a[i]);
    }
    printf("\n");
}
//主函数 
int main()
{
    list  *L;
    int i;
    L=(list *)malloc(sizeof(list));//开辟空间
    creat( L);
    printf("线性表的原顺序为:\n");
    out(L);
    printf("逆置后的顺序为:\n");
    reverse(L);
    out(L);
    return 0;
}
/*输出示例 
请输入数据元素个数(在 0到 100之间):9
a[0]=a
a[1]=b
a[2]=c
a[3]=d
a[4]=e
a[5]=f
a[6]=g
a[7]=h
a[8]=i

线性表的原顺序为:
a[0]=a
a[1]=b
a[2]=c
a[3]=d
a[4]=e
a[5]=f
a[6]=g
a[7]=h
a[8]=i

逆置后的顺序为:
a[0]=i
a[1]=h
a[2]=g
a[3]=f
a[4]=e
a[5]=d
a[6]=c
a[7]=b
a[8]=a

请按任意键继续. . .
*/ 

 

posted @ 2013-03-24 11:38  蓝色L火焰  阅读(401)  评论(0编辑  收藏  举报