第一天:线性链表,请大家多多指教哦

题目:已知线性链表第一个结点指针为list,请写一算法,将该链表分解为两个带有头结点的循环链表,并将两个循环链表的长度分别存放在各自头结点的数据域中。其中,线性表中序号为奇数的元素分解到第一个循环链表中,序号为偶数的元素分解到第二个循环链表中。

思路分析:看到题目,我想到这是对线性表的基本操作,也就是线性表的建立。仔细思考对线性表的分解其实就是将线性表做一下简单的拆分,即建立两个小线性链表,这时,我们需要考虑如何拆分,关键是看题目中给的要求(按序号拆分)。接下来就是根据条件,建立两个线性链表,为了节省时间和空间,我们无需开辟新空间,只需在原来的链表上进行操作。以下便是我个人写的代码来和大家分享,请多指教。

   1: #include "stdio.h"
   2: #include "stdlib.h"
   3:  
   4: typedef struct Node 
   5: {
   6:     int data;
   7:     struct Node *next;
   8: }LNode,*LinkList;
   9:  
  10: //链表输出
  11: void OutPutList(LinkList head)
  12: {
  13:     LNode *temp;
  14:     int count=head->data;
  15:     temp=head->next;
  16:     printf("该链表中的数据有;\n");
  17:     while(count)
  18:     {
  19:         printf("%d ",temp->data);
  20:         temp=temp->next;
  21:         count--;
  22:     }
  23:     printf("\n");
  24: }
  25:  
  26: //建立链表
  27: LinkList CreateList()
  28: {
  29:     LinkList list;
  30:     LNode *tail,*temp;
  31:     int number;
  32:     list=(LNode *)malloc(sizeof(LNode));
  33:     list->data=0;
  34:     list->next=NULL;
  35:     tail=list;
  36:     printf("从1开始依次输入结点序号,以0为结束标志:\n");
  37:     scanf("%d",&number);
  38:     while(number)
  39:     {
  40:         temp=(LNode *)malloc(sizeof(LNode));
  41:         temp->data=number;
  42:         if (list->next==NULL)
  43:         {
  44:             temp->next=list->next;
  45:             list->next=temp;
  46:         }
  47:         else
  48:         {
  49:             temp->next=tail->next;
  50:             tail->next=temp;
  51:         }
  52:         tail=temp;
  53:         list->data++;
  54:         scanf("%d",&number);
  55:     }
  56:     OutPutList(list);
  57:     return list;
  58: }
  59:  
  60: //分解链表
  61: void SeperateList(LinkList list,LinkList list1,LinkList list2)
  62: {
  63:     list1->data=0;
  64:     list1->next=NULL;
  65:     list2->data=0;
  66:     list2->next=NULL;
  67:     LNode *rep,*temp,*tail1,*tail2;
  68:     rep=temp=list->next;
  69:     tail1=list1;
  70:     tail2=list2;
  71:     while (temp)
  72:     {
  73:         if (temp->data%2)
  74:         {
  75:             rep=temp->next;
  76:             if (list1->next==NULL)
  77:             {
  78:                 temp->next=list1->next;
  79:                 list1->next=temp;
  80:             }
  81:             else
  82:             {
  83:                 temp->next=tail1->next;
  84:                 tail1->next=temp;
  85:             }
  86:             tail1=temp;
  87:             list1->data++;
  88:         }
  89:         else
  90:         {
  91:             rep=temp->next;
  92:             if (list2->next==NULL)
  93:             {
  94:                 temp->next=list2->next;
  95:                 list2->next=temp;
  96:             }
  97:             else
  98:             {
  99:                 temp->next=tail2->next;
 100:                 tail2->next=temp;
 101:             }
 102:             tail2=temp;
 103:             list2->data++;
 104:         }
 105:         temp=rep;
 106:     }
 107:     tail1->next=list1;
 108:     tail2->next=list2;
 109:     free(list);
 110: }
 111:  
 112:  
 113:  
 114: int main()
 115: {
 116:     LinkList list,list1,list2;
 117:     list=CreateList();
 118:     list1=(LNode *)malloc(sizeof(LNode));
 119:     list2=(LNode *)malloc(sizeof(LNode));
 120:     SeperateList(list,list1,list2);
 121:     printf("链表1:\n元素个数为%d\n",list1->data);
 122:     OutPutList(list1);
 123:     printf("链表2:\n元素个数为%d\n",list2->data);
 124:     OutPutList(list2);
 125:     return 0;
 126: }

转载于:https://www.cnblogs.com/niuniulili/archive/2011/08/24/2152231.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值