首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

小弟初学,看大家修改修改编程,链表的

2012-11-09 
小弟初学,望大家修改修改编程,链表的设C{a1,b1,a2,b2,…,an,bn}为一线性表,采用带头结点的hc单链表存放,设

小弟初学,望大家修改修改编程,链表的
设C={a1,b1,a2,b2,…,an,bn}为一线性表,采用带头结点的hc单链表存放,设计一个就地算法,将其拆分为两个线性表A={a1,a2,…,an}, B={b1,b2,…,bn},拆分后的线性表分别用带头结点的单链表存放。
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
void creat_LinkList(LinkList L,int n){
  LNode *P;
  printf("Please input the list:\n");
int i;
  for(i=0;i<n;i++){
P=(LNode *)malloc(sizeof(LNode));
  scanf("%d",&(P->data));
  P->next=L->next;
  L->next=P;
}
}
void Depart_LinkList(LinkList L,int n){
LinkList La,Lb;
  La=(LinkList)malloc(sizeof(ElemType));
La->data=NULL;
LNode *q;
  Lb=(LinkList)malloc(sizeof(ElemType));
Lb->data=NULL;
  LNode *P;
int i;
for(i=1;i<=n&&L->data;i++){
if(i%2){
q=(LNode *)malloc(sizeof(LNode));  
  P->next=La->next;
  La->next=P;La->data=L->data; }
else {
P=(LNode *)malloc(sizeof(LNode));
  P->next=Lb->next;
  Lb->next=P;Lb->data=L->data; }
L=L->next;
}
printf("Now the list is departed into two list:\nLa= ");
while(La->data){
printf("%d\t",La->data);
La=La->next; }
  printf("\nLb=: ")
while(Lb->data){
printf("%d\t",Lb->data);
Lb=Lb->next; }
}
void main()
{
LinkList L;
int n;
printf("Please input the length of the list:\n");
scanf("%d",&n);
L=(LinkList)malloc(sizeof(ElemType));
L->data=NULL;
creat_LinkList(L,n);
Depart_LinkList(L,n);
}

[解决办法]

C/C++ code
1 #include <stdio.h>  2 #include <stdlib.h>  3 #include <string.h>  4 #include <errno.h>  5   6 typedef int Elemtype;  7 typedef struct node  8 {  9     Elemtype data; 10     struct node *next; 11 }Node, *LinkNode; 12  13 void create_nodes(LinkNode *h, int n); 14 void depart_link(LinkNode *La, LinkNode *Lb, LinkNode h); 15 void show_link(LinkNode p); 16  17 int main(int argc, char *argv[]) 18 { 19     int num = 0; 20     LinkNode head = NULL, La, Lb; 21     printf("Please input the length of link: "); 22     scanf("%d", &num); 23     printf("Create the link: \n"); 24     create_nodes(&head, num); 25     show_link(head); 26     La = (LinkNode)malloc(sizeof(Node)); 27     La->data = -1; 28     La->next = NULL; 29     Lb = (LinkNode)malloc(sizeof(Node)); 30     Lb->data = -1; 31     Lb->next = NULL; 32     depart_link(&La, &Lb, head); 33     printf("Show La list: "); 34     show_link(La); 35     printf("Show Lb list: "); 36     show_link(Lb); 37  38     return 0; 39 } 40  41 void create_nodes(LinkNode *h, int n) 42 { 43     int input = 0; 44     int i; 45     LinkNode p = *h, q; 46  47     for (i = 0; i < n; i++) 48     { 49         printf("Please input NO.%d:\n", i+1); 50         scanf("%d", &input); 51         if (*h == NULL) 52         { 53             *h = (LinkNode)malloc(sizeof(Node)); 54             (*h)->data = input; 55             (*h)->next = NULL; 56             p = *h; 57         } 58         else 59         { 60             q = (LinkNode)malloc(sizeof(Node)); 61             q->data = input; 62             q->next = NULL; 63             p->next = q; 64             p = p->next; 65         } 66     } 67 } 68  69 void depart_link(LinkNode *La, LinkNode *Lb, LinkNode h) 70 { 71     int i; 72     LinkNode p = h, q = NULL; 73     LinkNode s = *La, t = *Lb; 74  75     for (i = 1; p!= NULL; i++, p = p->next) 76     { 77         if (i % 2) 78         { 79             if (s->data == -1) 80             { 81                 s->data = p->data; 82             } 83             else 84             { 85                 q = (LinkNode)malloc(sizeof (Node)); 86                 q->data = p->data; 87                 q->next = NULL; 88                 s->next = q; 89                 s = s->next; 90             } 91         } 92         else 93         { 94             if (t->data == -1) 95             { 96                 t->data = p->data; 97             } 98             else 99             {100                 q = (LinkNode)malloc(sizeof (Node));101                 q->data = p->data;102                 q->next = NULL;103                 t->next = q;104                 t = t->next;105             }106         }107     }108 }109 110 void show_link(LinkNode p)111 {112     while (p != NULL)113     {114         fprintf(stdout, " %d\t", p->data);115         p = p->next;116     }117     putchar('\n');118 } 

热点排行