您的位置:首页 > 理论基础 > 数据结构算法

实战数据结构(5)_双向循环链表的基本操作

2013-08-25 14:59 836 查看
/************************************************************************/
/* @author lynnbest
双向循环列表的使用:
1.创建
2.插入
3.删除
4.打印
5.按位置查找
6.按内容查找
7.退出                                                                */
/************************************************************************/
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *prior;
struct node *next;
}Dlistnode;
Dlistnode *CreateDClist(int n);
void printflist(Dlistnode *head);
int lengthlist(Dlistnode *head);
void InsertDClist(Dlistnode *head,int pos);
void DeleteDClist(Dlistnode *head,int pos);
void main()
{
printf("   双向循环链表基本操作	\n");
printf("----by lynnbest ----\n\n");
int  choice,num;
Dlistnode *head;
while(1)
{
printf("1---创建一个双向循环链表\n");
printf("2---插入节点操作\n");
printf("3---删除节点操作\n");
printf("4---打印链表元素\n");
printf("5---查找操作(按位置)\n");
printf("6---查找操作(按内容)\n");
printf("7---退出\n请输入操作:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("请输入创建元素的个数:\n");
scanf("%d",&num);
head=CreateDClist(num);
break;
case 2:
printf("输入插入的位置:\n");
scanf("%d",&num);
InsertDClist(head,num);
break;
case 3:
printf("输入删除的位置:\n");
scanf("%d",&num);
DeleteDClist(head,num);
break;
case 4:
printflist(head);
//	printf("共有%d个元素\n",lengthlist(head));
break;
case 5:
break;
case 6:
break;
case 7:
return ;
break;
default :
break;
}

}

}
Dlistnode *CreateDClist(int n) //创建一个带头节点的双向循环链表
{
Dlistnode *head,*newnode,*pre;
int data;
if(NULL==(head=(Dlistnode *)malloc(sizeof(Dlistnode))))
{
printf("头结点创建失败\n");
exit(-1);
}
pre=head;
for(int i=1;i<=n;i++)
{
if(NULL==(newnode=(Dlistnode *)malloc(sizeof(Dlistnode))))
{
printf("创建失败\n");
exit(-1);
}
printf("请输入第%d个元素\n",i);
scanf("%d",&data);
newnode->data=data;
//开始插入
pre->next=newnode;
newnode->prior=pre;
pre=newnode;
newnode->next=NULL;
}
newnode->next=head;
head->prior=newnode;	//做首位循环
return head;
}
void printflist(Dlistnode *head)
{
Dlistnode *cur=head->next;
while(cur->next!=head)
{
printf("%3d",cur->data);
cur=cur->next;
}
printf("%3d\n",cur->data);
return ;
}
int lengthlist(Dlistnode *head)
{
Dlistnode *cur=head;
int count=0;
while(cur->next!=head)
{
count++;
cur=cur->next;
}
return count;
}
void InsertDClist(Dlistnode *head,int pos)
{
if(pos<1||pos>lengthlist(head)+1){
printf("插入位置非法\n");
return ;
}
Dlistnode *cur=head,*newnode;
int data;
//完成插入点数据建立
if(NULL==(newnode=(Dlistnode *)malloc(sizeof(Dlistnode))))
{
printf("创建失败\n");
exit(-1);
}
printf("请输入要插入的节点数据:\n");
scanf("%d",&data);
newnode->data=data;
for(int i=0;i<pos;i++)	//查找要插入的位置
cur=cur->next;
//开始插入
cur->prior->next=newnode;	//插入需要4步
newnode->prior=cur->prior;
newnode->next=cur;
cur->prior=newnode;
}
void DeleteDClist(Dlistnode *head,int pos)
{
if(pos<1||pos>lengthlist(head)){
printf("删除位置非法\n");
return ;
}
Dlistnode *cur=head;
for(int i=0;i<pos;i++)	//查找要删除节点的位置
cur=cur->next;
//删除 需要3步
cur->prior->next=cur->next;	//前一个指针指向删除节点的下一个
cur->next->prior=cur->prior;//删除节点的前驱指针 指向删除节点前一个节点
free(cur);//释放堆
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: