数据结构(C语言版)求一个创建线性表、插入运算的程序和算法
哪位大佬可以给我一个创建线性表、插入运算的程序和算法呀,有注释的那种,想仔细的学习一个..
//Windows7环境 VS2017编译
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef struct NAME //自定义名字
{
int data;
struct NAME *next;
} name;
name *creat(int n) //创建长度为n的单链表
{
name *head, *node, *end;
head = (name*)malloc(sizeof(name));
end = head;
for (int i = 0; i < n; i++)
{
node = (name*)malloc(sizeof(name));
printf("第%d个节点的数据域为:", i + 1);
scanf("%d", &node->data);
printf("\n");
end->next = node;
end = node;
}
end->next = NULL;
return head;
}
void delet_normal(name*list, int n) //删除第n个节普通点并释放内存
{
name *in = nullptr; // in:所删除节点的前节点, t:要删除的节点
name *t = list;
int i = 0;
while (i < n && t != NULL)
{
in = t;
t = t->next;
i++;
}
if (t != NULL)
{
in->next = t->next;
free(t);
printf("删除成功\n");
}
else printf("该链表没有第%d个节点\n", n);
}
//删除表头节点为void delet_normal(name*list, 1)
void delet_tail(name * list) //删除表尾节点
{
name*t = list;
name *in = nullptr;
while (t->next != NULL) { //使t成为尾节点,in 为其前一个节点
in = t;
t = t->next;
}
in->next = NULL;
free(t);
printf("删除成功\n");
}
void inter_normal(name *list, int n, int data) //在第n个节点之后插入新节点,新节点数据域为data
{
name *t = list; //in:新插入的节点;t:第n个节点
name *in;
int i = 0;
while (i < n&&t != NULL)
{
t = t->next;
i++;
}
if (t != NULL)
{
in = (name*)malloc(sizeof(name));
in->data = data;
in->next = t->next;
t->next = in;
}
else
printf("该链表没有第%d个节点\n", n);
}
//在表头插入节点为void inter_normal(name *list, 0,data)
void inter_tail(name *list, int data) //在表尾插入节点
{
name *in; //in为插入的节点
name *t = list;
while (t->next != NULL) //使t成为尾节点
t = t->next;
in = (name*)malloc(sizeof(name));
in->data = data;
t->next = in;
in->next = NULL;
}
void output(name *list) //遍历输出
{
name *t = list;
t = t->next;
while (t != NULL)
{
printf("【%d】 ", t->data);
t = t->next;
}
printf(" end \n");
}
void search(int aim, name *list) //查找数据域元素出现的位置
{
bool flag = false;
name *t = list;
t = t->next;
for (int i = 1; t != NULL; t = t->next)
{
if (t->data == aim)
{
printf("第%d个 ", i);
flag = true;
}
i++;
}
if (flag == false)
{
printf("无该元素");
}
printf("\n");
}
void read(name*list, int a) //读出链表第a个元素
{
name *t = list;
int i = 0;
while (i < a&&t != NULL)
{
t = t->next;
i++;
}
if (t == NULL)
{
printf("该链表没有第%d个元素\n", a);
}
else
printf("第%d个元素数据域为%d\n", a, t->data);
}
void change(int a, name*list, int b) //修改第a个元素的数据域为b
{
name*t = list;
for (int i = 0; i < a; i++)
{
t = t->next;
}
if (t != NULL)
{
t->data = b;
printf("修改成功\n");
}
else
printf("该链表没有第%d个节点", a);
}
int main()
{
name * A = nullptr;
for (;;)
{
printf("选择功能\n1:创建新链表\n2:遍历输出链表数据域\n3:在第n个节点后插入节点\n4:在表头插入节点\n5:在表尾插入节点\n6:删除第n个节点后的节点\n7:删除头节点\n8:删除尾节点\n9:读出链表第n个元素的数据域 \n10:修改第n个节点的数据域\n11:查找数据域为a的节点在链表中的位置\n0:退出\n\n\n");
int choice;
scanf("%d", &choice);
switch (choice)
{
case 1:
int lenth;
printf("新建链表长度为:");
scanf("%d", &lenth);
A = creat(lenth);
break;
case 2:
output(A);
break;
case 3:
int locate, data;
printf("在第几个节点后插入?\n");
scanf("%d", &locate);
printf("插入节点的数据域为:");
scanf("%d", &data);
inter_normal(A, locate, data);
break;
case 4:
printf("插入节点的数据域为:");
scanf("%d", &data);
inter_normal(A, 0, data);
break;
case 5:
printf("插入节点的数据域为:");
scanf("%d", &data);
inter_tail(A, data);
break;
case 6:
printf("删除第n个节点 n为:");
scanf("%d", &locate);
delet_normal(A, locate);
break;
case 7:
delet_normal(A, 1);
break;
case 8:
delet_tail(A);
break;
case 9:
printf("读出第n个节点数据域 n为:");
scanf("%d", &locate);
read(A, locate);
break;
case 10:
printf("修改第n个节点数据域 n为:");
scanf("%d", &locate);
printf("想要改成:");
scanf("%d", &data);
change(locate, A, data);
break;
case 11:
printf("输入要查找的数据域:");
scanf("%d", &data);
search(data, A);
break;
case 0:
exit(0);
}
}
system("pause");
return 0;
}