Reputation: 163
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;
}
}
Upvotes: 1
Views: 450
Reputation: 163
got it working, just need to figure out how to make the printList and freeList functions
#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 *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);
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;
}
}
Upvotes: 0
Reputation: 4467
There are three issues in your impl:
you should append new numbers at the end, so you should have two pointers into the list: one points to the head, another one to the tail (you could also add at the top of list, then you need only one pointer, but then the list is reverse ordered)
the pointer-chaining code is wrong, i think you want to try to insert new elements after your head-node, so your code should be written like:
current=Header->next; // the node Header->next is currently pointing to
Header->next=newNode; // let header->next point to new ele
newNode->next=current; // let new ele's next point to old top ele
the header-node is somehow special and useless, you should handle an empty list as a special case, and header should be initially 0, and insertnode would work like this:
if (header==0)
header=newNode
else
// do the stuff like written in 2.
All this can for sure done in a different way, but this is one common aproach to that list-problem... (Hmm, somehow the 4 leading blanks for code dont work?)
Upvotes: 0
Reputation: 5757
current = Header;
write(current->next !=NULL);
current = current->next;
current->next = newNode;
You're trying to access current->next without every having assigned it. And you're not actually searching for the correct place to insert it in the linked list (from your question, it sounds like it's supposed to be sorted).
Try something like:
current = Header;
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;
}
Upvotes: 0
Reputation: 137442
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;
Upvotes: 3