1   1  /  1  页   跳转

[版聊] 双向链表

双向链表

///////////////////////////////////////////////////////////////
/*双向链表操作*/
///////////////////////////////////////////////////////////////
//home:http://hi.baidu.com/charme000
//author:charme
//data:2009.6.21
///////////////////////////////////////////////////////////////

#include "stdio.h"
#include "stdlib.h"

typedef struct DulNode{
int data;
struct DulNode *prior;
struct DulNode *next;
}DulNode,*DulList;
////////////////////////////////////////////////////////////////
//初始化链表
////////////////////////////////////////////////////////////////
DulList InitList(int n)
{

    DulList h,s,p;
if ((h=(DulList)malloc(sizeof(DulNode)))==NULL)
{
  printf("不能分配空间");
  return 0;
}
h->next=NULL;
h->prior=NULL;
h->data=0;
    p=h;
for (int i=0;i<n;i++)
{
  if ((s=(DulList)malloc(sizeof(DulNode)))==NULL)
  {
  printf("不能分配空间");
  return 0;
  }
  p->next=s;
  printf("请输入链表元素%d: ",i+1);
  scanf("%d",&s->data);
  s->prior=p;
  s->next=NULL;
  p=s;
}

h->prior=s;
p->next=h;

return h;

}


////////////////////////////////////////////////////////////
//寻找链表中的特定元素
////////////////////////////////////////////////////////////
DulList GetElem(DulList l,int n)
{
DulList p=l->next;

for (int i=0;i<n;i++)
{
  p=p->next;
}
return p->next;

}

////////////////////////////////////////////////////////////
//插值
////////////////////////////////////////////////////////////
DulList InsertList(DulList l,int n,int e)
{
    DulList pp,ll;
if (!(pp=GetElem(l,n)))
{
  printf("找不到元素");
  return 0;
}
if (!(ll=(DulList)malloc(sizeof(DulNode))))
{
  printf("链表空间非配失败");
  return 0;
}
ll->data=e;
ll->prior=pp->prior;
pp->prior->next=ll;
ll->next=pp;
pp->prior=ll;

return ll;


}
/////////////////////////////////////////////////////////
//显示链表
/////////////////////////////////////////////////////////
void PrintfList(DulList l,int n)
{
DulList nn=l->next;


printf("--->%d\n",l->data);

while (nn!=l)
{
  printf("--->%d\n",nn->data);
  nn=nn->next;
}
}
int main()
{
DulList k,k1;
int n,data;

    ////////////////////////////////
printf("请输入链表元素个数:\n");
scanf("%d",&n);
k=InitList(n);
PrintfList(k,n);
    ////////////////////////////////

    ////////////////////////////////
printf("请输入要插入的元素:\n");
scanf("%d",&data);
k1=InsertList(k,n,data);
PrintfList(k1,n);
    ////////////////////////////////

return 0;

}

用户系统信息:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 551; .NET CLR 2.0.50727)
分享到:
gototop
 

回复:双向链表

什么的嘎后

我所居兮,青埂之峰;我所游兮,鸿蒙太空。


gototop
 

回复:双向链表

链表
gototop
 

回复:双向链表

有水平,有深度
让爱做route,接通彼此的vlan;让心做tracertrouter,穿越你我的ACL!
CCNA|CCNP|CCIE的家园http://www.ciscohuawei.com/?fromuid=110271
gototop
 

回复 3F 最硬的石头 的帖子

难道去瑞星应聘不需要熟悉链表的东西?我只会asm和c,,,,,,,,,平常写病毒,不敢拿出手,呼呼,,怕被xxx,呼呼
gototop
 
1   1  /  1  页   跳转
页面顶部
Powered by Discuz!NT