有序链表印刷 [英] Ordered linked list printing
本文介绍了有序链表印刷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的工作我的家庭作业,这是据我已经得到了。我现在需要知道如何打印出已inputed到列表中的信息。我需要重新配置insertNode功能也将列表排序,从最小到最大的。
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;结构listNode {
int数据; //有序域
结构listNode *接下来的;
};//原型
无效insertNode(结构listNode *头,INT X);
INT的printList(结构listNode *头,INT X);
INT空闲列表(结构listNode *头,INT X);//主要
诠释主(){
结构listNode头= {0,NULL};
INT X = 1;
的printf(这项计划将创造数的odered链表更大的
大于0,直到用户输入0或负数\\ n);
而(X 0){
的printf(请输入到存储到列表中的值\\ n);
scanf函数(%d个,&安培; X);
insertNode(安培;头,X);
}
的printf(程序终止\\ n);
系统(暂停);
}
无效insertNode(结构listNode *头,诠释X){
结构listNode * newNode,*电流;
newNode =的malloc(sizeof的(结构listNode));
newNode->数据= X;
newNode->接着= NULL;
电流=头;
而(电流 - >接下来= NULL&放大器;!&安培; CURRENT->数据< X)
{
电流=电流 - >接着,
} 如果(电流 - >接下来== NULL){
电流 - >接下来= newNode;
}
其他{
newNode->接下来=电流 - >接下来,
电流 - >接下来= newNode;
}
}
解决方案
报头 - >接下来
为NULL,当你开始,当你添加一个元素,你改变目前的为标题
:
电流=头;
写(电流 - >!=旁边NULL);
//设定电流为报头 - >接着(其为NULL)
电流=电流 - >接着,
//取消引用空
电流 - >接下来= newNode;
相反,新的元素添加到末尾:
电流=头;
而(电流 - >!下次= NULL)
电流=电流 - >接着,
电流 - >接下来= newNode;
I'm working on my homework assignment, and this is as far as i've gotten. I now need to know how to print out the information that has been inputed into the list. and i need to reconfigure the insertNode function to also sort the list from smallest to greatest.
#include <stdio.h>
#include <stdlib.h>
struct listNode{
int data; //ordered field
struct listNode *next;
};
//prototypes
void insertNode(struct listNode *Head, int x);
int printList(struct listNode *Head, int x);
int freeList(struct listNode *Header, int x);
//main
int main(){
struct listNode Head = {0, NULL};
int x = 1;
printf("This program will create an odered linked list of numbers greater"
" than 0 until the user inputs 0 or a negative number.\n");
while (x > 0){
printf("Please input a value to store into the list.\n");
scanf("%d", &x);
insertNode(&Head, x);
}
printf("Program terminated.\n");
system("PAUSE");
}
void insertNode(struct listNode * Head, int x){
struct listNode *newNode, *current;
newNode = malloc(sizeof(struct listNode));
newNode->data = x;
newNode->next = NULL;
current = Head;
while (current->next != NULL && current->data < x)
{
current = current->next;
}
if(current->next == NULL){
current->next = newNode;
}
else{
newNode->next = current->next;
current->next = newNode;
}
}
解决方案
Header->next
is NULL when you start, and when you add an element you change current to be Header
:
current = Header;
write(current->next !=NULL);
// set current to be Header->next (which is NULL)
current = current->next;
// dereference null
current->next = newNode;
Instead, add the new element to the end:
current = Header;
while (current->next != NULL)
current = current->next;
current->next = newNode;
这篇关于有序链表印刷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文