408-王道-双向链表

1.选择菜单

在这里插入图片描述

2.头插法

void insertHead(DLinkList& L) {
	DNode* s; int x;
	L = (DLinkList)malloc(sizeof(DNode));//带头结点的链表,不带头结点
	L->next = NULL;
	L->prior = NULL;
	scanf("%d", &x);//从标准输入读取数据
	while (x != 999) {
		s = (DLinkList)malloc(sizeof(DNode));//申请一个空间空间,强制类型转换
		s->data = x;
		s->next = L->next;
		if (L->next != NULL)//插入第一个结点时,不需要这一步操作
		{
			L->next->prior = s;
		}
		s->prior = L;
		L->next = s;
		scanf("%d", &x);//读取标准输入
	}
}

3.尾插法

void insertEnd(DLinkList& L) {
	int x;
	L = (DLinkList)malloc(sizeof(DNode));//带头节点的链表
	DNode* s, * r = L;
	L->prior = NULL;
	scanf("%d", &x);
	while (x != 999) {
		s = (DNode*)malloc(sizeof(DNode));
		s->data = x;
		r->next = s;
		s->prior = r;
		r = s;//r指向新的表尾结点
		scanf("%d", &x);
	}
	r->next = NULL;//尾结点的next指针赋值为NULL
}

4.修改元素

bool modifyNode(DLinkList& L, int index ,ElemType data) {
	DLinkList temp = L->next;
	int count = 0;
	while (temp != NULL) {
		count++;
		if (count == index) {
			temp->data = data;
			return true;
		}
		else {
			temp = temp->next;
		}
	}
	return false;
}

5.查找元素

DLinkList searchNode(DLinkList L , int index) {
	int count = 0;
	DLinkList temp = L->next;
	if (index<1 || index>size(L)) {
		return NULL;
	}
	while (temp != NULL) {
		count++;
		if (count == index) {
			return temp;
		}
		else {
			temp = temp->next;
		}

	}
	return NULL;
}

6.删除元素

bool deleteNode(DLinkList& L, int index ) {
	if (index<1 || index>size(L)) {
		return false;
	}
	else {
		int count = 0;
		DLinkList temp = L->next;
		if (index == 1) {
			L->next = L->next->next;
			L->next->prior = L;
			return true;
		}
		while (temp != NULL) {
			count++;
			if(count == index){
				DLinkList parent = temp->prior;
				parent->next = temp->next;
				temp->prior = parent;
				temp->next = NULL;
				return true;
			}
			else {
				temp = temp->next;
			}
		}

	}

	return false;
}

7.遍历元素

void printfList(DLinkList L) {
	DLinkList temp = L->next;
	int count = 0;
	while (temp != NULL) {
		count++;
		printf("DlinkList[%d] = %d\n", count, temp->data);
		temp = temp->next;
	}
}

8.插入元素

bool insertNode(DLinkList& L, int index, ElemType data) {
	int count = 0;
	if (index<1 || index>size(L)) {
		return false;
	}else {
		DLinkList temp = L->next;
		while (temp != NULL) {
			count++;
			if (count == index) {
				DLinkList parent = temp->prior;
				DLinkList newNode = (DLinkList)malloc(sizeof(DNode));
				newNode->data = data;
				newNode->next = temp;
				newNode->prior = parent;
				parent->next = newNode;
				return true;
			}
			else {
				temp = temp->next;
			}

		}
		return false;
	}
}

9.链表长度

int size(DLinkList L) {
	DLinkList temp = L->next;
	int count = 0;
	while (temp != NULL) {
		count++;
		temp = temp->next;
	}
	return count;
}

10.汇总

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
//双向链表尾插法
//按序号查找结点值
//新结点插入第i个位置
//删除第i个结点
//链表打印
typedef int ElemType;
typedef struct DNode {
	ElemType data;
	struct DNode* prior;
	struct DNode* next;
}DNode,* DLinkList;

void insertHead(DLinkList& L);
void insertEnd(DLinkList& L);
bool deleteNode(DLinkList& L,int index);
bool modifyNode(DLinkList& L, int index,ElemType data);
bool insertNode(DLinkList& L, int index, ElemType data);
void printfList(DLinkList L);
void choice(DLinkList& L);
void menu(DLinkList& L);
int size(DLinkList L);
DLinkList searchNode(DLinkList L, int index);

bool insertNode(DLinkList& L, int index, ElemType data) {
	int count = 0;
	if (index<1 || index>size(L)) {
		return false;
	}else {
		DLinkList temp = L->next;
		while (temp != NULL) {
			count++;
			if (count == index) {
				DLinkList parent = temp->prior;
				DLinkList newNode = (DLinkList)malloc(sizeof(DNode));
				newNode->data = data;
				newNode->next = temp;
				newNode->prior = parent;
				parent->next = newNode;
				return true;
			}
			else {
				temp = temp->next;
			}

		}
		return false;
	}
}
bool deleteNode(DLinkList& L, int index ) {
	if (index<1 || index>size(L)) {
		return false;
	}
	else {
		int count = 0;
		DLinkList temp = L->next;
		if (index == 1) {
			L->next = L->next->next;
			L->next->prior = L;
			return true;
		}
		while (temp != NULL) {
			count++;
			if(count == index){
				DLinkList parent = temp->prior;
				parent->next = temp->next;
				temp->prior = parent;
				temp->next = NULL;
				return true;
			}
			else {
				temp = temp->next;
			}
		}

	}

	return false;
}
bool modifyNode(DLinkList& L, int index ,ElemType data) {
	DLinkList temp = L->next;
	int count = 0;
	while (temp != NULL) {
		count++;
		if (count == index) {
			temp->data = data;
			return true;
		}
		else {
			temp = temp->next;
		}
	}
	return false;

}
int size(DLinkList L) {
	DLinkList temp = L->next;
	int count = 0;
	while (temp != NULL) {
		count++;
		temp = temp->next;
	}
	return count;
}

DLinkList searchNode(DLinkList L , int index) {
	int count = 0;
	DLinkList temp = L->next;
	if (index<1 || index>size(L)) {
		return NULL;
	}
	while (temp != NULL) {
		count++;
		if (count == index) {
			return temp;
		}
		else {
			temp = temp->next;
		}

	}
	return NULL;
}
//双向链表头插法
void insertHead(DLinkList& L) {
	DNode* s; int x;
	L = (DLinkList)malloc(sizeof(DNode));//带头结点的链表,不带头结点
	L->next = NULL;
	L->prior = NULL;
	scanf("%d", &x);//从标准输入读取数据
	while (x != 999) {
		s = (DLinkList)malloc(sizeof(DNode));//申请一个空间空间,强制类型转换
		s->data = x;
		s->next = L->next;
		if (L->next != NULL)//插入第一个结点时,不需要这一步操作
		{
			L->next->prior = s;
		}
		s->prior = L;
		L->next = s;
		scanf("%d", &x);//读取标准输入
	}
}
void insertEnd(DLinkList& L) {
	int x;
	L = (DLinkList)malloc(sizeof(DNode));//带头节点的链表
	DNode* s, * r = L;
	L->prior = NULL;
	scanf("%d", &x);
	while (x != 999) {
		s = (DNode*)malloc(sizeof(DNode));
		s->data = x;
		r->next = s;
		s->prior = r;
		r = s;//r指向新的表尾结点
		scanf("%d", &x);
	}
	r->next = NULL;//尾结点的next指针赋值为NULL
}

void printfList(DLinkList L) {
	DLinkList temp = L->next;
	int count = 0;
	while (temp != NULL) {
		count++;
		printf("DlinkList[%d] = %d\n", count, temp->data);
		temp = temp->next;
	}
}
void choice(DLinkList& L) {
	int choice;
	printf("请输入你的选择:");
	scanf("%d", &choice);
	switch (choice) {
	case 1:
		insertHead(L);
		break;
	case 2:
		insertEnd(L);
		break;
	case 3: {
		int index;
		ElemType data;
		printf("请输入要你修改的序号:");
		scanf("%d", &index);
		printf("修改的值为:");
		scanf("%d", &data);
		bool isModify = modifyNode(L, index, data);
		if (isModify) {
			printf("修改成功\n");
		}
		else {
			printf("修改失败\n");
		}
		break;
	}
	case 4: {
		int index;
		printf("请输入你要查找的序号:");
		scanf("%d", &index);
		DLinkList search = searchNode(L, index);
		if (search != NULL) {
			printf("查找成功,结点数据为%d\n", search->data);
		}
		else {
			printf("查找失败\n");
		}
		break;
	}
	case 5: {
		int index;
		printf("请输入你要删除的序号:");
		scanf("%d",&index);
		bool isDelete = deleteNode(L, index);
		if (isDelete) {
			printf("删除成功\n");
		}
		else {
			printf("删除失败\n");
		}
		break;
	}
	case 6:
		printfList(L);
		break;
	case 7: {
		int index;
		ElemType data;
		printf("请输入你要插入的位置:");
		scanf("%d", &index);
		printf("请输入你要添加的值");
		scanf("%d", &data);
		bool isInsert = insertNode(L, index, data);
		if (isInsert) {
			printf("插入成功\n");
		}
		else {
			printf("插入失败\n");
		}
		break;
	}
	case 8: 
		printf("长度为%d\n", size(L));
		break;
	default:
		break;
	}
	menu(L);

}


void menu(DLinkList& L) {
	printf("----------------双向链表-----------------\n");
	printf("               1.头插法                 \n");
	printf("               2.尾插法                 \n");
	printf("               3.修改元素               \n");
	printf("               4.查找元素               \n");
	printf("               5.删除元素               \n");
	printf("               6.遍历元素               \n");
	printf("               7.插入元素               \n");
	printf("               8.链表长度               \n");
	choice(L);
}




int main() {
	DLinkList L;
	menu(L);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值