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

前言
这段时间将以前学习的东西整理了下,学欲进,常有疏,一个人在学习过程中要不断总结,这样才会有所收获。今天来总结下数据结构中链表的知识,链表无外乎离不开增删改查,链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer)。

两种链表
(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;
}

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页