双向链表
来源:互联网 发布:淘宝信用保证金 编辑:程序博客网 时间:2024/05/01 22:35
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define OK 1
typedef struct
{
char data;
}Usedata;
typedef struct A
{
struct A *pre;
struct A *next;
Usedata Data;
}Node;
void Printdbl(Node *h);
Node *Creatdbl(char *s);
Node *Searchnode(Node *h,int a);
int Insertdbl(Node *h,Node *q, int a);
void Destroydbl(Node *h);
void Sortdbl(Node *h,char *s);
void Destroydbl(Node *h)
{
Node *p;
for(; h!=0;)
{ p=h->next;
free(h);
h=p;
}
h=NULL;
}
void Printdbl(Node *h)
{ for(h=h->next; h->Data.data!=0;h=h->next)
printf("%c",h->Data.data);
printf("/n");
}
Node *Creatdbl(char *s)
{
Node *h, *q, *p;
q=h=(Node *)malloc(sizeof(Node));
if(h!=NULL)
{ h->Data.data= 0;
h->next=h;
h->pre=h;
while(*s!=0)
{ p=(Node *)malloc(sizeof(Node));
p->Data.data=*s;
p->next=q->next;
p->pre=q;
q->next->pre=p;
q->next=p;
q=p;
s++;
}
}
Printdbl(h);
return h; /*必须返回一个头指针*/
}
Node *Searchnode(Node *h,int a)
{ Node *p;
for(h=h->next;h->next->Data.data!=a && h->next->Data.data!=0;h=h->next)
;
if(h->next->Data.data==0)
p=h;
else
p=h->next;
return p;
}
int Insertdbl(Node *h,Node *q, int a)
{ Node *p;
p=Searchnode(h,a);
q->next=p->next;
q->pre=p;
p->next=q;
p->next->pre=q;
return OK;
}
/*这个排序的时间复杂度比较大,strlen(s)的平方,先是从第一个和第二个开始比较,大
的往后移动,在和他相邻的比较,最后面放的是最大的,然后又从第一个和第二个开始比较,
倒数第二个是放的次大的。依次比较下去,就会排好序。*/
void Sortdbl(Node *h,char *s)
{ Node *q,*t,*p,*a; int i;
for(i=0;i<strlen(s);i++)
{ for(t=h->next,p=h->next->next; p->Data.data!=0;p=p->next)
{ if(t->Data.data>p->Data.data)
{ t->next=p->next;
p->next->pre=t;
t->pre->next=p;
p->pre=t->pre;
t->pre=p;
p->next=t;
p=t;
}
else t=t->next;
}
p=h->next->next; /*必须这样,因为p的指向在不停的变化,
必须每次重新初始化,才能指向正确的位置。*/
}
}
void main(void)
{
char *s,a,b,c;
Node *h,*p;
h=NULL;
s=NULL;
p=NULL;
clrscr();
printf("please input a string:");
gets(s);
h=Creatdbl(s); /*在主函数必须这样调用,不然h头指针不能返回,如果直接
是creat(*s),则头指针h=null,自己可以跟踪h */
Sortdbl(h,s);
Printdbl(h);
printf("Are you want to insert a number?input n or y/n");
scanf("%c",&a);
getchar(); /*上一个scanf会留一个/n在键盘缓冲区。这里加getchar是用来清除键盘缓冲区的,不然将会把/n或其他字符送给下一个scanf。*/
while(a=='y'||a=='Y')
{ printf("Where do you want to insert?input a number:/n");
scanf("%c",&b);
getchar();
printf("Input the number you want to insert:/n");
scanf("%c",&c);
getchar();
p=(Node *)malloc(sizeof(Node));
p->Data.data=c;
p->pre=NULL;
p->next=NULL;
Insertdbl(h,p,b);
Printdbl(h);
printf("Are you want to insert a number too?input n or y:/n");
scanf("%c",&a);
getchar();
}
if(a!='y'||a!='Y')
Printdbl(h);
Destroydbl(h);
getch();
}
- SzNOI 双向约瑟夫(双向链表)
- 双向链表&&堆栈
- 双向链表
- 使用双向链表
- 双向链表
- 双向循环链表
- 双向循环链表
- 双向链表
- 实现双向链表
- 双向循环链表
- 建立双向链表
- 双向链表
- 双向链表
- 双向循环链表
- 双向链表
- 链表-双向链表
- 双向链表
- 双向动态链表
- RGB、HSB、HSL互相转换
- 如何删除windows服务
- C# AOP微型框架实现
- 关于对EventHandler 和e的理解
- 基于Web Services构建动态电子商务系统的研究
- 双向链表
- 【.NET 学习笔记】修饰符
- 开始学习C#和汇编
- 今天开博
- 如何在 Windows Mobile (WinCE 5.0) 中用Vc++编程查找并连接周围的蓝牙(Bluetooth)设备并实现数据通信
- 华为面试(一)
- .NET :消息与AOP
- 终于解决了,vista下用VB6的问题
- 用C#开发.NET CF 蓝牙通信模块