001 | /*单链表的各种操作*/ |
002 |
003 | # define null 0 |
004 |
005 | typedef
char ElemType; /* 字符型数据*/ |
006 |
007 | typedef
struct LNode |
008 | { |
009 | ElemType data; |
010 | struct LNode *next; |
011 | }; |
012 | |
013 | setnull( struct LNode **p); |
014 | int
length ( struct
LNode **p); |
015 | ElemType get( struct LNode **p, int i); |
016 | void
insert( struct
LNode **p,ElemType x, int
i); |
017 | int
delete ( struct LNode **p, int i); |
018 | void
display( struct
LNode **p); |
019 |
020 | main() |
021 | { |
022 | struct LNode *head,*q;
/*定义静态变量*/ |
023 | int select,x1,x2,x3,x4; |
024 | int i,n;
|
025 | int m,g; |
026 | char e,y;
|
027 | |
028 | head=setnull(&head); /*建议链表并设置为空表*/ |
029 | printf ( "请输入数据长度: " ); |
030 | scanf ( "%d" ,&n); |
031 | for (i=1;i<n;i++); |
032 | { |
033 | printf ( "将数据插入到单链表中: " ); |
034 | scanf ( "%d" ,&y); |
035 | insert(&head,y,i);} /*插入数据到链表*/ |
036 | display(&head); /*显示链表所有数据*/ |
037 | |
038 | printf ( "select 1 求长度 length()\n" ); |
039 | printf ( "select 2 取结点 get()\n" ); |
040 | printf ( "select 3 求值查找 locate()\n" ); |
041 | printf ( "select 4 删除结点 delete()\n" ); |
042 | printf ( "input your select: " ); |
043 | scanf ( "%d" ,&select); |
044 | switch (select) |
045 | { |
046 | case 1: |
047 | { |
048 | x1=length(&head); |
049 | printf ( "输出单链表的长度%d " ,x1); |
050 | display(&head); |
051 | } break ; |
052 | |
053 | case 2: |
054 | { |
055 | printf ( "请输入要取得结点: " ); |
056 | scanf ( "%d" ,&m); |
057 | x2=get(&head,m); |
058 | printf (x2); |
059 | display(&head); |
060 | } break ; |
061 | |
062 | case 3: |
063 | { |
064 | printf ( "请输入要查找的数据: " ); |
065 | scanf ( "%d" ,&e); |
066 | x3=locate(&head,e); |
067 | printf (x3); |
068 | display(&head); |
069 | } break ; |
070 | |
071 | case 4: |
072 | { |
073 | printf ( "请输入要删除的结点: " ); |
074 | scanf ( "%d" ,&g); |
075 | x4= delete (&head,g); |
076 | printf (x4); |
077 | display(&head); |
078 | } break ; |
079 | } |
080 | } |
081 | } |
082 |
083 |
084 | setnull( struct LNode **p) |
085 | { |
086 | *p=null; |
087 | } |
088 |
089 | int
length ( struct
LNode **p) |
090 | { |
091 | int n=0; |
092 | struct LNode *q=*p; |
093 | while (q!=null) |
094 | { |
095 | n++; |
096 | q=q->next; |
097 | } |
098 | return (n); |
099 | } |
100 |
101 | ElemType get( struct LNode **p, int i) |
102 | { |
103 | int j=1; |
104 | struct LNode *q=*p; |
105 | while (j<i&&q!=null) |
106 | { |
107 | q=q->next; |
108 | j++; |
109 | } |
110 | if (q!=null) |
111 | return (q->data); |
112 | else |
113 | printf ( "位置参数不正确!\n" ); |
114 | } |
115 |
116 | int
locate( struct
LNode **p,ElemType x) |
117 | { |
118 | int n=0; |
119 | struct LNode *q=*p; |
120 | while (q!=null&&q->data!=x) |
121 | { |
122 | q=q->next; |
123 | n++; |
124 | } |
125 | if (q==null) |
126 | return (-1); |
127 | else |
128 | return (n+1); |
129 | } |
130 |
131 | void
insert( struct
LNode **p,ElemType x, int
i) |
132 | { |
133 | int j=1; |
134 | struct LNode *s,*q; |
135 | s=( struct LNode *) malloc ( sizeof ( struct LNode)); |
136 | s->data=x; |
137 | q=*p; |
138 | if (i==1) |
139 | { |
140 | s->next=q; |
141 | p=s; |
142 | } |
143 | else |
144 | { |
145 | while (j<i-1&&q->next!=null) |
146 | { |
147 | q=q->next; |
148 | j++; |
149 | } |
150 | if (j==i-1) |
151 | { |
152 | s->next=q->next; |
153 | q->next=s; |
154 | } |
155 | else |
156 | printf ( "位置参数不正确!\n" ); |
157 | }
|
158 | } |
159 |
160 | int
delete ( struct LNode **p, int i) |
161 | { |
162 | int j=1; |
163 | struct LNode *q=*p,*t; |
164 | if (i==1) |
165 | { |
166 | t=q; |
167 | *p=q->next; |
168 | } |
169 | else |
170 | { |
171 | while (j<i-1&&q->next!=null) |
172 | { |
173 | q=q->next; |
174 | j++; |
175 | } |
176 | if (q->next!=null&&j==i-1) |
177 | { |
178 | t=q->next; |
179 | q->next=t->next; |
180 | } |
181 | else |
182 | printf ( "位置参数不正确!\n" ); |
183 | } |
184 | if (t=null) |
185 | free (t); |
186 | } |
187 |
188 | void
display( struct
LNode **p) |
189 | { |
190 | struct LNode *q; |
191 | q=*p; |
192 | printf ( "单链表显示: " ); |
193 | if (q==null) |
194 | printf ( "链表为空!" ); |
195 | else if
(q->next==null) |
196 | printf ( "%c\n" ,q->data); |
197 | else |
198 | { |
199 | while (q->next!=null) |
200 | { |
201 | printf ( "%c->" ,q->data); |
202 | q=q->next; |
203 | } |
204 | printf ( "%c" ,q->data); |
205 | } |
206 | printf ( "\n" ); |
207 | } |