C双向链表
来源:互联网 发布:tt域名cc域名 编辑:程序博客网 时间:2024/05/16 05:42
写了一个很简单的双向链表
#include <stdio.h>#include <STDLIB.H>#include <MALLOC.H>//一个简单链表,只有一条链typedef struct FUCK{int data;struct FUCK * pre;struct FUCK * next;}Node;Node *head=NULL;void initList(){head=(Node*)malloc(sizeof(Node));head->next=NULL;head->pre=NULL;}Node* find(int a){//找到插入点,在插入点的后面插入a元素Node *point=NULL;point=head->next;while(1){if (a<=point->data){return point->pre;}if (point->next==NULL) break;point=point->next;}return point;}void insertE(int a){Node *p=NULL;p=(Node *)malloc(sizeof(Node));//写入新节点内容p->data=a;p->next=NULL;p->pre=NULL;if(head->next==NULL){//证明链表现在是空的head->next=p;p->pre=head;}else{//链表不空,找到插入点Node* insPoint=find(a);Node* insPNext=insPoint->next; //记录原插入点的后一个节点//处理前一个点insPoint->next=p;p->pre=insPoint;//处理后一个点,情况稍微复杂一点,要考虑null的情况if(insPNext!=NULL){insPNext->pre=p;p->next=insPNext;}else{p->next=NULL; //这句可以不写,因为前面有初始化,但是为了清楚还是写上}}//至此已成功插入}void traversal(){Node *point;point=head->next;while(point!=NULL){printf("%d ",point->data);point=point->next;}printf("\n");}int main(){initList();insertE(1);insertE(2);insertE(15);insertE(9);//遍历链表traversal();return 0;}
输出结果:1 2 9 15
#include <stdio.h>#include <STDLIB.H>#include <MALLOC.H>//一个简单链表,只有一条链typedef struct FUCK{int data;struct FUCK * pre;struct FUCK * next;}Node;Node *head=NULL;void initList(){head=(Node*)malloc(sizeof(Node));head->next=NULL;head->pre=NULL;}Node* find(int a){//找到插入点,在插入点的后面插入a元素Node *point=NULL;point=head->next;while(1){if (a<=point->data){return point->pre;}if (point->next==NULL) break;point=point->next;}return point;}void insertE(int a){Node *p=NULL;p=(Node *)malloc(sizeof(Node));//写入新节点内容p->data=a;p->next=NULL;p->pre=NULL;if(head->next==NULL){//证明链表现在是空的head->next=p;p->pre=head;}else{//链表不空,找到插入点Node* insPoint=find(a);Node* insPNext=insPoint->next; //记录原插入点的后一个节点//处理前一个点insPoint->next=p;p->pre=insPoint;//处理后一个点,情况稍微复杂一点,要考虑null的情况if(insPNext!=NULL){insPNext->pre=p;p->next=insPNext;}else{p->next=NULL; //这句可以不写,因为前面有初始化,但是为了清楚还是写上}}//至此已成功插入}void traversal(){Node *point;point=head->next;while(point!=NULL){printf("%d ",point->data);point=point->next;}printf("\n");}int main(){initList();int a;while(1){printf("please enter an insert number:\n");scanf("%d",&a);insertE(a);printf("now the numbers in the list are:\n");//遍历链表traversal();getchar();getchar();system("CLS");}return 0;}
界面变成了:
0 0
- 双向链表c
- C双向链表
- 【C++】双向链表
- 【C++】双向链表
- C 双向链表
- 双向链表C语言
- 数据结构(C#)--双向链表
- List-c双向链表
- [C++]双向链表操作
- 双向链表(C++)
- 双向链表-C语言版
- 双向链表-C语言版
- 【C++】 双向链表.cpp
- c的双向链表
- C语言双向链表
- C/C++/JAVA 双向链表实现
- 双向链表-C/C++-多项式操作
- C语言实现双向链表删除、插入、双向输出
- poj2491Scavenger Hunt
- oops内核崩溃错误跟踪
- 算法导论--装备线调度(升序&&降序输出)
- 自定义ActionBar
- jQuery常用的正则表达式
- C双向链表
- mac下利用软链接来同步文件夹到dropbox
- openstack ice版创建实例流程整理(包括wsgi模块间调用解析)
- org.hibernate.exception.SQLGrammarException
- 【php】读取"文件列表"按时间倒序显示,并递归显示各层目录、!
- 基本HTTP流程
- 动态创建布局及添加控件
- 发烧怎么办?按这5个穴位
- poj2643Election