[数据结构-严蔚敏版]P37定义一个带头结点的线性链表

Posted Wecccccccc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[数据结构-严蔚敏版]P37定义一个带头结点的线性链表相关的知识,希望对你有一定的参考价值。

大家如果发现代码有错误,麻烦评论告知一下!!!

代码如下:

#include <iostream>
using namespace std;

typedef struct LNode
{
	int data;
	LNode *next;
}*Link,*Position;

typedef struct
{
	Link head, tail;
	int len;
}LinkList;

bool makeNode(Link &p, int e)
{
	p = new LNode;
	if (!p) return false;
	p->data = e;
	p->next = nullptr;
	return true;
}

void freeNode(Link &p)
{
	delete p;
	p = nullptr;
}

bool initList(LinkList &L)
{
	L.head = new LNode;
	if (!L.head) return false;
	L.tail = L.head;
	L.head->next = nullptr;
	L.len = 0;
	return true;
}

void destroyList(LinkList &L)
{
	LNode *p = L.head;
	while (p)
	{
		LNode *q = p;
		p = p->next;
		delete q;
	}
	L.tail = L.head = nullptr;
}

void clearList(LinkList &L)
{
	LNode *p = L.head->next;
	while (p)
	{
		LNode *q = p;
		p = p->next;
		delete q;
	}
	L.tail = L.head;
	L.len = 0;
	L.head->next = nullptr;
}

bool insFirst(Link h, Link s)
{
	s->next = h->next;
	h->next = s;
	return true;
}

bool delFirst(Link h, Link &q)
{
	q = h->next;
	h->next = h->next->next;
	q->next = nullptr;
	return true;
}

bool append(LinkList &L, Link s)
{
	L.tail->next = s;
	while (L.tail->next)
	{
		L.tail = L.tail->next;
		L.len++;
	}
	return true;
}

bool remove(LinkList &L, Link &q)
{
	LNode *s = L.head;
	while (s->next != L.tail)
	{
		s = s->next;
	}
	q = L.tail;
	L.tail = s;
	s->next = nullptr;
	L.len--;
	return true;
}

bool insBefore(LinkList &L, Link &p, Link s)
{
	LNode *q = L.head;
	while (q->next != p)
	{
		q = q->next;
	}
	s->next = p;
	q->next = s;
	L.len++;
	return true;
}

bool insAfter(LinkList &L, Link &p, Link s)
{
	s->next = p->next;
	p->next = s;
	p = s;
	L.len++;
	return true;
}

bool setCurElem(Link &p, int e)
{
	p->data = e;
	return true;
}

int getCurElem(Link p)
{
	return p->data;
}

bool listEmpty(LinkList L)
{
	if (L.len == 0) return true;
	else return false;
}

int listLength(LinkList L)
{
	return L.len;
}

Position getHead(LinkList L)
{
	return L.head;
}

Position getLast(LinkList L)
{
	return L.tail;
}

Position priorPos(LinkList L, Link p)
{
	LNode *s = L.head;
	while (s->next != p)
	{
		s = s->next;
	}
	if (s == L.head)
	{
		return nullptr;
	}
	return s;
}

Position nextPos(LinkList L, Link p)
{
	return p->next;
}

bool locatePos(LinkList L, int i, Link &p)
{
	int j = 1;
	LNode *s = L.head->next;
	if (i < 1 || i > L.len) return false;
	while (s && j < i)
	{
		s = s->next;
		j++;
	}
	if (!s || j > i)
	{
		return false;
	}
	p = s;
	return true;
}

Position locateElem(LinkList L, int e, bool(*compare)(int, int))
{
	LNode *s = L.head->next;
	while (s)
	{
		if (compare(s->data, e))
		{
			return s;
		}
	}
	return nullptr;
}


void listTraverse(LinkList L, void(*visit)(Link))
{
	LNode *s = L.head->next;
	while (s)
	{
		visit(s);
		s = s->next;
	}
}

LNode *h = nullptr;
LNode *s = nullptr;

bool listInsert_L(LinkList &L, int i, int e)
{
	if (!locatePos(L, i - 1, h)) return false;
	if (!makeNode(s, e)) return false;
	insFirst(h, s);
	return true;
}

void createList(LinkList &L, int n)
{
	int a;
	LNode *s;
	for (int i = 1; i <= n; i++)
	{
		cin >> a;
		makeNode(s, a);
		insAfter(L, L.tail, s);
	}
}

void visit(LNode *s)
{
	cout << s->data << " ";
}


int compare(int a, int b)
{
	if (a <= b) return -1;
	else return 1;
}

bool mergeList_L(LinkList &La, LinkList &Lb, LinkList &Lc,int (*compare)(int ,int ))
{
	LNode *q = nullptr;
	if (!initList(Lc)) return false;
	LNode *ha = getHead(La);
	LNode *hb = getHead(Lb);
	LNode *pa = nextPos(La, ha);
	LNode *pb = nextPos(Lb, hb);
	while (pa && pb)
	{
		int a = getCurElem(pa);
		int b = getCurElem(pb);

		if (compare(a, b) <= 0)
		{
			delFirst(ha, q);
		
			append(Lc, q);
			pa = nextPos(La, ha);
		}
		else
		{
			delFirst(hb, q);
			append(Lc, q);
			pb = nextPos(Lb, hb);
		}
	}
	if (pa) append(Lc, pa);
	else append(Lc, pb);
	freeNode(ha);
	freeNode(hb);
	return true;
}

int main()
{
	LinkList L;
	initList(L);
	int n;
	cin >> n;
	createList(L, n);
	listTraverse(L,visit);
	cout << endl;
	cin >> n;
	LinkList L1;
	initList(L1);
	createList(L1, n);
	listTraverse(L1, visit);
	LinkList L2;
	mergeList_L(L, L1, L2,compare);
	listTraverse(L2,visit);
	return 0;
}

以上是关于[数据结构-严蔚敏版]P37定义一个带头结点的线性链表的主要内容,如果未能解决你的问题,请参考以下文章

考研笔记之数据结构之线性表(严蔚敏版)

考研笔记之数据结构之线性表(严蔚敏版)

线性表的顺序表示和实现(严蔚敏版)

数据结构(严蔚敏)2.4一元多项式

数据结构笔记(C语言版)严蔚敏

[数据结构-严蔚敏版]P71串的抽象数据类型的定义