#ifndef _CLIST_H
#define _CLIST_H
#include<iostream>
#include<assert.h>
using namespace std;
#define ElemType int
typedef struct Node
{
ElemType data;
struct Node *next;
}Node, *PNode;
typedef struct List
{
PNode first;
PNode last;
size_t size;
}List;
void InitList(List *list);//初始化
bool push_back(List *list, ElemType x);//尾插
bool push_front(List *list, ElemType x);//头插
bool pop_back(List *list);//尾删
bool pop_front(List *list);//头删
Node* Find(List *list, ElemType key);//按值找地址
Node* FindPrio(List *list, ElemType key);//按值找前驱地址
bool insert_val(List *list, ElemType key,ElemType x);//按值找位置并插入值
bool delete_val(List *list, ElemType key);//按值找值删除
bool modify(List *list, ElemType key,ElemType x);//按值找值修改
void ShowList(List *list);//打印
void clear(List *list);//清除
void destroy(List *list);//摧毁
void resver(List *list);//逆置(值不相同)
int length(List *list);//长度
ElemType Next(List *list , ElemType key);//后继的值
ElemType Prio(List *list , ElemType key);//前驱的值
void sort(List *list);//排序
#endif
#include "CList.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->size = 0;
}
bool push_back(List *list, ElemType x)
{
Node *s = (Node *)malloc(sizeof(Node));
if(s == NULL)
{
return false;
}
s->data = x;
s->next = list->first;
list->last ->next = 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 ;
list->first->next = s;
if(list->size == 0)
{
list->last = s;
}
list->size ++;
return true;
}
bool pop_back(List *list)
{
if(list->size == 0)
{
return false;
}
Node *pre = list->first ;
while(pre->next != list->last)
{
pre = pre->next ;
}
pre->next = list->first;
free(list->last);
list->last = pre;
list->size--;
return true;
}
bool pop_front(List *list)
{
if(list->size == 0)
{
return false;
}
Node *p = list->first->next ;
if(list->size == 1)
{
free(p);
list->first->next = NULL;
list->last = list->first ;
}
else
{
list->first->next = p->next ;
free(p);
}
list->size--;
return true;
}
Node* Find(List *list, ElemType key)
{
Node *pre = list->first;
Node *p = pre->next;
while(pre != list->last && p->data != key)
{
p = p->next;
pre = pre->next;
}
return p;
}
Node* FindPrio(List *list, ElemType key)
{
Node *pre = list->first;
Node *p = pre->next ;
while(pre != list->last && p ->data!=key)
{
p = p->next;
pre = pre->next;
}
return pre;
}
bool insert_val(List *list, ElemType key,ElemType x)
{
Node *pos = Find(list,key);
if(pos == NULL)
{
return false;
}
Node *s = (Node *)malloc(sizeof(Node));
s->data = x;
s->next = pos->next ;
pos->next = s;
return true;
}
bool delete_val(List *list, ElemType key)
{
Node *pos = FindPrio(list,key);
if(pos == NULL)
{
return false;
}
Node *s = pos->next;
pos->next = s->next;
free(s);
list->size--;
return true;
}
bool modify(List *list, ElemType key,ElemType x)
{
Node *pos = Find(list,key);
if(pos == NULL)
{
return false;
}
pos->data = x;
return true;
}
void ShowList(List *list)
{
Node *p = list->first ->next ;
while(p != list->last)
{
cout<<p->data <<"-->";
p = p->next ;
}
cout<<list->last->data<<"-->"<<"循环"<<endl;
}
void clear(List *list)
{
Node *p = list->first->next;
while(p != NULL)
{
list->first->next = p->next;
free(p);
p = list->first->next;
}
list->first = list->last ;
list->last->next = list->first;
list->size = 0;
}
void destroy(List *list)
{
clear(list);
free(list->first);
list->first = list->last = NULL;
}
void sort(List *list)//数值不重复
{
int count = list->size;
Node *pre = list->first->next;
Node *p = pre->next;
if(list->size == 0)
{
return;
}
if(list->size == 1)
{
cout<<"排序后为:"<<list->last->data<<endl;
}
while(count)
{
if(p->data >= pre->data )
{
p = p->next ;
pre = pre->next ;
}
else
{
ElemType item = p->data;
p->data = pre->data ;
pre->data = item;
}
count--;
}
}
void resver(List *list)//数值不重复
{
Node* s = (Node*)malloc(sizeof(Node));
if(list->size == 0)
{
return;
}
if(list->size == 1)
{
cout<<"逆置后值为:"<<list->last->data <<endl;
}
s->next = list->last ;
while(list->first->next != list->last)
{
list->last->next = FindPrio(list,list->last->data);
list->last = list->last->next;
}
list->last->next = list->first;
free(list->first);
list->first = s;
}
int length(List *list)
{
return list->size;
}
ElemType Prio(List *list , ElemType key)
{
Node* pos = FindPrio(list,key);
if(pos == NULL)
{
return -1;
}
return pos->data;
}
ElemType Next(List *list , ElemType key)
{
Node *pos = Find(list,key);
if(pos == NULL)
{
return -1;
}
return pos->next->data;
}
#include "CList.h"
void main()
{
List mylist;
InitList(&mylist);
int select = 1;
ElemType item;
ElemType pos;
Node *p = NULL;
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] findprio [9] modify *"<<endl;
cout<<"* [10] sort [11] clear *"<<endl;
cout<<"* [12] destroy [13] length *"<<endl;
cout<<"* [14] resver [15] next *"<<endl;
cout<<"* [16] prio [17] find *"<<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:
ShowList(&mylist);
break;
case 4:
pop_back(&mylist);
break;
case 5:
pop_front(&mylist);
break;
case 6:
cout<<"请输入在何值后插入值:>";
cin>>item;
cout<<"请输入要插入的值:>";
cin>>pos;
insert_val(&mylist,item,pos);
break;
case 7:
cout<<"请输入要删除的值:>";
cin>>item;
delete_val(&mylist,item);
break;
case 8:
cout<<"请输入要查找的值:>";
cin>>item;
p = FindPrio(&mylist,item);
cout<<"所在查找值的前驱地址为:"<<p<<endl;
break;
case 9:
cout<<"请输入要修改的值:>";
cin>>item;
cout<<"请输入改变后的值:>";
cin>>pos;
modify(&mylist,item,pos);
break;
case 10:
sort(&mylist);
break;
case 11:
clear(&mylist);
break;
case 12:
destroy(&mylist);
break;
case 13:
cout<<"单链表的长度为:"<<length(&mylist)<<endl;
break;
case 14:
resver(&mylist);
break;
case 15:
cout<<"请输入要查找的值:>";
cin>>item;
break;
case 16:
cout<<"请输入要查找的值:>";
cin>>item;
cout<<"所在查找值的前驱为:"<<Prio(&mylist,item)<<endl;
break;
case 17:
cout<<"请输入要查找的值:>";
cin>>item;
p = Find(&mylist,item);
cout<<"所在查找值的地址为:"<<p<<endl;
break;
default:
break;
}
}
}