LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2007, 02:17 PM   #1
linuxlover1
Member
 
Registered: Jun 2003
Location: UK
Posts: 54

Rep: Reputation: 15
Linked List question (C)


Hi , i wrote a function , that creates an order Linked-List. The problem here is when i want to add a node at the end of the list nothing happens (I print the list and the last node is missing).
If you have any idea , whats wrong with the code please help.
Thx a lot !!

Code:
#include <stdio.h>
#include <stdlib.h>

struct linked_list {
	int num;
	struct linked_list *next;
};
typedef struct linked_list node;

void inorder(node **head,int value);
void print(node *head);

main() {
        node *head;
	head=NULL;
	printf("Implementing a Linked-List\n");
        inorder(&head,10);
        inorder(&head,7);
	inorder(&head,-1);
	inorder(&head,11);
	print(head);
}

void inorder(node **head,int value) {
	node *new_node,*start,*before;
	start=*head;
	int end=0;
	new_node=(node *)malloc(sizeof(node));
	new_node->num=value;
	if ( *head==NULL) {
		new_node->next=*head;
		*head=new_node;
	}
	else if (new_node->num < start->num) {
		new_node->next=start;
		*head=new_node;
	}
	else {
		while ( start->num < new_node->num) {
			before=start;
			start=start->next;
			if ( start==NULL) {
				start=new_node;
				new_node->next=NULL;
				end=1;
				break;
			}
		}
		if (end==0) {
			before->next=new_node;
			new_node->next=start;
		}
	}
}

void print(node *head) {
	int i=1;
	while (head != NULL ) {
		printf("Node%d=%d\n",i,head->num);
		i++;
		head=head->next;
	}
}
 
Old 03-07-2007, 06:45 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Code:
#include <stdio.h>
#include <stdlib.h>

struct linked_list {
        int num;
        struct linked_list *next;
};
typedef struct linked_list node;

void inorder(node **head,int value);
void print(node *head);

int main() { // main should be int
        node *head;
        head=NULL;
        printf("Implementing a Linked-List\n");
        inorder(&head,10);
        inorder(&head,7);
        inorder(&head,-1);
        inorder(&head,11);
        print(head);

        // here you should use a loop and free the memory. for each (successful) malloc call you need a free

        return 0;
}

void inorder(node **head,int value) {
        node *new_node,*start,*before;
        start=*head;
        int end=0;
        new_node=(node *)malloc(sizeof(node*)); //sizeof(node) and sizeof(node*) are not the same

        if (new_node == NULL) // alwayws check to see if malloc was successfully
        {
                perror("could not allocate space");
                return;
        }

        new_node->num=value;
        if ( *head==NULL) {
                new_node->next=*head;
                *head=new_node;
        }
        else if (new_node->num < start->num) {
                new_node->next=start;
                *head=new_node;
        }
        else {
                while ( start->num < new_node->num) { // the body of your while loop was the actual problem
                        before=start;
                        start=start->next;   
                        if ( start==NULL) {  
                                start=before;
                                start->next=new_node;
                                end=1;
                                break;
                        }
                }
                if (end==0) {
                        before->next=new_node;
                        new_node->next=start;
                }
        }
}

void print(node *head) {
        int i=1;
        while (head != NULL ) {
                printf("Node%d=%d\n",i,head->num);
                i++;
                head=head->next;
        }
}
hope it helps. let me know if you want to ask anything. also do some more testing to see if this is fail-proof.

Last edited by nadroj; 03-07-2007 at 06:53 PM.
 
Old 03-07-2007, 09:37 PM   #3
v.tieubao
LQ Newbie
 
Registered: May 2006
Posts: 7

Rep: Reputation: 0
Quote:
Originally Posted by linuxlover1
Hi , i wrote a function , that creates an order Linked-List. The problem here is when i want to add a node at the end of the list nothing happens (I print the list and the last node is missing).
If you have any idea , whats wrong with the code please help.
Thx a lot !!

Code:
....
		while ( start->num < new_node->num) {
			before=start;
			start=start->next;
			if ( start==NULL) {
				start=new_node; //before->next=new_node;
				new_node->next=NULL;
				end=1;
				break;
			}
		}
....
See red comment on your code, hope it help.

Cheers
 
Old 03-07-2007, 09:40 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,657
Blog Entries: 4

Rep: Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938
It's usually not considered good form to help a student with his or her homework...

The trick in working out algorithms like this one is to grab a piece of paper and a number-two pencil and draw it out.
 
Old 03-08-2007, 03:49 AM   #5
linuxlover1
Member
 
Registered: Jun 2003
Location: UK
Posts: 54

Original Poster
Rep: Reputation: 15
Thx a lot men !
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked list manas_sem Programming 3 12-21-2006 01:53 AM
C linked list exvor Programming 4 04-28-2006 05:25 AM
C++ Linked List question lowpro2k3 Programming 3 06-16-2005 10:15 AM
linked list + c dilberim82 Programming 5 05-04-2005 11:48 PM
C++ linked list fun chens_83 Programming 2 08-04-2003 07:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration