Thread: building 3 double linked list

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    building 3 double linked list

    I need to build 3 double linked lists of numbers. It is a part of a larger program I write, which will compare elemnts of those lists. After searching thoroughly archive could not find it. This is what I wrote, but I know it's a false.
    please help!


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct NodeStruct Node;
    typedef Node *NodePointer;
    struct NodeStruct{
    	int value;
    	NodePointer next;
    	NodePointer previous;
    };
    
    NodePointer newNode (NodePointer prevNode, int value)
    {
    	NodePointer newNode;
    	newNode=(NodePointer)malloc (sizeof(Node));
    	newNode->next=NULL;
    	newNode->previous=prevNode;
    	prevNode->next= newNode;
    	newNode->value= value;
    	return newNode;
    }
    	
    
    NodePointer CreateList (NodePointer head)
    {
    	NodePointer current;
    	current = head;
    	current->previous=NULL;
    	current->next=NULL;
    	return current;
    }
    
    
    
    void main ()
    {
    	Node head1, head2, head3;
    	NodePointer current1, current2, current3;
    	int count=0;
    
    	/*creating emtpy list no. 1*/
    	current1=CreateList(&head1);
    	/*populating list 1*/
    	puts ("enter an integer:");
    	scanf("%d",current1->value); 
    	while(current1->value!=0)
    	{
    		puts ("enter an integer:");
    		scanf("%d",&current1->value); 
    		current1->next=newNode(current1, current1->value);
    		current1 = current1->next;
    	}
    
    	
    	/*creating emtpy list no. 2*/
    	current2=CreateList(&head2);
    	/*populating list 2*/
    	puts ("enter an integer:");
    	scanf("%d",current2->value); 
    	while(current2->value!=0)
    	{
    		puts ("enter an integer:");
    		scanf("%d",&current2->value); 
    		current2->next=newNode(current2, current2->value);
    		current2 = current2->next;
    	}
    
    	
    	/*creating emtpy list no. 3*/
    	current3=CreateList(&head3);
    	/*populating list 3*/
    	puts ("enter an integer:");
    	scanf("%d",current3->value); 
    	while(current3->value!=0)
    	{
    		puts ("enter an integer:");
    		scanf("%d",&current3->value); 
    		current3->next=newNode(current3, current3->value);
    		current3 = current3->next;
    	}
    
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well, first off, this is wrong:
    Code:
            scanf("%d",current3->value);
    You need to pass the address of current3->value instead of the value of current3->value to scanf().
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    OK, code get copmiled after fixing this, but program is crashes after first integer input.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    	current1=CreateList(&head1);
    	/*populating list 1*/
    	puts ("enter an integer:");
    	scanf("%d",current1->value);
    CreateList() isn't allocating memory for the node, so where is the input actually being stored?
    If you understand what you're doing, you're not learning anything.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ronenk
    OK, code get copmiled after fixing this, but program is crashes after first integer input.
    Sometimes I just get the urge to say things like: "Yeah, that's probably bad. You should fix that."

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM