# 链表--玩转学生管理系统

（1）单链表

（2）双链表

1.创建链表
（1）头插法

（2）尾插法

typedef struct DLNode {
int data;
struct DLNode *prior;
struct DLNode next;
}DLNode,LDLNode;

void createDLink(DLNode *&p,int n)
{
int i;
int arr[100];
DLNode *r, *s;
p = (DLNode *)malloc(sizeof(DLNode));
p->prior = NULL;
p->next = NULL;
r = p;
for (i = 0; i < n; i++)
{
s = (DLNode *)malloc(sizeof(DLNode));
printf(“输入第%d个节点:”, i + 1);
scanf("%d", &arr[i]);
s->data = arr[i];
r->next = s;
s->prior = r;
r = s;
}
r->next = NULL;
}**：

2.查找（两种返回方式）

（1）按位置查找

//查找返回节点位置指针
LDLNode search(DLNode q, int pos)
{
int j = 0;
DLNode r;
r = q;
while (r != NULL && j < pos)
{
r = r->next;
j++;
}
if (j == pos)
return r;
}

（2）按值查找

LDLNode search(DLNode q, int data1)
{
int j = 0;
DLNode r;
r = q;
while (r != NULL&&r->data!=data1 )
{
r = r->next;
}
return r;
}

3.修改

//修改某节点数据
void modifyNode(DLNode *&h, int pos, int data)
{
DLNode *p = h;
int i = 0;
for (; p != NULL; p = p->next)
{
if (i == pos)
{
p->data = data;
printf(“modify successful”);
break;
}
i++;
}
}

4.删除

//删除结点
**void del(DLNode p, int pos)
{
DLNode p1 = find(p, pos);
if (p1 == NULL||pos<1)
{
printf(“error!\n”);
}
else
{
if (p1->prior == NULL && p1->next != NULL)
{
p1->next->prior = NULL; p = p1->next;//头指针变化
}
else if (p1->next == NULL && p1->prior != NULL)//尾指针
{
p1->prior->next = NULL;
}
else if (p1->prior == NULL && p1->next == NULL)//头尾都为空
{
p = NULL;
}
//头尾都不为空
else
{
p1->prior->next = p1->next;
p1->next->prior = p1->prior;
}
printf(“success! delete data”);
}
}

5.插入

**//插入结点(在第pos位置上插入data)
int insert(DLNode *&p, int data, int pos)
{
DLNode *p1 = find(p, pos);
DLNode *s;
s= (DLNode )malloc(sizeof(DLNode));
s->data = data;
if (pos < 1||(pos>get_List§+1))
{
printf(“error!\n”);
return 1;
}
else
{
if (pos == get_List§ + 1)
{
DLNode p2 = find(p, pos - 1);
p2->next = s;
s->prior = p2;
s->next = NULL;
}
else
{
if (p1->prior == NULL && p1->next != NULL)
{
s->next = p1;
p1->prior = s;
p = s;//头指针变化
}
else if (p1->prior == NULL && p1->next == NULL)
{
p = NULL;
}
else
{
s->prior = p1->prior;
p1->prior->next = s;
s->next = p1;
p1->prior = s;
}
}
printf(“success insert data”);
}
return 0;
}

#include <iostream
#include<stdio.h>
#include<stdlib.h>
typedef struct DLNode {
int data;
struct DLNode *prior;
struct DLNode *next;
}DLNode,*LDLNode;
void menu()//功能菜单
{
printf(" |________________________________________________|\n");
printf(" | |\n");
printf(" | |\n");
printf(" | |\n");
printf(" | 0、退出系统 |\n");
printf(" | 1、创建节点 |\n");
printf(" | 2、删除节点 |\n");
printf(" | 3、修改节点 |\n");
printf(" | 4、查找节点 |\n");
printf(" | 5、插入节点 |\n");
printf(" | 6、显示全部节点 |\n");
printf(" | |\n");
printf(" | |\n");
printf(" |________________________________________________|\n");
return;
}
//创建双链表
void createDLink(DLNode *&p,int n)
{
int i;
int arr[100];
DLNode *r, *s;
p = (DLNode *)malloc(sizeof(DLNode));
p->prior = NULL;
p->next = NULL;
r = p;
for (i = 0; i < n; i++)
{
s = (DLNode *)malloc(sizeof(DLNode));
printf(“输入第%d个节点:”, i + 1);
scanf("%d", &arr[i]);
s->data = arr[i];
r->next = s;
s->prior = r;
r = s;
}
r->next = NULL;
}
//链表长度
int get_List(DLNode *head)
{
DLNode *p;
int len = 0;
p = head;
for (p; p->next != NULL; p = p->next)
{
len++;
}
return len;
}
//查找节点位置指针
LDLNode find(DLNode *&q, int pos)
{
int j = 0;
DLNode *r;
r = q;
while (r != NULL && j < pos)
{
r = r->next;
j++;
}
if (j == pos)return r;
else return NULL;
}
//查找结点,返回结点位置
int search(DLNode *q, int pos)
{
DLNode *p1 = find(q, pos);
if (p1 == NULL || pos < 1)
{
printf(“error”);
return 1;
}
else
{
printf(“查找到第%d节点数据：%d”,pos, p1->data);
}
return 0;
}
//修改某节点数据
void modifyNode(DLNode *&h, int pos, int data)
{
DLNode *p = h;
int i = 0;
for (; p != NULL; p = p->next)
{
if (i == pos)
{
p->data = data;
printf(“modify successful”);
break;
}
i++;
}
}
//删除结点
void del(DLNode *p, int pos)
{
DLNode *p1 = find(p, pos);
if (p1 == NULL||pos<1)
{
printf(“error!\n”);
return;
}
else
{
if (p1->prior == NULL && p1->next != NULL)
{
p1->next->prior = NULL; p = p1->next;//头指针变化
}
else if (p1->next == NULL && p1->prior != NULL)
{
p1->prior->next = NULL;
}
else if (p1->prior == NULL && p1->next == NULL)
{
p = NULL;
}
else
{
p1->prior->next = p1->next;
p1->next->prior = p1->prior;
}
printf(“success! delete data”);
}
}
//插入结点(在第pos位置上插入data)
int insert(DLNode *&p, int data, int pos)
{
DLNode *p1 = find(p, pos);
DLNode *s;
s= (DLNode *)malloc(sizeof(DLNode));
s->data = data;
if (pos < 1||(pos>get_List§+1))
{
printf(“error!\n”);
return 1;
}
else
{
if (pos == get_List§ + 1)
{
DLNode *p2 = find(p, pos - 1);
p2->next = s;
s->prior = p2;
s->next = NULL;
}
else
{
if (p1->prior == NULL && p1->next != NULL)
{
s->next = p1;
p1->prior = s;
p = s;//头指针变化
}
else if (p1->prior == NULL && p1->next == NULL)
{
p = NULL;
}
else
{
s->prior = p1->prior;
p1->prior->next = s;
s->next = p1;
p1->prior = s;
}
}
printf(“success insert data”);
}
return 0;
}
//打印双链表
void print(DLNode *p)
{
DLNode *q;
q = p->next;
while (q != NULL)
{
printf("%d “, q->data);
q = q->next;
}
printf(”\n");
return;
}
int main()
{
int data, index;
DLNode *head=NULL;
DLNode *p2 = NULL;
int choose;
while (1)
{
menu();//功能菜单
printf("\n");
printf(“请输入您要选择的功能键：\n”);
scanf("%d", &choose);
switch (choose)
{
case 0://退出
printf(“谢谢使用！欢迎下次光临”);
exit(0);
case 1:
int n;
printf("\n");
printf(“请输入要创建的节点数：”);
scanf("%d", &n);
createDLink(head, n);
printf("\n");
break;
case 2:
printf("\n");
printf(“请输入删除的位置：”);
scanf("%d", &index);
del(head, index);
printf("\n");
break;
case 3:
printf("\n");
printf(“请输入的修改的位置：”);
scanf("%d", &index);
p2 = find(head, index);
if (p2 == NULL || index < 1)
{
printf(“error!\n”);
break;
}
printf(“请输入修改后的的数据：”);
scanf("%d", &data);
printf("\n");
modifyNode(head,index,data);
break;
case 4:
printf("\n");
printf(“请输入查找的节点：”);
scanf("%d", &index);
search(head, index);
printf("\n");
break;
case 5:
printf("\n");
printf(“请输入插入的数据：”);
scanf("%d", &data);
printf(“请输入插入的位置：”);
scanf("%d", &index);
insert(head, data, index);
printf("\n");
break;
case 6:
printf("\n");
print(head);
printf("\n");
break;
default:
printf("\n");
printf(“请输入正确的选择\n”);
printf("\n");
break;
}
}
system(“pause”);
return 0;
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

Schrodinger's Ali

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
04-10 151

10-21 1060
05-21 33
09-28 42万+
10-24 3603
04-08 118万+
09-14 3万+
10-19
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客