现在的位置: 首页 > 综合 > 正文

第二章—线性表

2013年05月26日 ⁄ 综合 ⁄ 共 4400字 ⁄ 字号 评论关闭

改造双向循环链表

View Code

1 #include <stdio.h>
2 #include <stdlib.h>
3 typedef char ElemType;
4 typedef struct DuLNode
5 {
6 ElemType data;
7 struct DuLNode *front;
8 struct DuLNode *rear;
9 }DuLNode ,*DuLinkList;
10
11  struct DuLNode * initCreateDuLinkList()
12 {
13 DuLinkList head;
14 head=(DuLinkList)malloc(sizeof(DuLNode));
15 DuLNode *p,*s;
16 head->front=head;
17 head->rear=head;
18 p=head;
19 int flag=1;
20 char data;
21 while (flag)
22 {
23 data=getchar();
24 if(data!='#')
25 {
26 s=(DuLinkList)malloc(sizeof(DuLNode));
27 s->data=data;
28 s->front=p;
29 p->rear=s;
30 p=s;
31 }
32 else
33 {
34 flag=0;
35 s->rear=head;
36 head->front=s;
37 }
38 }
39 return head;
40 }
41  void println(DuLinkList L)
42 {
43 DuLinkList p;
44 p=L->rear;
45 while(p!=L)
46 {
47 printf("%c",p->data);
48 p=p->rear;
49 }
50 printf("\n");
51 }
52  void reverseprintln(DuLinkList L)
53 {
54 DuLinkList p;
55 p=L->front;
56 while(p!=L)
57 {
58 printf("%c",p->data);
59 p=p->front;
60 }
61 printf("\n");
62 }
63  void changeDuLinkList(DuLinkList &L)
64 {
65 DuLinkList f,r,last;
66 DuLNode *s;
67 r=L->front; //最后一个结点
68   f=L->rear; // 第一个结点
69   last=r;
70 int flag=0;
71 while(f!=r)
72 {
73 flag++;
74 if(flag%2==0)
75 {
76 s=f;
77 f=f->rear;
78
79 s->front->rear=f;
80 f->front=s->front;
81
82 s->rear=r->rear;
83 r->rear->front=s;
84 r->rear=s;
85 s->front=r;
86 }
87 else
88 {
89 f=f->rear;
90 }
91
92
93 }
94
95 }
96  void main()
97 {
98 DuLinkList L;
99 L=initCreateDuLinkList();
100 println(L);
101 reverseprintln(L);
102 changeDuLinkList(L);
103 println(L);
104 }

合并A链表和B链表为C链表

View Code

1 #include <stdio.h>
2 #include <malloc.h>
3 typedef char ElemType;
4 typedef struct LNode
5 {
6 ElemType data;
7 struct LNode *next;
8 }LNode,*LinkList;
9
10  struct LNode *initCreateList()
11 {
12 LinkList head;
13 LNode *p,*s;
14 head=(LinkList)malloc(sizeof(LNode));
15 head->next=NULL;
16 p=head;
17 char c;
18 int flag=1;
19 while(flag)
20 {
21 scanf("%c",&c);
22 if(c!='#')
23 {
24 s=(LNode *)malloc(sizeof(LNode));
25 s->data=c;
26 p->next=s;
27 p=s;
28 }
29 else
30 {
31 flag=0;
32 p->next=NULL;
33 }
34
35 }
36 return head;
37 }
38  void println(LinkList L)
39 {
40 LinkList pl;
41 pl=L->next;
42 while(pl!=NULL)
43 {
44 printf("%c",pl->data);
45 pl=pl->next;
46
47 }
48 printf("\n");
49 }
50  void main()
51 {
52 LinkList C;
53 LinkList A;
54 LinkList B;
55 LinkList pa,pb,pc;
56 char aaa;
57 printf("初始化建立链表A:");
58 A=initCreateList();
59 scanf("%c",&aaa); //用来取消回车键的
60   printf("初始化建立链表B:");
61 B=initCreateList();
62 printf("A链表:");
63 println(A);
64 printf("B链表:");
65 println(B);
66 C=A;
67 pa=A->next;pb=B->next;pc=C;
68 while (pa!=NULL || pb!=NULL)
69 {
70 if(pa!=NULL)
71 {
72 pc->next=pa;
73 pc=pa;
74 pa=pa->next;
75 }
76 else
77 {
78 pc->next=pb;
79 break;
80 }
81 if(pb!=NULL)
82 {
83 pc->next=pb;
84 pc=pb;
85 pb=pb->next;
86 }
87 else
88 {
89 pc->next=pa;
90 break;
91 }
92 }
93 free(B);
94 printf("C链表:");
95 println(C);
96
97 }

将A链表B链表归并为C链表

View Code

1 #include <stdio.h>
2
3 #include <stdlib.h>
4 typedef int ElemType;
5 typedef struct LNode
6 {
7 ElemType data;
8 struct LNode *next;
9 }LNode ,*LinkList;
10
11  struct LNode *initCreateLink()
12 {
13 LinkList head;
14 LNode *p,*s;
15 int data;
16 int flag=1;
17 head=(LinkList)malloc(sizeof(LNode));
18 head->next=NULL;
19 p=head;
20 while(flag)
21 {
22 scanf("%d",&data);
23 if(data!=-1)
24 {
25 s=(LinkList)malloc(sizeof(LNode));
26 s->data=data;
27 p->next=s;
28 p=s;
29 }
30 else
31 {
32 p->next=NULL;
33 flag=0;
34 }
35
36 }
37 return head;
38 }
39
40  void println(LinkList L)
41 {
42 LinkList p;
43 p=L->next;
44 if(p==NULL) { printf("空链表\n"); return; }
45 while(p!=NULL)
46 {
47 printf("%4d",p->data);
48 p=p->next;
49 }
50 printf("\n");
51 }
52  struct LNode* UnionL(LinkList A,LinkList B)
53 {
54 LinkList head;
55 LinkList pa,pb,ph,ps;
56 pa=A->next;
57 pb=B->next;
58 head=(LinkList)malloc(sizeof(LNode));
59 head->next=NULL;
60 ph=head;
61 if(pa==NULL) {head=B;return head;}
62 if(pb==NULL) {head=A;return head;}
63 while(pa&&pb)
64 {
65 if( pa->data < pb->data)
66 {
67 ps=pa;
68 pa=pa->next;
69 ps->next=ph->next;
70 ph->next=ps;
71
72 }
73 else
74 {
75 ps=pb;
76 pb=pb->next;
77 ps->next=ph->next;
78 ph->next=ps;
79 }
80 }
81 while (pa)
82 {
83 ps=pa;
84 pa=pa->next;
85 ps->next=ph->next;
86 ph->next=ps;
87 }
88 while(pb)
89 {
90 ps=pb;
91 pb=pb->next;
92 ps->next=ph->next;
93 ph->next=ps;
94 }
95 free(A);
96 free(B);
97 return head;
98 }
99  void main()
100 {
101 LinkList A,B,C;
102 char cancelEnter;
103 printf("输入A链表:");
104 A=initCreateLink();
105 printf("输入B链表:");
106 scanf("%c",&cancelEnter);
107 B=initCreateLink();
108 printf("输出A链表:");
109 println(A);
110 printf("输出B链表:");
111 println(B);
112 C=UnionL(A,B);
113 printf("输出C链表:");
114 println(C);
115 }

将一个含3类字符的数据元素分割为3个循环链表

View Code

1 #include <stdio.h>
2 #include <stdlib.h>
3 typedef char ElemType;
4 typedef struct LNode
5 {
6 ElemType string;
7 struct LNode *next;
8 }LNode,*LinkList;
9
10  struct LNode * initCreateLink()
11 {
12 LinkList head;
13 LNode *p,*s;
14 head=(LinkList)malloc(sizeof(LNode));
15 head->next=NULL;
16 p=head;
17 int flag=1;
18 char c;
19 while(flag)
20 {
21 c=getchar();
22 if(c!='#')
23 {
24 s=(LinkList)malloc(sizeof(LNode));

抱歉!评论已关闭.