001 | /*单链表的各种操作*/ |
002 |
003 | # define null 0 |
004 |
005 | typedef
char ElemType; /* 字符型数据*/ |
006 |
007 | typedef
struct LNode |
008 | { |
009 | ElemType data; |
010 | structLNode *next; |
011 | }; |
012 | |
013 | setnull(structLNode **p); |
014 | int
length (struct
LNode **p); |
015 | ElemType get(structLNode **p,inti); |
016 | void
insert(struct
LNode **p,ElemType x,int
i); |
017 | int
delete(structLNode **p,inti); |
018 | void
display(struct
LNode **p); |
019 |
020 | main() |
021 | { |
022 | structLNode *head,*q;
/*定义静态变量*/ |
023 | intselect,x1,x2,x3,x4; |
024 | inti,n;
|
025 | intm,g; |
026 | chare,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 | case1: |
047 | { |
048 | x1=length(&head); |
049 | printf("输出单链表的长度%d ",x1); |
050 | display(&head); |
051 | }break; |
052 | |
053 | case2: |
054 | { |
055 | printf("请输入要取得结点: "); |
056 | scanf("%d",&m); |
057 | x2=get(&head,m); |
058 | printf(x2); |
059 | display(&head); |
060 | }break; |
061 | |
062 | case3: |
063 | { |
064 | printf("请输入要查找的数据: "); |
065 | scanf("%d",&e); |
066 | x3=locate(&head,e); |
067 | printf(x3); |
068 | display(&head); |
069 | }break; |
070 | |
071 | case4: |
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(structLNode **p) |
085 | { |
086 | *p=null; |
087 | } |
088 |
089 | int
length (struct
LNode **p) |
090 | { |
091 | intn=0; |
092 | structLNode *q=*p; |
093 | while(q!=null) |
094 | { |
095 | n++; |
096 | q=q->next; |
097 | } |
098 | return(n); |
099 | } |
100 |
101 | ElemType get(structLNode **p,inti) |
102 | { |
103 | intj=1; |
104 | structLNode *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 | intn=0; |
119 | structLNode *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 | intj=1; |
134 | structLNode *s,*q; |
135 | s=(structLNode *)malloc(sizeof(structLNode)); |
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(structLNode **p,inti) |
161 | { |
162 | intj=1; |
163 | structLNode *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 | structLNode *q; |
191 | q=*p; |
192 | printf("单链表显示: "); |
193 | if(q==null) |
194 | printf("链表为空!"); |
195 | elseif
(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 | } |
1335

被折叠的 条评论
为什么被折叠?



