标签:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
programproject1; type point=^node; node= record data: longint ; next:point; end ; var i,n,e: longint ; p,q,head,last:point; begin write ( ‘Inputthenumbercount:‘ ); readln(n); i:= 1 ; new(head); read(e); head^.data:=e; head^.next:= nil ; last:=head; q:=head; whilei<ndo begin inc(i); read(e); new(p); q^.next:=p; p^.data:=e; p^.next:= nil ; last:=p; q:=last end ; //建立链表 q:=head; whileq^.next<>nildo begin write (q^.data, ‘‘ ); q:=q^.next; end ; write (q^.data); //输出 readln; readln end . |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
functiondelete(n: longint ;varz:point): longint ; var t,s:point; begin t:=z; while (t^.next<> nil ) and (t^.data<>n) do begin s:=t; t:=t^.next; end ; ift^.data<>nthenexit( 0 ); s^.next:=t^.next; dispose(t); exit⑴ end ; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
functioninsert(w,nn: longint ;varzz:point): longint ; vard: longint ;v,vp,vs:point; begin v:=zz; ford:=1towdo ifv^.next= nil thenexit(d) else begin vp:=v; v:=v^.next; end ; new(vs); vs^.data:=nn; vp^.next:=vs; vs^.next:=v; exit( 0 ) end ; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
#include<stdio.h> #include<stdlib.h> #include<iostream.h> usingnamespacestd; structNode { intdata; //数据域 structNode*next; //指针域 }; /* Create *函数功能:创建链表. *输入:各节点的data *返回值:指针head */ Node*Create() { intn=0; Node*head,*p1,*p2; p1=p2=newNode; cin>>p1->data; head=NULL; while (p1->data!=0) { if (n==0) { head=p1; } else p2->next=p1; p2=p1; p1=newNode; cin>>p1->data; n++; } p2->next=NULL; returnhead; } /* insert *函数功能:在链表中插入元素. *输入:head链表头指针,p新元素插入位置,x新元素中的数据域内容 *返回值:无 */ voidinsert(Node*head,intp,intx) { Node*tmp=head; //for循环是为了防止插入位置超出了链表长度 for (inti=0;i<p;i++) { if (tmp==NULL) return ; if (i<p-1) tmp=tmp->next; } Node*tmp2=newNode; tmp2->data=x; tmp2->next=tmp->next; tmp->next=tmp2; } /* del *函数功能:删除链表中的元素 *输入:head链表头指针,p被删除元素位置 *返回值:被删除元素中的数据域.如果删除失败返回-1 */ intdel(Node*head,intp) { Node*tmp=head; for (inti=0;i<p;i++) { if (tmp==NULL) return -1; if (i<p-1) tmp=tmp->next; } intret=tmp->next->data; tmp->next=tmp->next->next; returnret; } voidprint(Node*head) { for (Node*tmp=head;tmp!=NULL;tmp=tmp->next) printf ( "%d" ,tmp->data); printf ( "\n" ); } intmain() { Node*head; head=newNode; head->data=-1; head->next=NULL; return0; } 例子 #include<iostream> #defineNULL0 structstudent { longnum; structstudent*next; }; intmain() { inti,n; student*p=(structstudent*) malloc ( sizeof (structstudent)); student*q=p; printf ( "输入几个值" ); scanf ( "%d" ,&n); for (i=1;i<=n;i++) { scanf ( "%d" ,&(q->num)); q->next=(structstudent*) malloc ( sizeof (structstudent)); q=q->next; } printf ( "值第几个" ); intrank; scanf ( "%d%d" ,&(q->num),&rank); student*w=p; for (i=1;i<rank-1;i++) { w=w->next; } q->next=w->next; w->next=q; for (i=1;i<=n+1;i++) { printf ( "%d" ,p->num); p=p->next; } return0; } //指针后移麻烦链表形式循环链表 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#include<stdio.h> #include<malloc.h> #defineN41 #defineM5 typedefstructnode*link; structnode { intitem; linknext; }; linkNODE(intitem,linknext) { linkt= malloc ( sizeof *t); t->item=item; t->next=next; returnt; } intmain( void ) { inti; linkt=NODE(1,NULL); t->next=t; for (i=2;i<=N;i++) t=t->next=NODE(i,t->next); while (t!=t->next) { for (i=1;i<M;i++) t=t->next; t->next=t->next->next; } printf ( "%d\n" ,t->item); return0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
#include<stdio.h> #include<stdlib.h> #include<conio.h> typedefstructSlist { intdata; structSlist*next; } SLIST; SLIST*InitList_Sq() /*初始化函数*/ { inta; SLIST*h,*s,*r; h=(SLIST*) malloc ( sizeof (SLIST)); /*建立头指针,头指针不可以更改!!!*/ r=h; if (!h) { printf ( "分配失败" ); exit (0); } scanf ( "%d" ,&a); for (;a!=-1;) { s=(SLIST*) malloc ( sizeof (SLIST)); /*每次都开辟一个结点空间并赋值*/ s->data=a; r->next=s; r=s; scanf ( "%d" ,&a); } r->next= ‘\0‘ ; returnh; } voidprint_list(SLIST*finder) /*打印函数*/ { while (finder!= ‘\0‘ ) { printf ( "->%d" ,finder->data); finder=finder->next; } printf ( "->end\n" ); } intDeleteNode(SLIST*killer) //删除节点函数 { inti,j=0; SLIST*p,*q; intx; p=killer; q=killer->next; printf ( "请输入您要删除的节点序号:" ); scanf ( "%d" ,&i); while ((p->next!= ‘\0‘ )&&(j<i-1)) { p=p->next; j++; q=p->next; } if (p->next== ‘\0‘ ||j>i-1) { printf ( "\nerror" ); return -1; } else { p->next=q->next; x=q->data; free (q); returnx; } } voidInsert_Node(SLIST*jumper) //插入函数,本算法为前插结点法 { intt,e,j=0; SLIST*p,*q; p=jumper; printf ( "请输入要插入位置的序号:" ); scanf ( "%d" ,&t); printf ( "请输入要插入的元素:" ); scanf ( "%d" ,&e); while (p->next!= ‘\0‘ &&j<t-1) { j++; p=p->next; } if (p== ‘\0‘ ||j>t-1) printf ( "插入的目的位置不存在" ); else { q=(SLIST*) malloc ( sizeof (SLIST)); q->data=e; q->next=p->next; p->next=q; } } voidLocate_List(SLIST*reader) //查找值为e的元素 { inte,i=0; SLIST*p; p=reader; printf ( "请输入要查找的元素:" ); scanf ( "%d" ,&e); while (p->next!= ‘\0‘ &&p->data!=e) { i++; p=p->next; } if (p->data==e) printf ( "此元素在%d号位置\n" ,i); else printf ( "无此元素!" ); } voidmain() { inti,k,y; SLIST*head; printf ( "\n1.建立线性表" ); printf ( "\n2.在i位置插入元素e" ); printf ( "\n3.删除第i个元素,返回其值" ); printf ( "\n4.查找值为e的元素" ); printf ( "\n5.结束程序运行" ); printf ( "\n===================================================" ); printf ( "请输入您的选择:" ); scanf ( "%d" ,&k); switch (k) { case1: { head=InitList_Sq(); print_list(head->next); } break ; case2: { head=InitList_Sq(); print_list(head->next); Insert_Node(head); print_list(head->next); } break ; case3: { head=InitList_Sq(); print_list(head->next); y=DeleteNode(head); print_list(head->next); if (y!=-1) printf ( "被删除元素为:%d" ,y); } break ; //头结点不算,从有数据的开始算第一个 case4: { head=InitList_Sq(); print_list(head->next); Locate_List(head); } break ; } } |
标签:
原文地址:http://www.cnblogs.com/wallan/p/5540466.html