改造双向循环链表
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));
25 s->string=c;
26 p->next=s;
27 p=s;
28 }
29 else
30 {
31 flag=0;
32 s->next=NULL;
33 }
34 }
35 return head;
36 }
37
38 void splitLinkList(LinkList &Union ,LinkList &A,LinkList &B,LinkList &C)
39 {
40 LinkList pa,pb,pc,pu;
41 LNode *s;
42 A=(LinkList)malloc(sizeof(LNode));
43 B=(LinkList)malloc(sizeof(LNode));
44 C=(LinkList)malloc(sizeof(LNode));
45 A->next=NULL;
46 B->next=NULL;
47 C->next=NULL;
48 pa=A; //a-z A-Z 链
49 pb=B; //数字 链
50 pc=C; //其他字符 链
51 pu=Union->next;
52 while(pu!=NULL)
53 {
54 char string = pu->string;
55 s=(LinkList)malloc(sizeof(LNode));
56 s->string=string;
57 if((string>='a' && string <='z') ||(string>='A' && string <='Z'))
58 {
59 pa->next=s;
60 pa=s;
61 }
62 else if(string >='0' && string <='9')
63 {
64 pb->next=s;
65 pb=s;
66 }
67 else
68 {
69 pc->next=s;
70 pc=s;
71 }
72 pu=pu->next;
73
74 }
75 pa->next=A;
76 pb->next=B;
77 pc->next=C;
78
79 }
80 void println(LinkList L)
81 {
82 LinkList p;
83 p=L->next;
84 while(p!=NULL)
85 {
86 printf("%c",p->string);
87 p=p->next;
88 }
89 printf("\n");
90 }
91 void printlnLoopLink(LinkList L)
92 {
93 LinkList p;
94 p=L->next;
95 while(p!=L)
96 {
97 printf("%c",p->string);
98 p=p->next;
99 }
100 printf("\n");
101 }
102
103
104 void main()
105 {
106 LinkList Union,A,B,C;
107 printf("输入Union链表, # 结束:");
108 Union=initCreateLink();
109 printf("Union链表:");
110 println(Union);
111 splitLinkList(Union,A,B,C);
112 printf("A链表:");
113 printlnLoopLink(A);
114 printf("B链表:");
115 printlnLoopLink(B);
116 printf("C链表:");
117 printlnLoopLink(C);
118 }
判别哪个单词在字典中排在前面
View Code
1 #include <stdio.h>
2 #include <string.h>
3 typedef struct Word
4 {
5 char * string ;
6 int length;
7 }Word;
8 void main()
9 {
10 Word stringA;
11 Word stringB;
12 stringA.string="switch";
13 stringA.length=strlen(stringA.string);
14 stringB.string="swich";
15 stringB.length=strlen(stringB.string);
16
17 int index=0;
18 int status=0; //相等
19 while (index<stringA.length && index<stringB.length)
20 {
21 if(stringA.string[index]>stringB.string[index])
22 {
23 status=1; //stringB在前面
24 break;
25 }
26 else if(stringA.string[index]<stringB.string[index])
27 {
28 status=-1; //stringA 在前面
29 break;
30 }
31 else
32 {
33 index++;
34 if(stringA.length>stringB.length) status=1 ;
35 if(stringA.length<stringB.length) status=-1;
36 }
37 }
38
39 switch (status)
40 {
41 case 0:
42 {
43 printf("%s\n",stringA.string);
44 }break;
45 case 1:
46 {
47 printf("%s\n",stringB.string);
48 }break;
49 case -1:
50 {
51 printf("%s\n",stringA.string);
52 }break;
53 default:
54 {
55
56 }
57 }
58 }
求链表A和链表B的交集C
View Code
1 #include <stdio.h>
2 #include <stdlib.h>
3
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 *s,*p;
15 int flag=1;
16 ElemType data;
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 return head;
37 }
38
39 void println(LinkList L)
40 {
41 LinkList p;
42 p=L->next;
43 while(p)
44 {
45 printf("%4d",p->data);
46 p=p->next;
47 }
48 printf("\n");
49 }
50 struct LNode* UnionL(LinkList A,LinkList B)
51 {
52 LinkList head;
53 LinkList pa,pb,ph;
54 LNode *s;
55 head=(LinkList)malloc(sizeof(LNode));
56 head->next=NULL;
57 ph=head;
58 pa=A->next;
59 pb=B->next;
60 while(pa&&pb)
61 {
62 s=(LinkList)malloc(sizeof(LNode));
63 if(pa->data <pb->data)
64 {
65
66 s->data=pa->data;
67 ph->next=s;
68 ph=s;
69 pa=pa->next;
70 }
71 else
72 {
73 if(pa->data==pb->data)
74 {
75 pa=pa->next;
76 }
77 s->data=pb->data;
78 ph->next=s;
79 ph=s;
80 pb=pb->next;
81 }
82 }
83 while(pa)
84 {
85 s=(LinkList)malloc(sizeof(LNode));
86 s->data=pa->data;
87 ph->next=s;
88 ph=s;
89 pa=pa->next;
90 }
91 while(pb)
92 {
93 s=(LinkList)malloc(sizeof(LNode));
94 s->data=pb->data;
95 ph->next=s;
96 ph=s;
97 pb=pb->next;
98 }
99 ph->next=NULL;
100 return head ;
101 }
102 void main()
103 {
104 LinkList A,B,C;
105 char cancelEnter;
106 printf("输入链表A:");
107 A=initCreateLink();
108 cancelEnter=getchar();
109 printf("输入链表B:");
110 B=initCreateLink();
111 printf("输出链表A:");
112 println(A);
113 printf("输出链表B:");
114 println(B);
115 C=UnionL(A,B);
116 printf("输出A,B链表的交集为C链表:");
117 println(C);
118 }
删除表中在某个范围的元素
View Code
1 #include <stdio.h>
2 #include <stdlib.h>
3 typedef int ElemType;
4 typedef struct LNode
5 {
6 ElemType data;
7 struct LNode * next;
8 }LNode ,*LinkList;
9
10 struct LNode *initCreateLinkList()
11 {
12 LinkList head;
13 LNode *s,*p;
14 int data;
15 int flag =1;
16 head=(LinkList)malloc(sizeof(LNode));
17 head->next=NULL;
18 p=head;
19
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 flag=0;
33 p->next=NULL;
34 }
35 }
36 return head;
37 }
38 void delLinkList(LinkList L,int mink,int maxk)
39 {
40 LNode *p,*q;
41 int data;
42 p=L;
43 q=p;
44 p=p->next;
45 while(p!=NULL)
46 {
47 data=p->data;
48 if(data> mink && data<maxk)
49 {
50 q->next=p->next;
51 free(p);
52 p=q->next;
53 }
54 else
55 {
56 q=p;
57 p=p->next;
58 }
59 }
60
61 }
62 void println(LinkList L)
63 {
64 LinkList pl;
65 pl=L->next ;
66 if(pl==NULL)
67 {
68 printf("链表为空");
69 }
70 while(pl!=NULL)
71 {
72 printf("%4d",pl->data);
73 pl=pl->next;
74 }
75 printf("\n");
76 }
77
78 void main()
79 {
80 LinkList L;
81 int mink,maxk;
82 char cancelEnter;
83 printf("创建一个递增有序排列的单链表L:");
84 L=initCreateLinkList();
85 printf("输出单链表:");
86 println(L);
87 printf("删除表中所有值大于mink且小于maxk的元素\n");
88 printf("输入mink的值:");
89 scanf("%d",&mink);
90 scanf("%c",&cancelEnter);
91 printf("输入maxk的值:");
92 scanf("%d",&maxk);
93
94 delLinkList(L,mink,maxk);
95 printf("删除结点后的单链表L:");
96 println(L);
97 }
实现顺序表的就地逆置
View Code
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 const OK=1;
5 const Error=-1;
6 typedef int status;
7 typedef char ElemType;
8 typedef struct SqList
9 {
10 ElemType *array;
11 int length;
12 }SqList,*PSqList;
13 status InitSqList(SqList &L)
14 {
15 L.array=(char *)malloc(100);
16 printf("输入要逆置的数组:");
17 scanf("%s",L.array);
18 L.length=strlen(L.array);
19 // printf("%s,%d",L.array,L.length);
20 return OK;
21 }
22 void inverseArray(SqList &L)
23 {
24 for(int i=0,j=L.length-1;i<j;i++,j--)
25 {
26 char temp=L.array[i];
27 L.array[i]=L.array[j];
28 L.array[j]=temp;
29 }
30 }
31 void main()
32 {
33 SqList L;
34 InitSqList(L);
35 printf("逆置前:");
36 printf("%s\n",L.array);
37 printf("逆置后:");
38 inverseArray(L);
39 printf("%s\n",L.array);
40 }
有错的欢迎各位指正,这几个都是一般的做法,若有更好的算法可以发上来学习下,不胜感激!谢谢