链表源代码(包含头插法尾插法)vs2019环境下,可直接运行

链表源代码(包含头插法尾插法)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) {
	//1断言
	//2给plist每一个成员赋初值
	//3创建头结点
	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;
	//pnewnode->next = NULL;可写可不写
	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;
	}
	//申请一个临时指针p指向待删除结点
	//申请一个临时指针q指向删除结点的上一个结点
	//指向跨越(本质让待删除结点上一个结点的next域保存待删除结点下一个结点)
	//释放结点
	struct Node* p = plist->next;
	plist->next = p->next;
	//struct Node* q = plist;
	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);
	//申请一个临时指针p指向待删除结点
	//申请一个临时指针q指向删除结点的上一个结点
	struct Node* q = plist;
	//for (; pos > 0; pos--) {
	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;
}

运行截图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值