数据结构之实用单链表(参考整理严蔚敏数据结构)

#include<iostream>
using namespace std;

typedef int ElemType;

typedef struct Node
{
	ElemType data;
	Node *pNext;
}Node,*Link;

typedef struct LinkList
{
	Link pHead;
	Link pTail;
	int Length;
}LinkList;

void MakeNode(Link &p, ElemType e)
{
	p = new Node;
	p->data = e;
	p->pNext = NULL;
}

void FreeNode(Link &p)
{
	delete p;
	p = NULL;
}

void InitList(LinkList &L)
{
	Link p;
	p = new Node;
	p->pNext = NULL;
	L.pHead = p;
	L.pTail = p;
	L.Length = 0;
}

void ClearList(LinkList &L)
{
	Link p, q;
	if (L.pHead != L.pTail)//or L.Length!=0
	{
		p = L.pHead->pNext;
		q = L.pHead->pNext;
		L.pHead->pNext = NULL;
		while (q!= L.pTail)
		{
			q = p->pNext;
			delete p;
			p = q;
		}
		delete p;//or delete q;
		p = q = NULL;
		L.pTail = L.pHead;
		L.Length = 0;
	}
}

void DestroyList(LinkList &L)
{
	ClearList(L);
	FreeNode(L.pHead);
	L.pTail = NULL;
	L.pHead = NULL;
	L.Length = 0;
}

void InsFront(LinkList &L, Link h, Link s)
{// h指向L的一个结点(包括头结点),插入结点成为h后第一个结点
	s->pNext = h->pNext;
	h->pNext = s;
	if (L.pTail == h) // h指向尾结点
		L.pTail = h->pNext; // 修改尾指针
	L.Length++;
}

bool RemoveFront(LinkList &L, Link h, Link &q)
{// h指向L的一个结点(包括头结点),移除h后面第一个结点并将该结点交给q处理
	q = h->pNext;
	if (q)// 以防止:链表为空或者h指向尾结点
	{
		h->pNext = q->pNext;
		if (!(h->pNext))//删除结点为尾结点
			L.pTail = h;
		L.Length--;
		return true;
	}
	else
		return false;
}

bool RemoveListTail(LinkList &L, Link &q)
{// 移除线性链表L中的尾结点并以q返回,改变链表L的尾指针指向新的尾结点
	Link p = L.pHead;
	if (L.Length==0)
	{
		q = NULL;
		return false;
	}
	else
	{
		while (p->pNext != L.pTail)
			p = p->pNext;//p指向倒数第二个结点
		q = L.pTail;
		p->pNext = NULL;
		L.pTail = p;
		L.Length--;
		return true;
	}
}

void ListTailAppend(LinkList &L, Link s)
{// 将指针s(s->data为第一个数据元素)所指(彼此以指针相链,以NULL结尾)的
	// 一串结点链接在线性链表L的最后一个结点之后,并改变链表L的尾指针指向新
	// 的尾结点
	int i = 1;
	L.pTail->pNext = s;
	while (s->pNext)
	{
		s = s->pNext;
		++i;
	}
	L.pTail = s;
	L.Length += i;
}

Link PriorPos(LinkList L, Link p)
{ // 已知p指向线性链表L中的一个结点,返回p所指结点的直接前驱的指针
	// 若无前驱,则返回NULL
	Link q;
	q = L.pHead->pNext;//指向第一个结点
	if (p==L.pHead)
		return NULL;
	else if (p==q)
		return L.pHead;
	else
	{
		while (q->pNext != p)// q不是p的直接前驱
			q = q->pNext;
		return q;
	}
}

bool InsBefore(LinkList &L, Link &p, Link s)
{// 已知p指向线性链表L中的一个结点(包括头结点),将s所指结点插入在p所指结点之前,
   // 并修改指针p指向新插入的结点
	Link q;
	q = PriorPos(L, p);
	if (!q)	
		return false;	
	else
	{				
		s->pNext = p;
		q->pNext = s;
		p = s;
		L.Length++;
		return true;
	}
}

void InsAfter(LinkList &L, Link &p, Link s)
{// 已知p指向线性链表L中的一个结点(包括头结点),将s所指结点插入在p所指结点之后,
   // 并修改指针p指向新插入的结点
	if (p == L.pTail)// 修改尾指针
		L.pTail = s;
	s->pNext = p->pNext;
	p->pNext = s;
	p = s;
	L.Length++;
}

int ListLength(LinkList L)
{
	return L.Length;
}

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

void SetCurElem(Link &p, ElemType e)
{
	p->data = e;
}

ElemType GetCurElem(Link p)
{
	return p->data;
}

Link GetHead(LinkList L)
{
	return L.pHead;
}

Link GetTail(LinkList L)
{
	return L.pTail;
}

Link NextPos(Link p)
{
	return p->pNext;
}

bool LocatePos(LinkList L, int pos, Link &p)
{//pos==0为头结点
	int i=0;
	if (pos<0 ||pos>L.Length)
		return false;
	else
	{
		p = L.pHead;
		while (i < pos)
		{
			p = p->pNext;
			++i;
		}
		return true;
	}
}

Link LocateElem(LinkList L, ElemType e)
{
	Link p = L.pHead;
	do
		p = p->pNext;
	while (p&&p->data != e);
	return p;
}

void ListPrint(LinkList L)
{
	Link p = L.pHead->pNext;
	int i = 1;
	while (i <= L.Length)
	{
		cout <<p->data<< " ";
		p = p->pNext;
		++i;
	}
	cout << endl;
}




int main(void)
{
	Link p, h;
	LinkList L;
	int i, j;
	InitList(L);
	for (i = 1; i <= 5; ++i)
	{
		MakeNode(p, i);
		InsFront(L, L.pTail, p);
	}
	ListPrint(L);
	for (i =1; i <= 6; ++i)
	{
		if (LocatePos(L, i, p))
			cout << "存在" << p->data << " ";
		else
			cout << "不存在!" << endl;
	}
	for (i = 0; i <= 6; ++i)
	{
		p = LocateElem(L, i);
		if (p)
			cout << "存在" << p->data << " ";
		else
			cout << "不存在!" <<" ";
	}
	cout << endl;
	for (i = 1; i <= 6; ++i)
	{
		RemoveFront(L, L.pHead, p);
		if (p)
			cout << GetCurElem(p) << " ";
		else
			cout << "The List is Empty!" << endl;
	}	
	cout << ListLength(L) << endl;
	if (ListEmpty(L))
		cout << "The List is Empty!" << endl;
	MakeNode(p, 14);
	for (i = 6; i >= 1; --i)
	{//逆序
		MakeNode(h, 2 * i);
		h ->pNext = p;
		p = h;
	}
	ListTailAppend(L, h);
	ListPrint(L);
	for(i = 1; i <= 6; ++i)
	{
		LocatePos(L, i, p);
		h = PriorPos(L, p);
		if (h == L.pHead)
			cout << p->data << "头结点" << endl;
		else if (!h)
			cout << p->data << "无前驱!" << endl;			
		else
			cout << p->data << " " << h->data << endl;
	}
	cout << endl;
	j = ListLength(L);
	for (i = j; i>=0; --i)
	{
		LocatePos(L, i, p);
		h = NextPos(p);
		if (h)
		{
			if (p==L.pHead)
				cout<<"头结点 "<<h->data<<endl;
			else
				cout << p->data << " " << h->data << endl;
		}
		else
			cout << p->data << " 无后继" << endl;
	}
	cout << ListLength(L) << endl;
	if (!ListEmpty(L))
		cout << "Not Empty!" << endl;
	p = GetTail(L);
	SetCurElem(p, 18);
	cout <<GetCurElem(GetHead(L)->pNext)<< " " << GetCurElem(p)<<endl;
	MakeNode(h, 16);
	InsBefore(L, p, h);
	p = p->pNext;// p恢复为尾结点
	MakeNode(h, 20);
	InsAfter(L, p, h);
	ListPrint(L);
	j = ListLength(L);
	cout << j << endl;
	DestroyList(L);
	cout << L.pHead << endl;
	cout << L.pTail << endl;
	cout << L.Length << endl;
	return(0);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值