• 双向循环链表 1：双向循环链表创建 2：双向循环链表长度 3：双向循环链表正向遍历 4：双向循环链表反向遍历 5：双向循环链表节点插入 6：双向循环链表节点删除 7：双向循环链表倒序 8：双向循环链表判空 9...
/*
双向循环链表
1：双向循环链表创建
2：双向循环链表长度
3：双向循环链表正向遍历
4：双向循环链表反向遍历
5：双向循环链表节点插入
6：双向循环链表节点删除
7：双向循环链表倒序
8：双向循环链表判空
9：双向循环链表排序
10：双向循环链表销毁
*/

#include <iostream>
using namespace std;
typedef struct node{
struct node *pFront;
int value;
struct node *pNext;
}DCList;
/*创建双向循环链表*/
void Create(DCList* &Head,DCList* &End,int &nodecount )
{
if(nodecount <=0)  return ;
DCList *node = NULL;
int nodevalue = 0;
for(int i=0;i<nodecount;i++){
cin>>nodevalue;
node = new DCList;
node->value = nodevalue;
node->pFront = node;
node->pNext = node;
}
else{
End->pNext = node;
node->pNext->pFront = node;
node->pFront = End;
}
End = node;
}
}
/*获取双向链表的长度*/
{
length = 1;
while(ptr != End){
length++;
ptr = ptr->pNext;
}
}
/*双向链表正向遍历*/
{
if(Head == NULL || End == NULL)  return ;
while(ptr != End){
cout<<ptr->value<<" ";
ptr= ptr->pNext;
}
cout<<End->value<<endl;
}
/*双线循环链表的反向遍历*/
if(Head == NULL || End == NULL)  return ;
DCList * ptr= End;
cout<<ptr->value<<" ";
ptr=ptr->pFront;
}
}
/*双向循环链表节点插入*/
void Insert(DCList* &Head,DCList* &End,int &InsertPos,int InsertValue)
{
if((Head == NULL || End == NULL) && InsertPos != 1)  return ;
int length =0 ;
if(InsertPos <=0 && InsertPos > length+1)  return ;
DCList *insertnode = NULL;
insertnode = new DCList;
insertnode->value = InsertValue;
int insertpos =1;
DCList* insertfront = End;
while(insertpos != InsertPos){
insertfront = insertfront->pNext;
insertnext =  insertfront->pNext;
insertpos++;
}
insertnode->pNext = insertnext;
insertnode->pNext->pFront = insertnode;
insertfront->pNext = insertnode;
insertfront->pNext->pFront = insertfront;
if(InsertPos == 1){
}
else if(InsertPos == length+1){
End = insertnode;
}
}
/*双向循环链表节点删除*/
{
if(Head == NULL || End == NULL)  return ;
int length =0;
if(DelPos <= 0 || DelPos > length) return ;
DCList * delfront = End;
int delpos = 1;
while(delpos != DelPos){
delfront = delfront->pNext;
delnext = delnext->pNext;
delpos++;
}
DCList *ptr = delfront->pNext;
delfront->pNext = delnext;
delnext->pFront = delfront;
delete ptr ;
ptr = NULL;
if(DelPos == 1){
}
else if(DelPos == length){
End = delfront;
}
}
/*双向循环链表倒序*/
{
if(Head == NULL || End == NULL)  return ;
DCList* End_New = NULL;
DCList *ptr = End;
DCList *ptr_front = NULL;
ptr_front = ptr->pFront ;
ptr->pFront = ptr;
ptr->pNext = ptr;
}
else{
End_New->pNext = ptr;
ptr->pNext->pFront = ptr;
ptr->pFront = End_New;
}
End_New = ptr;
ptr = ptr_front;
}
End = End_New;
}
/*双向循环链表排序*/
{
if(Head == NULL || End == NULL)  return ;
DCList *node = NULL;
DCList* node_next = NULL;
DCList* node_front = NULL;
DCList* tmp = NULL;
int length = 0;
int nflag =0;
for(int i=0;i<length;i++){
nflag = 0;
node_next = node->pNext;
for(int j=0;j<length-i-1;j++){
if(node->value > node_next->value){
node->pNext = node_next->pNext;
node->pNext->pFront = node;
node_next->pNext = node;
node_next->pNext->pFront = node_next;
node_front->pNext = node_next;
node_next->pFront = node_front;
}
node_next->pFront = NULL;
}
if(node_next == End){
End = node;
}
tmp = node;
node = node_next;
node_next = tmp;
nflag = j+1;
}
node_front = node;
node = node->pNext;
node_next = node->pNext;
}
if(nflag == 0){
break;
}
i = length-nflag-1;
}
}
/*双向链表销毁*/
if(Head == NULL || End == NULL)        return ;
DCList* ptr_next = NULL;
while(ptr != End){
ptr_next = ptr->pNext;
delete ptr ;
ptr = NULL;
ptr = ptr_next;
}
delete End ;
End = NULL;
}
/*双向链表判空*/
{
if(Head == NULL || End == NULL)
return true;
else
return false;
}
int main(){
int length,nodecount,insertpos,insertvalue,delpos;
DCList *Head = NULL ,*End = NULL;
/*创建双循环链表*/
cin>> nodecount;
cout<<length<<endl;
/*双向循环链表节点插入*/
cin>> insertpos >> insertvalue;
cout<<length<<endl;
/*双向循环链表节点删除*/
cin>> delpos ;
cout<<length<<endl;
/*双向链表倒序*/
cout<<length<<endl;
/*双向循环链表排序*/
cout<<length<<endl;
/*双向循环链表判空*/
if(m_empty ){
cout<<"is_empty"<<endl;
}
else{
cout<<"is_not_empty"<<endl;
}
if(m_empty ){
cout<<"is_empty"<<endl;
}
else{
cout<<"is_not_empty"<<endl;
}
system("pause");
return 0;
}

展开全文

...