双向循环链表
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; }
相关文章推荐
- android小问题:代码中设置Button被选中
- Angular 学习笔记——filter
- linux下字符串处理工具二:awk(1)
- 项目管理之起点
- 数据抽象与封装的好处--【primer第四版】
- 从尾到头打印链表
- 函数式编程中的常用技巧
- 七牛是如何搞定每天500亿条日志的
- win10 80070002
- C/C++学习(二)输入n个整数,输出其中最小的k个。
- jquery图片时钟
- bzoj1095[ZJOI2007]Hide 捉迷藏
- 颜色模型的应用——利用HSI模型特性消除红眼
- genymotion模拟器链接本地tomcat服务器
- 合并两个有序数组(C++版)
- UML学习:机房收费系统-类图
- 关闭CentOS的错误提示音
- System.IO.Path类
- 转: 相对路径和绝对路径的区别
- LR VS D-Tree VS SVM