链表,获取不需要的值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了链表,获取不需要的值相关的知识,希望对你有一定的参考价值。
我正在尝试创建一个链接列表,该列表将接收来自用户的输入,对其进行排序,并在用户输入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.
");
while (x > 0){
printf("Please input a value to store into the list.
");
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
", *current);
}
current = current->next;
}
}
它在列表中有一个零,因为你把它放在那里:
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
", 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
在printList
你打印的值是*current
,它不是一个整数(它是一个struct listNode
)。你的编译器可能会警告你这件事。
尝试打印current->data
,而不仅仅是*current
,事情应该有效。
您可能还需要更新您的if(current > 0)
测试,使其更像current->data > 0
,如果这真的是您想要的。
您正在打印的printList()函数中的第一项是列表的Head元素,其中包含零作为数据。你很幸运,因为你的struct的第一个元素是int数据,所以当你取消引用current的指针时,你碰巧在struct的开头得到了int。
实际上你应该重写打印功能,如下所示:
void printList(struct listNode * Head){
struct listNode *current = Head->next;
while (current != NULL){
printf("%d
", current->data);
current = current->next;
}
}
... 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节点的情况。
以上是关于链表,获取不需要的值的主要内容,如果未能解决你的问题,请参考以下文章