Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1969326
  • 博文数量: 610
  • 博客积分: 11499
  • 博客等级: 上将
  • 技术积分: 5511
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-12 19:27
文章分类

全部博文(610)

文章存档

2016年(5)

2015年(18)

2014年(12)

2013年(16)

2012年(297)

2011年(45)

2010年(37)

2009年(79)

2008年(101)

分类: LINUX

2010-06-13 14:04:31

/*
利用链表的插入运算建立循环双向链表,然后利用链表的查找、删除、计数、输出等运算反复实现链表的这些操作(插入、删除、查找、计数、
输出单独写成函数的形式),并能在屏幕上输出操作前后的结果。
*/
#include
#include
/*创建长度为n的双向循环链表,值为整数。
查找第k个元素并输出。
删除所有值为m的元素。
逆置链表并输出。
*/
int n;
typedef struct str
{
 int num;
 struct str *pre;
 struct str *next;
}node;
void creat(node * h)
{
 node *c,*r;
 r=h;
 cout<<"输入链表的长度: ";
 cin>>n;
 for(int i=0;i {
  c=(node *)malloc(sizeof(node));
  cout<<"输入数值: "<  cin>>c->num;
  r->next=c;
  c->pre=r;
  r=c;
 }
 r->next=h->next;
 h->next->pre=r;
}//..................

void select(node *h,int k)
{
 node *p;
 p=h->next;
 if(k>n)
  cout<<"errer."< else
 {
  for(int i=1;i  {
   p=p->next;
  }
  cout<<"第"<num< }
}
 
void Delete(node *h,int m)
{
 node *p,*q;
 q=h;
 p=h->next;
 while(p->next!=h->next)
 {
  if(h->next->num==m)      //删除首结点
  {
   h->next=p->next;
   p->pre->next=p->next;
   p->next->pre=p->pre;
   free(p);
   p=q->next;
  }
  else if(p->num==m)           //删除中间结点
  {
   q->next=p->next;
   p->next->pre=q;
   free(p);
   p=q->next;
  }
  else
  {
   q=p;
   p=p->next;
  }
 }
 if(p->num==m)              //删除尾结点
 {
  q->next=p->next;
  p->next->pre=q;
  free(p);
 }
}

void nizhi(node *h) 
{
 node *t,*p;
 h->next=h->next->pre;    //把头结点只向尾结点
 p=t=h->next;
 for(int i=0;i {
  t->next=p->next;
  p->next=p->pre;
  p->pre=t->next;
  p=p->next;
 }
}

void dep(node *h)  //显示链表
{
 node *p;
 p=h->next;
 cout<<"head->";
 while(p->next!=h->next)
 {
  cout<num<<"->";
  p=p->next;
 }
 cout<num;
 cout<<"->head"<}

void main()
{
 node *h;
 int k,m;
 h=(node *)malloc(sizeof(node));
 creat(h);
 dep(h);
 cout<<"输入查找的结点:";
 cin>>k;
 select(h,k);
 cout<<"输入删除的值: ";
 cin>>m;
 Delete(h,m);
 nizhi(h);
 dep(h);
}
阅读(1069) | 评论(0) | 转发(0) |
0

上一篇:单链表

下一篇:线性顺序表

给主人留下些什么吧!~~