码迷,mamicode.com
首页 > 其他好文 > 详细

双循环链表(包含头指针与尾指针)

时间:2015-05-12 23:06:36      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:指针   链表   

双循环链表(包含头指针与尾指针)

以及基本功能的实现

list_ d_link_c.h

#ifndef _LIST_D_LINK_C_
#define _LIST_D_LINK_C_

#include<iostream>
#include<assert.h>
using namespace std;

#define ElemType int 

typedef struct Node
{
	ElemType data;
	struct Node *prio;
	struct Node *next;
}Node,*PNode;

typedef struct List
{
	PNode first;
	PNode last;
	size_t size;
}List;

void Initlist(List *list);
void push_back(List *list,ElemType x);
void push_front(List *list,ElemType x);
bool pop_back(List *list);
bool pop_front(List *list);
void show_seqlist(List *list);
bool delete_val(List  *list,ElemType key);
int length(List *list);
Node* find(List *list,ElemType key);
void clear(List *list);
void destroy(List *list);
bool next(List *list,ElemType key);
bool prio(List *list,ElemType key);
bool resver(List *list);
bool sort(List *list);//升序
bool modify(List *list,ElemType key);
bool insert_val(List *list,ElemType key);

#endif //_LIST_D_LINK_C_

list_ d_link_c.cpp

#include"Seqlist_ d_link_c.h"

void Initlist(List *list)
{
	Node *s = (Node *)malloc(sizeof(Node));
	assert(s != NULL);
	list->first = list->last = s;
	list->last->next = list->first;
	list->first->prio = list->last;
	list->size = 0;
}

void push_back(List *list,ElemType x)
{
	Node *p = (Node *)malloc(sizeof(Node));
	assert(p != NULL);
	p->data = x;

	p->next = list->first;
	list->first->prio = p;
	list->last->next = p;
	p->prio = list->last;

	list->last = p;
	list->size++;
}

void push_front(List *list,ElemType x)
{
	Node *p = (Node *)malloc(sizeof(Node));
	assert(p != NULL);
	p->data = x;

	if(list->size == 0)
	{
		list->last = p;
	}
	p->next = list->first->next;
	p->prio = list->first;
	list->first->next = p;
	list->size++;
}

bool pop_back(List *list)
{
	Node *p = list->last;
	
	if(list->size == 0)
	{
		cout<<"list is NULL!"<<endl;
		return false;
	}
	if(list->size == 1)
	{
		list->last = list->first;
	}
	p->prio->next = list->first;
	list->first->prio = p->prio;
	free(p);
	list->last = list->first->prio;
	list->size--;
	return true;
}

bool pop_front(List *list)
{
	Node *p = list->first->next;
	if(list->size ==0)
	{
		cout<<"the list is null!"<<endl;
		return false;
	}
	if(list->size == 1)
	{
		list->last = list->first;
		//list->last->next = list->first;
	}
	list->first->next = p->next;
	p->next->prio = list->first;
	free(p);
	list->size--;
	return true;
}

void show_seqlist(List *list)
{
	Node *p = list->first->next;
	while(p!= list->first)
	{	
		cout<<p->data<<"-->";
		p = p->next;
	}	
	cout<<"Nul"<<endl;
}

int length(List *list)
{
	return list->size;
}

Node* find(List *list,ElemType key)
{
	Node *p = list->first->next;
	while(p != list->first && p->data != key)
		p = p->next;
	if(p == list->first)
		return NULL;
	return p;
}

void clear(List *list)
{
	Node *p = list->first->next;
	while(p != list->first)
	{
		list->first->next = p->next;
		free(p);
		p = list->first->next;
	}
	list->last = list->first;
	list->size = 0;
}

void destroy(List *list)
{
	clear(list);
	free(list->first);
	list->first = list->last = NULL;
}

bool next(List *list,ElemType key)
{
	Node *p = find(list,key);
	if(p == NULL)
		return false;
	else if(p == list->last)
		cout<<key<<"'s next is "<<list->first->next->data<<endl;
	else
		cout<<key<<"'s next is "<<p->next->data<<endl;
	return true;
}

bool prio(List *list,ElemType key)
{
	Node *p = find(list,key);
	if(p == NULL)
		return false;
	else if(p == list->first->next)
		cout<<key<<"'s prio is "<<list->last->data<<endl;
	else
		cout<<key<<"'s prio is "<<p->prio->data<<endl;
	return true;
}

bool delete_val(List *list,ElemType key)
{
	Node *p = find(list,key);
	if(p == NULL)
		return false;
	
	p->prio->next = p->next;
	p->next->prio = p->prio;
	if(p == list->last)
		list->last = p->prio;
	free(p);
	list->size--;
	return true;
}

bool resver(List *list)
{
	Node *p = list->first->next;
	Node *q = p->next;

	p->next = list->first;
	list->first->prio = p;
	list->last = p;

	if(list->size <= 1)
		return false;
	while(q != list->first)
	{
		p = q;
		q = q->next;

		p->next = list->first->next;
		p->prio = list->first;
		list->first->next = p;
		p->next->prio = p;
	}
	return true;
}

bool sort(List *list)//升序
{
	Node *p = list->first->next;
	Node *q = p->next;
	
	p->next = list->first;
	list->first->prio = p;
	list->last = p;
	
	if(list->size <= 1)
		return false;
	list->size = 1;//设定初始长度为1
	while(q != list->first)
	{
		p = q;
		q = q->next;
		insert_val(list,p->data);
		free(p);
	}
	return true;
}

bool modify(List  *list,ElemType key)
{
	Node *p = find(list,key);
	if(p == NULL)
		return false;
	cout<<"please input a new item:";  
    cin>>key; 
	p->data = key;
	return true;
}

bool insert_val(List  *list,ElemType key)
{
	Node *p = find(list,key);
	if(p != NULL)
		return false;
	Node *q = (Node *)malloc(sizeof(Node));
	q->data = key;
	p = list->first;
	while(p != list->last)  
    {  
        if(p->next->data > key)  
            break;
        else  
            p = p->next;  
    }
	q->next = p->next;
	p->next->prio = q;
	q->prio = p;
	p->next = q;
	if(p == list->last)
		list->last = q;
	list->size++;
	return true;
}

main.cpp(测试)

#include"Seqlist_ d_link_c.h"
void main (void)
{
	List mylist;
	Initlist(&mylist);
		int select = 1;
	int item;
	while(select)
	{
		cout<<"***********************************"<<endl;
		cout<<"* [1] push_back   [2] push_front  *"<<endl;
		cout<<"* [3] show_seqlist[0] quit_system *"<<endl;
		cout<<"* [4] pop_back    [5] pop_front   *"<<endl;
		cout<<"* [6] delete_val  [7] insert_val  *"<<endl;
		cout<<"* [08] modify     [09]clear       *"<<endl;
		cout<<"* [10] destroy    [11]sort        *"<<endl;
		cout<<"* [12] resver     [13]length      *"<<endl;
		cout<<"* [14] next       [15]prio        *"<<endl;
		cout<<"***********************************"<<endl;
		cout<<"请选择:";
		cin>>select;
		switch(select)
		{
		case 1:
			cout<<"请输入要插入的数据(-1结束):>";
			while(cin>>item,item!=-1)
			{
				push_back(&mylist,item);
			}
			break;
		case 2:
			cout<<"请输入要插入的数据(-1结束):>";
			while(cin>>item,item!=-1)
			{
				push_front(&mylist,item);
			}
			break;
		case 3:
			show_seqlist(&mylist);
			break;
		case 4:
			pop_back(&mylist);
			break;
		case 5:
			pop_front(&mylist);
			break;
		case 6:
			cout<<"please input a value:";
			cin>>item;
			delete_val(&mylist,item);
			break;
		case 7:
			cout<<"please input a value:";
			cin>>item;
			insert_val(&mylist,item);
			break;
		case 8:
			cout<<"please input a value:";
			cin>>item;
			modify(&mylist,item);
			break;
		case 9:
			clear(&mylist);
			break;
		case 10:
			destroy(&mylist);
			select = 0;
			break;
		case 11:
			sort(&mylist);
			break;
		case 12:
			resver(&mylist);
			break;
		case 13:
			cout<<"length is "<<length(&mylist)<<endl;
			break;
		case 14:
			cout<<"please input a value:";
			cin>>item;
			next(&mylist,item);
			break;
		case 15:
			cout<<"please input a value:";
			cin>>item;
			prio(&mylist,item);
			break;
		default:
			select = 0;
			break;
		}
	}
}

技术分享

双循环链表(包含头指针与尾指针)

标签:指针   链表   

原文地址:http://blog.csdn.net/chenmengmengx/article/details/45674765

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!