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

双链表的实现及其功能大全!!!

时间:2015-05-10 17:24:34      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

//头文件seqlist.h
#include<iostream>
 using namespace std;
#include<assert.h>
#define ElemType int
typedef struct Node
{
  ElemType data;
  struct Node * next;
  struct Node * prio;
}Node,*PNode;

typedef struct SeqList
{
      PNode	first;
	  PNode last;
      size_t size;
}List;
/////////////////////////函数的实现
void InitList(List *list)
{
   Node *s = (Node *)malloc(sizeof(Node));
   assert(s != NULL);

   list->first = list->last =s;
   list->first->next =s;
   list->first->prio =s;
   s->prio =list->first;
   s->next = list->first;
   
   list->size = 0;
}

bool push_back(List *list,ElemType x)
{
   Node *s = (Node *)malloc(sizeof(Node));
   if(s == NULL)
	   return false;
  
   s->data = x;
   list->last->next = s;
   s->prio = list->last;
   s->next = list->first;
   s->next->prio = s;
   list->last =s;
   
   list->size ++;
  return true;
}


bool push_front(List *list,ElemType x)
{
  Node *s = (Node *)malloc(sizeof(Node));
   if(s == NULL)
	   return false;
   s->data = x;
   s->next = list->first->next;
   s->next->prio = s;
   s->prio = list->first;
   list->first->next = s;
   
   if(list->size == 0)
   {
     list->last = s;
   }
   list->size++;
  return true;
}

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

bool pop_back(List *list)
{
   if(list->size == 0)
		return false;
   Node *p = list->last;
   
   list->last = p->prio;
   p->prio->next = list->first;
   p->next->prio = p->prio;
   free(p);
   list->size--;
  return true;
}


bool pop_front(List *list)
{
  if(list->size == 0)
	  return false;
  Node *p = list->first->next;
  p->prio->next = p->next;
  p->next->prio = p->prio;
  
  if(list->size == 1)
  {
    list->last = list->first;
  }
  list->size--;
  free(p);
 return true;
}

bool insert_val(List *list,ElemType key)
{
	if(list->size == 0 || key>list->last->data)
	{
	   push_back(list,key);
	   return true;
	}
    Node *s =(Node *)malloc(sizeof(Node));
	
	if(s == NULL)
	 return false;
    
	s->data = key;  

	Node *pre =list->first->next;
	while(key > pre->data)
	{
	    pre = pre->next;
	}
	 pre->prio->next = s;
	 s->prio = pre->prio;
	 s->next = pre;
	 pre->prio = s;
	 list->size++;
   return true;	 
}

bool delete_val(List * list,ElemType key)
{
   if(list->size == 0)
	   return false;
   Node *pre = list->first->next;
   
   while(key != pre->data)
   {
      pre = pre->next;
   }
    if(pre == list->first)
	{
	   cout<<"not exit"<<endl;
	   return false;
	}
   pre->prio->next = pre->next;
   pre->next->prio = pre->prio;
   if(pre == list->last)
	  list->last =pre->prio;
   free(pre);
   list->size--;
  return true;
}

bool sort(List *list)
{
	if(list->size == 0 || list->size ==1)
		return false;
   Node * pre =list->first->next;
   Node *pro = pre->next;
   
   pre->next =list->first;
   list->first->prio =pre;
   list->last = pre;
   
   while(pro != list->first)
   {
	 if(pro->data > list->last->data)
	 {
	    list->last->next =pro;
		pro->prio =list->last;
		list->last = pro;
		pro = pro->next;
	  continue;
	 }
      pre = list->first->next;

	while(pro->data > pre->data)	 
	  pre=pre->next;
     
    Node * ss =pro;
	 pro = pro->next;
	 pre->prio->next = ss;
	 ss->next = pre;
	 ss->prio =pre->prio;
	 ss->next =pre;
   }
   list->last->next = list->first;
   list->first->prio =list->last;
   return true;
}

bool find(List *list,ElemType key)
{
  if(list->size == 0)
	 return false;
   Node *pre= list->first->next;
  while(key!=pre->data )
  {
	 if(pre == NULL)
	 {
	   cout<<"not exit"<<endl;
	 }
   pre = pre->next;
  }
  cout<<"exit:"<<pre->data<<endl;
 return true;
}

bool modify(List *list,ElemType key,ElemType x)
{
  if(list->size == 0)
	 return false;
  Node *pre= list->first->next;
  while(key!=pre->data )
  {
    if(pre == NULL)
	{
	  cout<<"not exit"<<endl;
	}
    pre = pre->next;
  }
  cout<<"修改前:";
  show_seqlist(list);
  pre->data = x;
  cout<<"修改后:";
  show_seqlist(list);
   return true;
}

bool clear(List *list)
{
 if(list->size == 0)
   return false;
 Node *pre =list->first->next;
 while(pre != list->first)
 {
   pre->data = 0;
   pre=pre->next;
 }
 return true;
}

void destroy(List *list)
{
  if(list->size == 0)
	return ;
  Node *pre =list->first->next;
  
  while(pre != list->first)
  {
   Node *pro = pre;
   pre = pre->next;
   free(pro);
  }
  list->last = list->first;
  list->last->next = list->first;
  list->last->prio=list->first;
  list->first->prio =list->first;
  list->first->next =list->first;
}

void length(List * list)
{
  cout<<list->size<<endl;
}

void next(List *list,ElemType key)
{
  if(list->size == 0)
	   return ;
   Node *pre= list->first->next;
  while(key!=pre->data )
  {
	 if(pre == NULL)
	 {
		cout<<"not exit"<<endl;
	 }
    pre = pre->next;
  }
  cout<<"exit:"<<pre->next->data<<endl;
}

void prio(List *list,ElemType key)
{
  if(list->size == 0)
	   return ;
   Node *pre= list->first->next;
  while(key!=pre->data )
  {
	if(pre == NULL)
	{
		cout<<"not exit"<<endl;
	}
    pre = pre->next;
  }
  cout<<"exit:"<<pre->prio->data<<endl;
}
#include"seqlist.h"

void main()
{
    int select;
	int pos = 0;
	List mylist;
	ElemType x;
	ElemType item;
	Node *p =NULL;
	
	InitList(&mylist);
 
 while(select)
	{
		cout<<"**********************************"<<endl;
		cout<<"* [0] quit_system [1] push_back   *"<<endl;
		cout<<"* [2] push_front  [3] show_seqlist*"<<endl;
		cout<<"* [4] pop_back    [5] pop_front   *"<<endl;
		cout<<"* [6] sort        [7]insert_val   *"<<endl;
		cout<<"* [8] delete_val  [9] find        *"<<endl;
		cout<<"* [10]modify      [11]clear       *"<<endl;
		cout<<"* [12]destroy     [13]length      *"<<endl;
		cout<<"* [14]next        [15]prio        *"<<endl;
		cout<<"***********************************"<<endl;
		cout<<"请选择:>";
		cin>>select;
		switch(select)
		{
		case 1:
			 cout<<"请输入要插入的值(以-1结束):"<<endl;
			 while(cin>>item,item != -1)
			 push_back(&mylist,item);
			break;
		case 2:
			cout<<"请输入要插入的值(以-1结束):"<<endl;
			 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 7:
		   cout<<"请输入你要插入的值";
		   cin>>item;
		   insert_val(&mylist,item);
			break;
		case 8:
		   cout<<"请输入你要删除的值";
		   cin>>item;
		   delete_val(&mylist,item);
			break;
		case 6:
		   sort(&mylist);
			break;
		case 9:
			cout<<"请输入你要查找的值";
			cin>>item;
			find(&mylist,item);
			break;
		case 10:
			cout<<"请分别输入要修改的值和修改后的值:";
			cin>>item>>x;
			modify(&mylist,item,x);
			break;
		case 11:
			clear(&mylist);
			break;
		case 12:
             destroy(&mylist);
			break;
		case 13:
		    length(&mylist);
			break;
		case 14:
			cout<<"请输入你要求的后继的数:";
			cin>>item;
		     next(&mylist,item);
			break;
		case 15:
				cout<<"请输入你要求的前驱的数:";
			cin>>item;
		     prio(&mylist,item);
			break;
		default:
			break;
		}
	}	
} 
技术分享技术分享技术分享

双链表的实现及其功能大全!!!

标签:

原文地址:http://blog.csdn.net/zr1076311296/article/details/45621567

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