双循环链表的实现及其功能!!!

//头文件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] insert_val  [7] delete_val  *"<<endl;
		cout<<"* [8] sort        [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 6:
		   cout<<"请输入你要插入的值";
		   cin>>item;
		   insert_val(&mylist,item);
			break;
		case 7:
		   cout<<"请输入你要删除的值";
		   cin>>item;
		   delete_val(&mylist,item);
			break;
		case 8:
		   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;
		}
	}	

你可能感兴趣的:(双循环链表的实现及其功能!!!)