链表源代码(包含头插法尾插法)vs2019环境下
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int ELEM_TYPE;
typedef struct Node {
ELEM_TYPE data;
struct Node* next;
}Node, * PNode;
void Init_list(PNode plist) {
assert(plist != NULL);
plist->next = NULL;
}
bool Insert_head(struct Node* plist, ELEM_TYPE val) {
assert(plist != NULL);
struct Node* pnewnode = (PNode)malloc(sizeof(struct Node));
pnewnode->data = val;
pnewnode->next = plist->next;
plist->next = pnewnode;
return true;
}
bool Insert_tail(Node* plist, ELEM_TYPE val) {
assert(plist != NULL);
struct Node* pnewnode = (struct Node*)malloc(sizeof(Node));
assert(pnewnode != NULL);
pnewnode->data = val;
struct Node* p = plist;
while ((plist->next) != NULL) {
plist = plist->next;
}
plist->next = pnewnode;
pnewnode->next = NULL;
plist = p;
return true;
}
bool Insert_pos(PNode plist, int pos, ELEM_TYPE val) {
assert(plist != NULL);
struct Node* pnewnode = (PNode)malloc(sizeof(struct Node));
pnewnode->data = val;
struct Node* p = plist;
while (pos != 0) {
p = p->next;
pos--;
}
pnewnode->next = p->next;
p->next = pnewnode;
return 1;
}
bool Del_head(PNode plist) {
assert(plist != NULL);
if (plist->next==NULL) {
return 0;
}
struct Node* p = plist->next;
plist->next = p->next;
free(p);
p = NULL;
}
bool Del_tail(PNode plist) {
assert(plist != NULL);
if (plist->next==NULL) {
return 0;
}
struct Node* p = plist;
for (; p->next != NULL; p = p->next);
struct Node* q = plist;
for (; q->next != p; q = q->next);
q->next = p->next;
free(p);
p = NULL;
}
bool Del_pos(Node* plist, int pos) {
assert(plist != NULL);
struct Node* q = plist;
for (int i = 0; i < pos; i++) {
q = q->next;
}
struct Node* p = q->next;
q->next = p->next;
free(p);
p = NULL;
return 1;
}
void Show(Node* plist) {
struct Node* p = plist->next;
for (; p != NULL; p = p->next) {
printf("%d ", p->data);
}
}
void Menu() {
printf("\n\n");
printf("#######################################################\n");
printf("1头插法\n");
printf("2尾插法\n");
printf("3输出该链表\n");
printf("4尾删元素(删除最后一个元素)\n");
printf("5头删元素(删除最第一个元素)\n");
printf("6按位置插入\n");
printf("7按位置删除\n");
printf("######################################################\n");
printf("\n\n\n");
}
int main() {
Node stu;;
int n;
int num;
int num1,num2;
Init_list(&stu);
while (1) {
Menu();
printf("请输入你的选项\n");
scanf("%d", &num);
switch (num) {
case 1:
printf("请输入头插的数据");
scanf("%d", &n);
Insert_head(&stu, n);
printf("头插成功\n");
break;
case 2:
printf("请输入尾插的数据");
scanf("%d", &n);
Insert_tail(&stu, n);
printf("尾插成功\n");
break;
case 3:printf("该链表中包含的数据为\n");
Show(&stu); break;
case 4:
Del_tail(&stu);
printf("尾删成功\n"); break;
case 5:
Del_head(&stu);
printf("头删成功\n");
break;
case 6:printf(" 请输入你要插入的位置及数据用逗号隔开\n");
scanf("%d,%d", &num1, &num2);
Insert_pos(&stu, num1,num2);
printf("按位置插入成功\n");
break;
case 7:printf(" 请输入你要删除的位置\n");
scanf("%d", &num1);
Del_pos(&stu, num1);
printf("按位置删除成功\n");
break;
}
}
return 0;
}
运行截图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d32d0c24946ba01dc7da148213aad1fd.png)