双向链表的基本操作
2016-03-29 10:56
218 查看
#include <stdio.h> #include <stdlib.h> struct DLinklist{ char data; struct DLinklist *prior,*next; }; DLinklist * createDL(){ DLinklist * head,*p; char x; head = (DLinklist *)malloc(sizeof(DLinklist)); head->next = head; head->prior = head; printf("please input the values!\n"); scanf("%c",&x); while(x != '\n'){ p = (DLinklist *)malloc(sizeof(DLinklist)); p->data = x; p->next = head->next; p->prior = head; head->next->prior = p; head->next = p; scanf("%c",&x); } return head; } DLinklist * getLocation(DLinklist * head,int i){ DLinklist * p = head; int j=0; while(p->next != head && j<i){ j++; p = p->next; } if(p->next != head){ return p; }else{ return NULL; } } void insertDL(DLinklist * head,int i,char x){ DLinklist * p,*q; p = getLocation(head,i-1); if(p == NULL){ printf("the location doesn't exist!\n"); }else{ q=(DLinklist *)malloc(sizeof(DLinklist)); q->data = x; q->next = p->next; q->prior = p; p->next->prior = q; p->next = q; } } void delDL(DLinklist * head,int i){ DLinklist * p; p = getLocation(head,i); if(p == NULL){ printf("the location doesn't exist!\n"); }else{ p->prior->next = p->next; p->next->prior = p->prior; free(p); } } void printDL(DLinklist * head){ DLinklist * p; p = head->next; while(p != head){ printf("%c",p->data); p = p->next; } } int main(){ DLinklist * head,*p; head = createDL(); printDL(head); printf("\n"); p = getLocation(head,3); if(p == NULL){ printf("the location doesn't exist!\n"); }else{ printf("%c",p->data); } printf("\n"); printf("after insert:\n"); insertDL(head,5,'a'); printDL(head); printf("\n\n"); printf("after delete:\n"); delDL(head,4); printDL(head); printf("\n"); return 0; }
相关文章推荐
- 上班族健康 吐气如兰
- ZOJ 2797 106 miles to Chicago(多校连萌,用最短路径求概率问题)
- socket、tcp、udp、http 的认识及区别
- VC++的Unicode编程
- ORM框架分析之DB到Entity的映射(二)
- 使用融云SDK开发即时聊天(三)----启用客服
- C#知识点总结
- opencv使用convexityDefects计算轮廓凸缺陷
- Navicat 同步数据结构,对比数据资源
- MFC radio button 的用法
- 关于c语言#include的用法
- Android开发库VUtils之关联代码行的log
- Android 打造形形色色的进度条 实现可以如此简单
- Treemap的基本用法
- Android——Scroll分析
- iOS完整App资源收集
- jQuery.dialog
- XML序列化中含有List的情况,序列化到根节点下一层
- Android显示等宽图片的问题
- css CENTER