#include <stdio.h>
#include <stdlib.h>
struct node
{
int data ;
struct node *next;
};
int add_node(struct node *head ,int data)
{
struct node *new = malloc(sizeof(struct node));
new->next = NULL ;
new->data = data ;
printf("-------1-------\n");
struct node *p = head ;
while(p->next != NULL)
{
p = p->next ;
}
p->next = new ;
return 0;
}
int last_node(struct node *head , int data)
{
struct node *new = malloc(sizeof(struct node ));
new->next = NULL ;
new->data = data ;
printf("-------1-------\n");
new->next = head->next;
head->next = new ;
return 0;
}
int dele_node(struct node *head , int dele_data)
{
struct node *pos = head ;
struct node *tmp = pos ;
while(pos !=NULL)
{
if(pos->data == dele_data)
{
printf("找到删除的数据:%d\n",pos->data);
tmp->next = pos->next ;
pos->next = NULL;
free(pos);
pos = tmp ;
}
tmp = pos ;
pos = pos->next ;
}
return 0;
}
int find_node(struct node *head ,int find_data)
{
struct node *p = head ;
int flag = 0;
while(p != NULL)
{
if(p->data == find_data)
{
printf("找到要查找的数据:%d\n",p->data);
flag = 1;
}
p = p->next;
}
if(flag == 0)
{
printf("没找到对应的数据\n");
}
return 0;
}
int xiugai_node(struct node *head , int old_data ,int new_data )
{
struct node *p = head;
int ret = 0 ;
while(p->next !=NULL)
{
if(p->data == old_data)
{
p->data = new_data ;
ret = 1;
}
p = p->next;
}
if(ret == 0)
{
printf("没有找到该数据");
}
return 0;
}
void show_node(struct node *head)
{
struct node *p = head->next ;
while(p != NULL)
{
printf("数据显示: %d\n", p->data);
p = p->next ;
}
return ;
}
int main(void)
{
struct node *head = malloc(sizeof(struct node));
head->next = NULL;
head->data = 0;
while(1)
{
int a = 0;
printf("请输入对应的功能:1 尾插 2头插 3查找数据 4删除数据 5显示数据 6修改数据\n");
scanf("%d",&a);
switch(a)
{
case 1 :
{
int c = 0;
printf("请输入尾插数据:\n");
scanf("%d",&c);
add_node(head ,c);
}
break;
case 2 :
{
int c = 0;
printf("请输入头插数据:\n");
scanf("%d",&c);
last_node( head , c);
}
break;
case 3 :
{
int c = 0;
printf("请输入查找数据:\n");
scanf("%d",&c);
find_node(head ,c);
}
break;
case 4 :
{
int c = 0;
printf("请输入删除数据:\n");
scanf("%d",&c);
dele_node(head , c);
}
break;
case 5 :
{
show_node(head);
}
break;
case 6 :
{
int c = 0;
printf("请输入需要修改旧数据:\n");
scanf("%d",&c);
int d = 0;
printf("请输入修改新数据");
scanf("%d",&d);
xiugai_node(head , c,d );
}
}
}
return 0;
}