Thread: Adding linked list problem

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    22

    Adding linked list problem

    Hi guys. I'm trying to learn how to add two long integers in a linked list.

    So 1->2->3->NULL and 4->5->6->NULL should give me 5->7->9->NULL

    I know how to approach the problem but the problem is, the thing I'm trying to do isn't working and the compiler gives me an error.

    So far, this is a very very rough draft of my code and I'm basically going to change it later but for the purposes of testing, this should be enough.

    So my method of creating the linked list is this:

    Code:
    void create_list(struct list **ptr,char *input) {
    	int i;
    	struct list *tmp;
    	
    	for(i = 0; i < strlen(input); i++){
    		if(i == 0){
    			(*ptr) = malloc(sizeof(struct list));
    			(*ptr)->data = input[i]-'0';
    			(*ptr)->next = NULL;
    			(*ptr)->prev = NULL;
    		}
    		else {
    			tmp = malloc(sizeof(struct list));
    			tmp->data = input[i]-'0';
    			tmp->next = NULL;
    			tmp->prev = *ptr;
    			(*ptr)->next = tmp;
    			*ptr = tmp;
    		}
    	}
    	//printf("%d", temp->prev->data);
    }
    I know it works because as you can see, I included a printf just for testing and if I enter values like 1234 in the command prompt, I get the value 3 when printf works so that's fine.

    So what I have for my add method is this:

    Code:
    void add(struct list *first, struct list *second) {
    
    int carry = 0;
    
    while(*first != NULL && *second != NULL) {
    
    
    }
    }

    Yes, I'm aware the add method is virtually empty but my problem is simple. Why is the while loop giving me an error?

    What I'm trying to do is traverse the list and I included a while loop to check if both lists are not null and then I'm going to get the data from both list's tail and add them together. But I just have to figure out the while loop first.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should store your numbers backwards: 123 +45

    5->4
    3->2->1
    ----------
    8->6->1



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

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    22
    I thought I already was.

    Like I said, when I enter 12345, and the printf evalutes, it returns 4 because temp->prev->data = 4. So I know I'm doing it backwards.

    Besides, that's not my problem. I just need to figure out why the while loop gives me an error.


    Maybe I cannot actually compare a linked list with != ? But then how would I say loop it until the linked lists are null?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You are trying to compare dreferenced pointers to a pointer... NULL = void *0... lose the asterisks in your while loop and see what happens.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void add(struct list *first, struct list *second) {
    
    int carry = 0;
    
    while(*first != NULL
    That means "give me the structure itself", not "is this pointer null".


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

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    22
    Okay so I have
    Code:
    while(first!=NULL && second!=NULL) and it works fine now.
    I still don't get it. Can you explain it in simple terms?


    And can you also check if I'm doing the algorithm right?
    What I'm trying to do is get the last data of first so first->data and then add it to second->data
    So it will be first->data + second ->data.
    Will that work? And will the while loop go the first/second->prev->data or do I have to implement it myself?

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    22
    I guess it doesn't work like this?

    Code:
    void add(struct node *first, struct node *second) {
    	
    	
    	int sum = 0;
    	struct node *addition;
    	
    	while ((first != NULL && second != NULL)) {
    		sum = first->data + second ->data;
    		create_list(&addition, sum);
    		first->prev;
    		second->prev;
    	}
    	
    	/* printf("%d", addition->data); */
    	
    
    }
    Thing is, I had no problem with creating the list but I think I'm not getting the concept yet. And I don't know any good books that teach linked lists well enough. The K&R book just discusses it, the Steve Oulline book isn't helpful and I don't know any others. Recommendations?

    Also, yeah I know I'm trying to pass it an integer when it asks for a string but I did it to test it I guess. I thought it would still work. [/dumb]
    Also, I haven't factored in the carry yet. That's easy once I've figured out how to actually make this work.
    Last edited by Watabou; 04-29-2011 at 10:42 AM.

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I think you have two main problems:

    1) You don't really understand what pointers are or at least how to use them properly. Referencing and dereferencing are a complete blur in your head and that is obvious by reading your code.

    You can work on this problem by reading the Pointer tutorial on this site or any other similar one.

    2) You are jumping ahead of the game here trying to code something without knowing the algorithm your code is going to implement. In other words, figure out what the code should do, and explain it in English (i.e. I iterate the nodes in the list, I select the current node, etc.) After that you are going to have a much easier time implementing it, because you will KNOW what you want to do before jumping to do it.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Me again! This time adding to the linked list
    By Welshy in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2005, 08:10 AM
  2. Adding to linked list from external file
    By cghv in forum C Programming
    Replies: 1
    Last Post: 03-09-2005, 02:05 PM
  3. Logic problem adding nodes to linked list
    By SeanMSimonsen in forum C++ Programming
    Replies: 0
    Last Post: 04-02-2003, 07:04 PM
  4. Adding To The Middle Of A Linked List
    By LostNotFound in forum C++ Programming
    Replies: 1
    Last Post: 02-23-2003, 06:02 PM
  5. Adding a node to a linked list
    By slopeski007 in forum C Programming
    Replies: 2
    Last Post: 02-02-2003, 12:31 AM