能帮我写个一般程序和简易程序吗?设计一个处理单向链表的一般程序和简易程序:链表的排序

求单向链表排序程序及其设计报告_百度知道问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
上图是题目需要实现的效果。纯小白才开始学C语言,求指导,只输出了0到9,不知如何逆序输出。中间重复的代码部分应该是要优化一下的,但暂时还不知到该怎么写。
#import &Foundation/Foundation.h&
typedef struct Node_ {
struct Node_ *
Node *createNode(int value, Node *next) {
Node* node = malloc(sizeof(Node));
node-&value =
node-&next =
void printList(Node *firstNode) {
for (Node *node=firstN node!=NULL; node=node-&next) {
printf("%d\n", node-&value);
int main(int argc, const char * argv[]) {
@autoreleasepool {
Node *current = createNode(0, NULL);
Node *first =
current-&next = createNode(1, NULL);
current = current-&
current-&next = createNode(2, NULL);
current = current-&
current-&next = createNode(3, NULL);
current = current-&
current-&next = createNode(4, NULL);
current = current-&
current-&next = createNode(5, NULL);
current = current-&
current-&next = createNode(6, NULL);
current = current-&
current-&next = createNode(7, NULL);
current = current-&
current-&next = createNode(8, NULL);
current = current-&
current-&next = createNode(9, NULL);
current = current-&
printList(first);
char *str = "reversed:";
printf("%s\n", str);
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
用递归就好了
void printReversedList(Node *firstNode){
if(firstNode == NULL)
printReversedList(firstNode-&next);
printf("%d\n", firstNode-&value);
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
遍历链表,从第一个节点开始,依次把节点push到栈中。遍历完成后,依次从栈中pop出来,同时打印即可。
@aluo1的答案用了递归,本质上也是用了栈,只不过是更底层的函数调用栈。
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
已经有人回答了你的正面问题。所以我就不重复了,但我想说:为什么不设计成双向的链表呢?如果可以的话应该尽量是双向的。
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
定义一个temp指针,一个一个往前指= =
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
不知道这样你明白了没有。。。
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
是不是也可以用一个数组来存储,数组逆序输出。大家觉得不好,请喷
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
C语言以前学过,现在忘光了,但是前几天刚好看到链表这块
我感觉是在链表里再加个struct Node_ *prev,用于指向上一个结点的地址
同步到新浪微博
分享到微博?
Hi,欢迎来到 SegmentFault 技术社区!⊙▽⊙ 在这里,你可以提出编程相关的疑惑,关注感兴趣的问题,对认可的回答投赞同票;大家会帮你解决编程的问题,和你探讨技术更新,为你的回答投上赞同票。
明天提醒我
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
扫扫下载 App
SegmentFault
一起探索更多未知提问者:匿名 & 时间:
>> >> >> >> >> >> >> >> >> >>
C语言单向链表排序如何实现?
这是我的整体程序代码:创建链表+打印链表+排序链表(输入的参数是一个建立好了的链表),现在就是排序链表函数代码写不出来。求高人指教。#include
"stdio.h"#define
sizeof(struct student)struct
*creatLink();void
printLink(struct
student *);struct
*sortLink(struct
student *); struct
student{char name[10];
float score[5];
/*记录结点个数*/struct
*creatLink(){struct
student *head,*p1,*p2;n=0;int N;printf("请输入一组学生的个数N =");scanf("%d",&N);
printf("请输入学生的信息,格式为:学号 姓名 年龄 00 00 00 00 00\n");
head=NULL;while(N!=0){
p1=(struct
student *)malloc(LEN); /*开辟一个新单元*/
scanf("%d %s %d",&p1-&num,&p1-&name,&p1-&age);
for(i=0;i&5;++i){
scanf(" %f",&p1-&score[i]);
/*结点个数加1*/
head=p1; /*是第一个结点, 置头指针*/
p2-&next=p1; /*非首结点, 将新结点插入到链表尾*/
/*设置新的尾结点*/N=N-1;;}p2-&next=NULL;return(head);}void
printLink(struct
student *head){struct
student *p,*t;printf("num\tname\tage\tscore\t\t\t\t\taverage\tsum\n",n);p=if(head!=NULL)
float sum=0;printf("%d\t%s\t%d",p-&num,p-&name,p-&age);
for(i=0;i&5;++i){
printf("\t%4.2f",p-&score[i]);
sum=sum+p-&score[i];
printf("\t%4.2f\t%4.2f",sum/5,sum);
printf("\n&quo缉埂光忌叱涣癸惟含隶t;);
}while(p!=NULL);
*sortLink(struct
student *head){} main(){
head=creatLink();
printf("Now ,these [%d] records are:",n);
printf("\n排序之前:\n");
printLink(head);
printf("排序之后:\n");
printLink(head);
getch(); }
struct student* printf_sort(struct student *head){struct student *p1,*p2,*ptemp,*pfinished=NULL;for(p1=p1-&next!=)//对链表进行从大到小排序(这里用冒泡法)//p1使之总是指向头结点,pfinished使之总是指向已排序好的最前面的结点//ptemp作为中介,保存p2的上一个结点{for(p2=p1;p2-&next!=){if(p2-&num&p2-&next-&num)//p2的值小于p2-&next的值,交换{if(p2==p1)//头结点要交换{p1=p缉埂光忌叱涣癸惟含隶2-&p2-&next=p1-&p1-&next=p2;ptemp=p1;}else{ptemp-&next=p2-&ptemp=p2-&p2-&next=ptemp-&ptemp-&next=p2;}}else//不需要交换,则p2、ptemp前进1位{ptemp=p2;p2=p2-&}}pfinished=p2;}}javanetwork
C语言 单向链表如何排序?
用C语言实现单向链表的直接插入排序,冒泡排序和选择排序
c语言单向链表排序
单向链表排序
C语言链表中如何实现对一组数据进行排序?更多相关问题&&
单向链表的排序设计
C语言的单向链表删除节点的问题
Java语言写出实现将单向链表顺序反转的函数?
单向链表的建立与遍历
C++单向链表 排序排序的算法有很多,对空间的要求及其时间效率也不尽相同。下面列出了一些常见的排序算法。这里面插入排序和冒泡排序又被称作简单排序,他们对空间的要求不高,但是时间效率却不稳定;而后面三种排序相对于简单排序对空间的要求稍高一点,但时间效率却能稳定在很高的水平。基数排序是针对关键字在一个较小范围内的排序算法。以下是常见的几种排序算法:插入排序冒泡排序选择排序快速排序堆排序归并排序基数排序希尔排序以冒泡算法为例给出以下的代码例子:LinkList* LinkListBubbleSort(LinkList* pHead){
LinkList* pCurr = (LinkList *)NULL;
LinkList* pPost = (LinkList *)NULL;
LinkList* pFirst = (LinkList *)NULL;
LinkList* pPrev = (LinkList *)NULL;
unsigned int uwLinkListLength = 0;
unsigned int uwInnerLoopIndex = 0;
unsigned int uwOuterLoopIndex = 0;
if (NULL == pHead)
return (LinkList *)NULL;
pFirst = (LinkList *)malloc(sizeof(LinkList));
if (NULL == pFirst)
return (LinkList *)NULL;
uwLinkListLength = GetLinkListLength(pHead);
uwOuterLoopIndex = uwLinkListL
pFirst-&m_next = pH
while (uwOuterLoopIndex & 0)
pFirst-&m_next = pH
pCurr = pFirst-&m_
pPost = pCurr-&m_
uwInnerLoopIndex = uwOuterLoopI
while ((NULL != pPost) && (uwInnerLoopIndex & 0))
if (pCurr-&m_key & pPost-&m_key)
pFirst-&m_next-&m_next = pPost-&m_
pPost-&m_next = pFirst-&m_
if (pHead == pFirst-&m_next)
pHead = pP
pPrev = pP
余下全文&&求证:这个程序是实现了一个链表的大致功能吗?
[问题点数:50分,结帖人giveupwithtears]
求证:这个程序是实现了一个链表的大致功能吗?
[问题点数:50分,结帖人giveupwithtears]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2013年 总版技术专家分年内排行榜第三
2012年 总版技术专家分年内排行榜第七
本帖子已过去太久远了,不再提供回复功能。链表(一)――创建一个最基本的单向链表
链表中用来存储一个数据的存储单元。
一个链表至少需要由两部分组成,就是数据域和指针域,一般形式的结点定义为:
struct node
E //Elem类型泛指基本数据类型
struct node *
typedef struct node E
以上两步等价于:
typedef struct node
struct node *
2.使用指针变量p表示结点的成员
*p.data //错误,正确为(*p).data
p->data //正确
p->next //正确
注:(.)、(->)、([])三种运算符的优先级依次递减(这三个是C语言中优先级最高的运算符)。
3.前驱结点;某个结点的前一个结点。
后继结点:某个结点的后一个结点。
单向链表的特点:
(1)有且只有一个结点无前驱,即头结点。头结点通过head指针指向。
(2)有且只有一个结点无后继,即尾结点。尾结点的next域值为NULL。
(3)除了头结点尾结点之外剩下的所有结点有且只有一个前驱,有且只有一个后继。
example 1:创建单向链表,最基本的实现方式。
#define NULL 0
typedef struct node{
struct node * //不能使用ElemSN
int main()
ElemSN *head, *p;
int i,/*ms用来存放结点的个数*/
printf("Please input node number:");
scanf("%d", &ms);
head = p = (ElemSN *)malloc(sizeof(ElemSN));
printf("Please input data:");
scanf("%d", &x);
head->data =
head->next = NULL;/*创建第一个结点,因为是头结点,和后面的结点有些不一样所以单独写出来*/
for(i = 0; i next = (ElemSN *)malloc(sizeof(ElemSN));
printf("Please input data:");
scanf("%d", &x);
p->next->data =
p->next->next = NULL;
for(p = p != NULL; p = p->next)
printf("%d ", p->data);
printf("\n");
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'

我要回帖

更多关于 什么是程序 的文章

 

随机推荐