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

//头文件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;
		}
	}
} 

时间: 2024-09-27 08:41:05

双链表的实现及其功能大全!!!的相关文章

优先双链表

题目: 设有一个双链表,每个结点中除有prior,data和 next这3个域外,还有一个访问频度域 freq,在链表被启用前其值均初始化为0.每当在在链表上进行一次查找操作Locate(L, x)时,令元素值为x的结点中的freq域的值增加1,并使此链表中的结点保持按访问频度域递减的顺序排列,以便使频繁访问的结点总是靠近表头 (1)首先创建一个双链表. (2) 设计一个符合上述要求的Locate(L, x)函数. (3) 具有输出显示访问频度功能. (4) 要求程序通过一个主菜单进行控制,在主

java数据结构与算法之改良顺序表与双链表类似ArrayList和LinkedList(带Iterator迭代器与fast-fail机制)

转载请注明出处(请尊重原创!谢谢~): http://blog.csdn.net/javazejian/article/details/53073995 出自[zejian的博客] 关联文章: java数据结构与算法之顺序表与链表设计与实现分析 java数据结构与算法之双链表设计与实现 java数据结构与算法之改良顺序表与双链表类似ArrayList和LinkedList(带Iterator迭代器与fast-fail机制) ??这篇是数据结构与算法的第3篇,通过前两篇的介绍,对应顺序表和链表已有

js基本功能大全

1.javascript的数组API: //定义数组 var pageIds = new Array(); pageIds.push('A'); 数组长度 pageIds.length; //shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift(); //a:[2,3,4,5] b:1 //unshift:将参数添加到原数组开头,并返回数组的长度 var a = [1,2,3,4,5];

单链表,双链表,循环链表的区别

单向链表(单链表) 单向链表,它包含两个域,一个信息域和一个指针域.这个链接指向表中的下一个节点,而最后一个节点则 指向一个空值NULL. 单向链表只可向一个方向遍历. 查找一个节点的时候需要从第一个节点开始每次访问下一个节点,一直访问到需要的位置.也可以提前把一个节点的位置另外保存起来,然后直接访问. 双向链表,(双链表) 双向链表中不仅有指向后一个节点的指针,还有指向前一个节点的指针.第一个节点的"前连接"指向NULL,最后一个节点的"后连接"指向NULL. 这

数据结构实践——循环双链表应用

本文针对数据结构基础系列网络课程(2):线性表的实践项目. [项目- 循环双链表应用] 设非空线性表ha和hb都用带头节点的循环双链表表示.设计一个算法Insert(ha,hb,i).其功能是:i=0时,将线性表hb插入到线性表ha的最前面:当i>0时,将线性表hb插入到线性表ha中第i个节点的后面:当i大于等于线性表ha的长度时,将线性表hb插入到线性表ha的最后面. 请在实现算法时,除项目中给出的特殊要求,其余工作均可利用项目4完成的算法支持. [参考解答](循环双链表的基本运算算法,请参考

【算法和数据结构】_13_小算法_双链表

没什么新的内容,把自己写的练习代码贴出来,供大家批判. 1 /* 2 本程序用来测试非线性存储结构:双链表 3 */ 4 5 6 #include <stdio.h> 7 #include <stdlib.h> 8 9 10 //**************************************************0 11 // 定义双链表数据结构 12 struct dbllink 13 { 14 char data; 15 struct dbllink* preN

js基本功能大全(函数封装)

js基本功能大全 1.javascript的数组API: //定义数组 var pageIds = new Array(); pageIds.push('A'); 数组长度 pageIds.length; //shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift(); //a:[2,3,4,5] b:1 //unshift:将参数添加到原数组开头,并返回数组的长度 var a = [1,2

[C++11][数据结构]自己的双链表实现

这个双链表,是我模仿stl的list制作的,只实现了一些基本功能,像merge,transfer这些就没有实现,用户可以用基本操作来自己做外部实现. 我没有选用stl的[begin,end)迭代器模式,而是使用传统的[head,tail].不过,为了配合stl算法,我还是加了两个begin(),end()方法,模拟了一下stl容器.模拟的还算及格,至少我可以做类似for (; begin != end; ++begin)这样的事,也可以让我的容器搭配一些stl算法,这在之后的demo里可以看到.

双链表的基本操作

1 #include <stdio.h> 2 #include <stdlib.h> 3 typedef struct line{ 4 struct line * prior; 5 int data; 6 struct line * next; 7 }line; 8 //双链表的创建 9 line* initLine(line * head); 10 //双链表插入元素,add表示插入位置 11 line * insertLine(line * head,int data,int