关于链表的菜鸟问题

wjj2100 2008-03-27 11:10:25
本人刚刚接触数据结构,由于C语言还没有学玩,所以对一些基础问题非常混沌,向高手求教。
1、对输入的一批数据用尾插法建立一带表头结点的单向线性链表, 输出该链表的数据,仅对单向线性链表进行处理,在不建立新链表的前提下,使原链表中的数据结点逆序,输出处理后的链表数据。
2、实现仅带尾结点指针单向循环链表的建立、插入、删除、查找操作算法。
3、对两个带表头结点的单向线性链表按结点数据域分别进行递增排序,然后归并成一个链表。
...全文
195 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangfei_119 2008-05-04
  • 打赏
  • 举报
回复
楼主,现在先回答你的第一个问题,链表的尾插法创建和不建新链表的情况下进行逆置.

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
};

struct node* creat()
{
struct node *p,*q,*h;
int i,n;

printf("输入想创建的节点个数:");
scanf("%d",&n);
h=(struct node*)malloc(sizeof(struct node));
h->data=n;
h->next=NULL;
q=h;
for(i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
printf("input num[%d]:",i);
q->next=p;
scanf("%d",&p->data);
p->next=NULL;
q=p;
}
printf("创建的链表为:");
return h;
}

void print(struct node *h)
{
struct node *p;
p=h->next;
while(p!=NULL)
{
printf("->%d",p->data);
p=p->next;
}
printf("\n");
}

struct node* contrary(struct node *head)
{
struct node *h,*p,*q,*r;
h=head;
p=head->next;
q=h;
q->next=NULL;
while(p!=NULL)
{

r=p->next;
p->next=q->next;
q->next=p;
p=r;
}
printf("逆置后的链表:");
return h;
}

main()
{
struct node *head,*head1;
head=creat();
print(head);
head1=contrary(head);
print(head1);
}

运行结果如下:
输入想创建的节点个数:5
input num[0]:1
input num[1]:2
input num[2]:3
input num[3]:4
input num[4]:5
创建的链表为:->1->2->3->4->5
逆置后的链表:->5->4->3->2->1
Press any key to continue

fangfei_119 2008-05-04
  • 打赏
  • 举报
回复
两个有序链表的归并排序如下:

#include <stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

void print(struct node *h)
{
struct node *p;
p=h->next;
while(p!=NULL)
{
printf("->%d",p->data);
p=p->next;
}
printf("\n\n");
}

struct node* order(struct node *head)
{
struct node *h,*p,*q;
int i,n,chg;
printf("ÊäÈ뵱ǰÅÅÐòÁ´±í³¤¶È:");
scanf("%d",&n);
h=head;
for(i=0;i<n-1;i++)
for(q=h->next,p=q->next;p!=NULL;q=p,p=p->next)
if(q->data>p->data)
{
chg=p->data;
p->data=q->data;
q->data=chg;
}
printf("ÅÅÐòºóµÄÁ´±íΪ:");
print(h);
return h;
}

struct node* creat()
{
int i,n;
struct node *p,*q,*h;

printf("ÊäÈëÒª´´½¨µÄÁ´±í³¤¶È:");
scanf("%d",&n);
h=(struct node*)malloc(sizeof(struct node));
h->data=n;
h->next=NULL;

q=h;
for(i=0;i<n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
printf("input num[%d]:",i);
scanf("%d",&p->data);
p->next=NULL;
q->next=p;
q=p;
}
printf("´´½¨µÄÁ´±íΪ:");
print(h);
return h;
}

struct node* join(struct node *head1,struct node *head2)
{
struct node *h,*p,*p1,*p2;
h=(struct node *)malloc(sizeof(struct node));
h->data=0;
h->next=NULL;
p=h;
p1=head1->next;
p2=head2->next;
while(p1&&p2)
{
if(p1->data<p2->data)
{
p->next=p1;
p=p1;
p1=p1->next;
}
else
{
p->next=p2;
p=p2;
p2=p2->next;
}
}
p->next=p1?p1:p2;
printf("ºÏ²¢ºóµÄÁ´±íΪ:");
print(h);
return h;
}

void main()
{
struct node *head,*head1,*head2;
head1=creat();
head1=order(head1);
head2=creat();
head2=order(head2);
head=join(head1,head2);
}

运行结果如下:
输入要创建的链表长度:5
input num[0]:12
input num[1]:3
input num[2]:43
input num[3]:54
input num[4]:79
创建的链表为:->12->3->43->54->79

输入当前排序链表长度:5
排序后的链表为:->3->12->43->54->79

输入要创建的链表长度:3
input num[0]:20
input num[1]:23
input num[2]:12
创建的链表为:->20->23->12

输入当前排序链表长度:3
排序后的链表为:->12->20->23

合并后的链表为:->3->12->12->20->23->43->54->79

Press any key to continue
UltraBejing 2008-05-01
  • 打赏
  • 举报
回复
这个简单啊,网上搜一下就得到答案了.
knowledge_Is_Life 2008-05-01
  • 打赏
  • 举报
回复
有问题请先GOOGLE,BAIDU
甲壳虫 2008-04-07
  • 打赏
  • 举报
回复
这些题目好象教材上都有明确的答案,只不过你需要的是把最后的几章好好看了,就会非常清楚的.这些东西都是C语言的经典,学计算机的就必须把他弄明白.
xiguagege 2008-03-28
  • 打赏
  • 举报
回复
路过。。。
建议读看看书
joinnycoo 2008-03-27
  • 打赏
  • 举报
回复
这些东西网上太多了,百度一下就可以了。~~
qiucp 2008-03-27
  • 打赏
  • 举报
回复
同意楼上

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧