单链表的基本操作
#include <stdio.h>
typedef struct Node{
int info;
struct Node *next;
}*LinkList,*Lnode;
LinkList createNullList(){
LinkList list = (LinkList)malloc(sizeof(Lnode));
list->next = NULL;
printf("创建成功\n");
return list;
}
void insert (LinkList list,int x){
Lnode p = (Lnode)malloc(sizeof(Lnode));
p->info = x;
if(list->next == NULL){
list->next = p;
p->next = NULL;
}else{
p->next = list->next;
list->next = p;
}
}
void deleteElem(LinkList list,int x){
Lnode p = list->next;
Lnode pre = list;
while(p!=NULL && p->info!=x){
pre = p;
p = p->next;
}
if(p==NULL){
printf("没有这个值");
}else{
pre ->next = p->next;
free(p);
}
}
void show(LinkList list){
Lnode p;
p=list;
while(p->next!=NULL){
printf("%d ",p->next->info);
p = p->next;
}
}
int main(){
LinkList list = createNullList();
int x;
scanf("%d",&x);
while(x != 99){
insert(list,x);
scanf("%d",&x);
}
show(list);
deleteElem(list,3);
show(list);
}
|