|
-
January 15th, 2011, 04:09 PM
#1
Linked Lists....Trouble.
Code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int x;
struct node *next;
};
//this function is to build a list by creating a node then appending it on the end of the list
node * create_append(int x, node * oldtail);
//navigate and print list
void print_list(node * list);
void main()
{
node *head;
node *tail;
node *insert;
head = (node*)malloc(sizeof(node));
tail = head;
head->next = tail;
tail->next = NULL;
/*insert = (node*)malloc(sizeof(node));
insert->x = 6;
insert->next = NULL;*/
for(int i = 0; i <= 5; i++)
{
create_append(i, tail);
}
print_list(head);
}
node * create_append(int x, node * oldtail)
{
node * temp;
temp = (node*)malloc(sizeof(node));
oldtail->next = temp;
temp->x = x;
temp->next = NULL;
return temp;
}
void print_list(node * list)
{
node * end;
end = (node*)malloc(sizeof(node));
end = list;
while(end->next != NULL)
{
printf("%d\r\n", end->x);
end = end->next;
}
}
Code not working. O_o I'm about to get out some notebook paper and write everything down instead of doing it in my head but in the meantime I thought I'd post here. This code doesn't do what I want (look through and append new node to end, then print). Any ideas?
Ultimate goal is to do this:
"Insert in order Given a linked list of integers sorted from smallest (at the head end) to largest, and a pointer to a single node containing an integer, insert the node in the linked list so that it remains sorted."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|