网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
01月25日漏签0天
c语言吧 关注:801,825贴子:4,375,770
  • 看贴

  • 图片

  • 吧主推荐

  • 视频

  • 游戏

  • 1回复贴,共1页
<<返回c语言吧
>0< 加载中...

双链表

  • 只看楼主
  • 收藏

  • 回复
  • 大爱的thmise
  • 毛蛋
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include <stdio.h>
#include <malloc.h>
typedef struct Node
{
struct Node *prior;
struct Node *next;
int num;
}Node;
Node *createNode(int value)
{
Node *p=(Node *)malloc(sizeof(Node));
p->num=value;
p->next=p->prior=NULL;
return p;
}
Node *create_empty_list()
{
return createNode(0);
}
Node *insert(Node *head,int num)
{
Node *p2,*p;
p2=head;
p=createNode(num);
if(head->next==NULL)// 如果为空链表的话
{
head->next=p;
p->next=NULL;
p->prior=NULL;
head->prior=p;
}
else
{
/*while(p2->next!=NULL) //如果不是空链表,遍历得到最后一个节点
{
p2=p2->next;
}
p2->next=p;
p->next=NULL;
p->prior=p2;
head->prior=p;*/
head->next->prior=p;
p->prior=NULL;
p->next=head->next;
head->next=p;
}
}
void output(Node *h)
{
Node *l;
l=h->next;
while(l!=NULL)
{
printf("%d\n",l->num);
l=l->next;
}
}
int list_len(Node *head)
{
int length=0;
Node *p;
p=head->next;
while(p!=NULL)
{
length++;
p=p->next;
}
head->num=length;
return length;
}
Node *index_list(Node *head,int number)
{
Node *p;
p=head->next;
while(p != NULL && p->num != number)
{
p = p->next;
}
if(p->num == number)
{
return p;
}
else
return NULL;
}
Node *delete_node(Node *head,int number)
{
Node *p;
p=index_list(head,number);
if(p->prior == NULL)
{
head->next=p->next;
}
else
p->prior->next=p->next;
}
int main(int argc, char *argv[])
{
Node *head,*p1;
head = create_empty_list(); //创建空链表
insert(head,11); //插入数据
insert(head,19);
insert(head,7);
insert(head,6);
insert(head,34);
insert(head,56);
insert(head,67);
insert(head,78);
insert(head,13);
insert(head,15);
insert(head,8);
printf("有%d个数据\n",list_len(head));
p1 = index_list(head,78);
if(p1== NULL) return 0;
printf("找到的数据是%d\n",p1->num);
delete_node(head,8);
output(head);
return 0;
}


  • 大爱的thmise
  • 毛蛋
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
这个非循环双链表 有什么简单的排序方法吗?


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 1回复贴,共1页
<<返回c语言吧
分享到:
©2026 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示