线性表(逆置)

链式存储方式

 

#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

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

 

转载于:https://www.cnblogs.com/lanshy/archive/2013/03/24/2978314.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值