int型双链表,大学基础数据结构

int型双链表

大学基础数据结构

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

typedef int ElemType;

typedef struct LNode {//双链表结构
	ElemType data;//数据
	struct LNode* next;//后指针
	struct LNode* prior;//前指针
}LNode, *DLinkList;

bool InitDLinkList(DLinkList&L) {//初始化双链表
	L = (LNode*)malloc(sizeof(LNode));//头结点
	if (L == NULL)	return false;
	L->next = NULL;
	L->prior = NULL;//头结点初始化
	return true;
}

bool InsertNextNode(LNode* p,ElemType e) {//在给定结点后插
	if (p == NULL)return false;
	LNode* s = (LNode*)malloc(sizeof(LNode));
	s->data = e;
	if (p->next == NULL) {//如果p是最后一个结点
		s->next = NULL;
		s->prior = p;
		p->next = s;
	}
	else {
		s->next = p->next;
		s->prior = p;
		s->next->prior = s;
		p->next = s;
	}
}

bool InsertPriorNode(LNode* p, ElemType e) {//给定结点前插
	if (p == NULL) return false;
	LNode* s = (LNode*)malloc(sizeof(LNode));
	s->data = e;
	if (p->prior == NULL) {//该结点是头结点
		s->next = p;
		s->prior = NULL;
		p->prior = s;
	}
	else {
		s->next = p;
		s->prior = p->prior;
		s->prior->next = s;
		s->next->prior = s;
	}

}

bool DeletNode(LNode* p) {//删除p结点
	if (p == NULL||p->prior == NULL)	return false;//不存在p结点,或者p是头结点,不能删
	else if (p->next==NULL) {//p是尾结点
		p->prior->next= NULL;
		free(p);
	}
	else {//p是中间的结点
		p->prior->next = p->next;
		p->next->prior = p->prior;
		free(p);
	}
}

LNode* GetNode_i(DLinkList L,int i) {//返回i位置的结点指针
	if (i < 0)	return NULL;
	LNode* p = L;
	int j = 0;
	while (p!=NULL&&j<i) {//i=0返回头指针,i=1如果表中只有头结点返回NULL,否则返回第一个结点
		p = p->next;
		j++;
	}
	return p;
}

LNode* GetNode_val(DLinkList L, ElemType e) {//返回值为i的第一个结点的指针
	if (L == NULL) return L;
	LNode* p = L->next;//p为第一个结点
	while (p != NULL && p->data != e) {
		p = p->next;
	}
	return p;
}

bool Insert_i(DLinkList& L, int i, ElemType e) {//在第i位置插入结点
	if (i < 1) return false;
	LNode* p=GetNode_i(L, i - 1);
	InsertNextNode(p, e);
}

bool head_Insert_fun1(DLinkList& L) {//头部正序插入
	LNode* p = L;
	int x;
	scanf("%d", &x);
	while (x!=9999) {
		InsertNextNode(p, x);
		p = p->next;
		scanf("%d", &x);
	}
	return true;
}

bool head_Insert(DLinkList& L) {
	LNode* p = L;
	int x;
	scanf("%d", &x);
	while (x != 9999) {
		InsertNextNode(p, x);
		scanf("%d", &x);
	}
	return true;
}

void PrintList(DLinkList L) {
	LNode* p = L->next;
	while (p != NULL) {
		printf("%d ", p->data);
		p = p->next;
	}
}

int list_len(DLinkList L) {//求表长
	int i=0;
	LNode* p = L;
	while (p->next != NULL) {
		i++;
		p = p->next;
	}
	return i;
}

bool tail_insert(DLinkList& L) {
	int i=list_len(L);
	LNode* r=GetNode_i(L,i);
	int x;
	scanf("%d", &x);
	while (x != 9999) {
		InsertNextNode(r, x);
		r = r->next;
		scanf("%d", &x);
	}
	return true;
}
int main() {
	DLinkList L=NULL;
	InitDLinkList(L);
	head_Insert(L);
	LNode* p=GetNode_i(L, 1);
	InsertNextNode(p, 6666);
	InsertPriorNode(p, 8888);
	//head_Insert_fun1(L);
	tail_insert(L);
	//list_len(L);
	PrintList(L);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值