双向链表
用结构体指针定义变量制作一个可以查增删改的双向链表存储信息
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
typedef struct info_list
{
int ID;
char name[16];
struct info_list* prev;
struct info_list* next;
}INFO, * PINFO;
PINFO init_list(void);
PINFO make_list(void);
bool insert_list(PINFO p_node, PINFO node, PINFO n_node);
void show_list(PINFO phead);
void find_list(PINFO phead, char* name);
PINFO delete_list(PINFO phead, char* name);
PINFO change_list(PINFO phead, char* name);
int main(void)
{
int num;
PINFO head = init_list();
if (head == NULL)
return -1;
while (true)
{
printf("1.添加节点\t 2.遍历节点\t 3.排序节点\t 4.查询节点\t 5.删除节点\t 6.更改节点\t 0.退出\n");
scanf("%d", &num);
switch (num)
{
case 0:
printf("退出程序!\n");
exit(0);
case 1:
{
printf("添加节点\n");
PINFO pnew = make_list();
if (pnew == NULL)
return -1;
insert_list(head->prev, pnew, head);
break;
}
case 2:
{
printf("遍历查询\n");
show_list(head);
break;
}
case 4:
{
char name[16];
printf("please enter a Name : \n--");
scanf("%s", name);
find_list(head, name);
break;
}
case 5:
{
char name[16];
printf("please enter a Name to delete : \n--");
scanf("%s", name);
delete_list(head, name);
break;
}
case 6:
{
char name[16];
printf("please enter a Name to change : \n--");
scanf("%s", name);
change_list(head, name);
break;
}
default:
printf("非法字符,请重新输入\n");
continue;
}
}
}
PINFO init_list(void)
{
PINFO phead = (PINFO)malloc(sizeof(INFO));
if (phead == NULL)
return NULL;
phead->ID = 0;
strcpy(phead->name, "");
phead->next = phead;
phead->prev = phead;
return phead;
}
PINFO make_list(void)
{
int ID;
char Name[8];
printf("请输入学号和姓名:");
scanf("%d %s", &ID, Name);
PINFO pnew = (PINFO)malloc(sizeof(INFO));
if (pnew == NULL)
{
printf("创建新节点失败\n");
return NULL;
}
pnew->ID = ID;
strcpy(pnew->name, Name);
pnew->prev = pnew->next = pnew;
return pnew;
}
bool insert_list(PINFO p_node, PINFO node, PINFO n_node)
{
if (p_node == NULL || node == NULL || n_node == NULL)
{
printf("插入节点失败!\n");
return false;
}
p_node->next = node;
node->next = n_node;
node->prev = p_node;
n_node->prev = node;
return true;
}
void show_list(PINFO phead)
{
PINFO p = phead->next;
while (p != phead)
{
printf("ID:%d, name:%s\n", p->ID, p->name);
p = p->next;
}
}
void find_list(PINFO phead, char* name)
{
PINFO p = phead->next;
while (p != phead)
{
if (strcmp(p->name, name) == 0)
{
printf("找到该名字\n");
printf("ID=%d \t Name=%s \n", p->ID, p->name);
return;
}
if (p->next == phead)
{
printf("没有找到该名字\n");
return;
}
p = p->next;
}
}
PINFO delete_list(PINFO phead, char* name)
{
PINFO p = phead->next;
while (p != phead)
{
if (strcmp(p->name, name) == 0)
{
PINFO x = p->prev;
PINFO y = p->next;
x->next = y;
y->prev = x;
free(p);
return p;
}
if (p->next == phead)
{
printf("没有找到该名字\n");
return NULL;
}
p = p->next;
}
}
PINFO change_list(PINFO phead, char* name)
{
PINFO p = phead->next;
while (p != phead)
{
if (strcmp((p->name), name) == 0)
{
int ID;
char Name[8];
printf("请输入要修改的数据:学号 姓名");
scanf("%d %s",&ID,Name);
p->ID = ID;
strcpy((p->name),Name);
return p;
}
if (p->next == phead)
{
printf("没有找到该名字");
return NULL;
}
p->next;
}
}
|