您的位置:首页 > 其它

双向循环链表

2015-11-23 21:17 363 查看
直接贴代码:

/**
带空表头结点的双向循环链表
by 左鸢
//请忽略我捉急的英文--
**/
#include<stdio.h>
#include<stdlib.h>

typedef int data_type;      /*双向链表结点数据域类型*/
typedef struct node{
data_type data;
struct node *llink;
struct node *rlink;
}Node;                     /*双向链表数据结点数据结构*/

Node* createNode()
{
Node* p;
if((p=(Node*)malloc(sizeof(Node)))==0)
{
printf("no space left!\n");
}
return p;
}

Node* CreateList(Node* head,int number)
{
int i;
head=createNode();
head->data=0;
head->rlink=head->llink=head;//构造空表头结点

Node *q,*p=head;//q作为遍历链表的结点,p用于记录链表位置
for(i=0;i<number;i++)
{
q=createNode();
printf("please input the %d data of the list:",i+1);
scanf("%d",&q->data);
//从左至右链接链表
p->rlink=q;
q->llink=p;

p=q;//p前驱一个结点指向q

//从右至左链接链表,将链表链接至头结点
q->rlink=head;
head->llink=q;
}
return head;
}

void destroyList(Node *head)
{
if(head!=0)
{
Node *q,*p=head->rlink; /* p指向第一个结点 */

while(p!=head) /* p没到表头 */
{
q=p->rlink;
free(p);
p=q;
}
free(head);
}
}

Node* insert_Node(Node *head,data_type data)
{
/*总是在表尾插入*/
Node *p;
p=createNode();
p->data=data;

//从左至右链接链表,head->llink是尾结点
head->llink->rlink=p;
p->rlink=head;
//从右至左链接链表,将链表链接至头结点
p->llink=head->llink;
head->llink=p;
return head;
}

//链表结点的查找,x为数据域
Node* search_Node(Node *head,data_type x)
{
Node* p;
int i=0;
data_type y;
for(p=head->rlink;p!=head;p=p->rlink)
{
y=p->data;
i++;
if(y==x)
{
printf("having found data!\nthe data is in the %d node\n",i);

return p;
}
}
printf("not find data!\n");
return head;
}

Node* delete_Node(Node *head,data_type x)
{
Node* p=search_Node(head,x);
//从左至右链接链表
if(p!=head)
{
p->llink->rlink=p->rlink;
p->rlink->llink=p->llink;
printf("delete successfully!\n");
}
return head;
}

void print(Node* head)
{
Node *n;
int i=0;
if(head==0)
{
return;
}
for(n=head->rlink;n!=head;n=n->rlink)
{
i++;
printf("the %d node's data is:%d \n",i,n->data);
}
printf("\n\n");
}

void menu()
{
system("cls");
printf("menu:\n\n");
printf("----\t1.create list\n");
printf("----\t2.insert node\n");
printf("----\t3.delete node\n");
printf("----\t4.find node\n");
printf("----\t5.print list\n");
printf("----\t0.exit\n");
printf("please input your choice:");
}

int main()
{
int number;
Node *head=0;
data_type data;

int op;
do
{
menu();
scanf("%d",&op);
switch(op)
{
case 1:
{
system("cls");
printf("please input number:\n");
scanf("%d",&number);
head=CreateList(head,number);
printf("create successfully\n");
system("pause");
break;
}
case 2:
{
system("cls");
printf("please input the data you want to insert:");
scanf("%d",&data);
insert_Node(head,data);
printf("insert successfully!\n");
system("pause");
break;
}
case 3:
{
system("cls");
printf("please input the data you want to delete:");
scanf("%d",&data);
Node *n;
head=delete_Node(head,data);
system("pause");
break;
}
case 4:
{
system("cls");
printf("please input the data you want to search:");
scanf("%d",&data);
search_Node(head,data);
system("pause");
break;
}
case 5:
{
system("cls");
print(head);
system("pause");
break;
}
defaul:break;
}
}while(op);

destroyList(head);//销毁双向循环链表
printf("destroy list successfully!\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: