我正在尝试创建一个链接列表,该列表将接收来自用户的输入,对其进行排序,并在用户输入0或负数后将其打印出来。在某处,我的代码在打印循环的开头添加“0”。
示例:我输入1-2-3-4-5。程序然后返回0-1-2-3-4-5。
例2:我输入1-2-3-4-5。程序然后返回0-5-1-2-3-4。这对我来说也是一个问题,因为我最终需要让程序从最小到最大的顺序排列。但是现在我专注于让它接受输入1-2-3-4-5并打印1-2-3-4-5。
#include <stdio.h>
#include <stdlib.h>
struct listNode{
int data;
struct listNode *next;
};
//prototypes
void insertNode(struct listNode *Head, int x);
void printList(struct listNode *Head);
int freeList(struct listNode *Head, 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);
if (x > 0){
insertNode(&Head, x);
}
}
printList(&Head);
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;
}
}
void printList(struct listNode * Head){
struct listNode *current = Head;
while (current != NULL){
if(current > 0){
printf("%d \n", *current);
}
current = current->next;
}
}
答案 0 :(得分:1)
列表中有一个零,因为你把它放在那里:
struct listNode Head = {0, NULL};
如果您想要快速修复,请更改printList()
中的行以及处理该列表的任何其他内容:
struct listNode *current = Head;
为:
struct listNode *current = Head->next;
这将从列表的第二个元素开始,忽略你在那里开始的那个元素。
然而,一个更好的方法可能是不来拥有那个多余的元素:
#include <stdio.h>
#include <stdlib.h>
struct listNode {
int data;
struct listNode *next;
};
// Prototypes (freeList removed since not defined).
void insertNode(struct listNode **pHead, int val);
void printList(struct listNode *Head);
// Main program for testing.
int main(void) {
// List initially empty.
struct listNode *Head = NULL;
int x = 1;
puts("This program will create an ordered linked list");
puts(" of numbers greater than 0 until the user");
puts(" enters 0, a negative number, or a non-integer.");
for(;;) {
puts("Please input a value to store into the list.");
if ((scanf("%d", &x) != 1) || (x <= 0)) break;
insertNode(&Head, x);
}
printList(Head);
}
void insertNode(struct listNode **pHead, int val){
struct listNode *newNode, *current, *previous;
// Allocate new node, should really check for failure here.
newNode = malloc (sizeof (struct listNode));
newNode->data = val;
newNode->next = NULL;
// Handle inserting into empty list.
if (*pHead == NULL) {
*pHead = newNode;
return;
}
// Find node to insert before.
current = *pHead;
while (current != NULL && current->data < val) {
previous = current;
current = current->next;
}
// Handle inserting at start of list.
if (current == *pHead) {
newNode->next = *pHead;
*pHead = newNode;
return;
}
// Handle inserting at end of list.
if (current == NULL) {
previous->next = newNode;
return;
}
// Handle inserting somewhere inside the list.
newNode->next = current;
previous->next = newNode;
}
void printList (struct listNode *Head) {
struct listNode *current = Head;
if (current == NULL) {
puts ("There are no numbers.");
return;
}
puts ("Numbers are:");
while (current != NULL) {
printf (" %d\n", current->data);
current = current->next;
}
}
我还清理了其他几件事,例如将*current
更改为更明确的current->data
,将指针传递给头部,以便你可以改变它,并对主输入循环稍作修改。这是一个示例运行:
This program will create an ordered linked list
of numbers greater than 0 until the user
inputs 0 or a negative number.
Please input a value to store into the list.
4
Please input a value to store into the list.
1
Please input a value to store into the list.
8
Please input a value to store into the list.
5
Please input a value to store into the list.
6
Please input a value to store into the list.
3
Please input a value to store into the list.
2
Please input a value to store into the list.
9
Please input a value to store into the list.
7
Please input a value to store into the list.
0
Numbers are:
1
2
3
4
5
6
7
8
9
答案 1 :(得分:0)
在printList
中,您打印的值*current
不是整数(它是struct listNode
)。你的编译器可能会警告你这件事。
尝试打印current->data
,而不仅仅是*current
,事情应该有效。
您可能还需要将if(current > 0)
测试更新为current->data > 0
,如果这真的是您想要的那样。
答案 2 :(得分:0)
您正在打印的printList()函数中的第一项是列表的Head元素,其中包含零作为数据。你很幸运,因为你的struct的第一个元素是int数据,所以当你取消引用current的指针时,你碰巧在struct的开头得到了int。
实际上你应该重写打印功能,如下所示:
void printList(struct listNode * Head){
struct listNode *current = Head->next;
while (current != NULL){
printf("%d \n", current->data);
current = current->next;
}
}
答案 3 :(得分:0)
... Somewhere my code is adding a "0" to the beginning of the print loop.
是的,在您的代码第一次初始化Head
时,您引入了0
。
这是一行:
struct listNode Head = {0, NULL};
假设您将上述值从0更改为999,您的代码将打印出999作为第一个数字。
您需要在插入过程中处理Head节点的情况。