Linked Lists Integer addition ? HELP Please??

This is a discussion on Linked Lists Integer addition ? HELP Please?? within the C Programming forums, part of the General Programming Boards category; I'm still very new to programming and I've never done linked lists before. Here's the assignment: http://www.geocities.com/blueninja83/hugeintegers.html /* Note there ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    Linked Lists Integer addition ? HELP Please??

    I'm still very new to programming and I've never done linked lists before. Here's the assignment: http://www.geocities.com/blueninja83/hugeintegers.html /* Note there picture of the reversed linked list is missing - it's suppose to have the above number (say it was 594) in boxes (representing the link list ) it the order 495 (backwards) so that 5 is next to the null character meaning it is your head at the end. */
    I can't figure out how to just add single digits in a linked list. Like there are two linked lists and I can't figure out how to add one from one list to another from another list. I use a char data type because I heard from someone that that was the only way to read a huge number one digit at a time. My program compiles but gives a segmentation fault (which I think is caused by a pointer not pointing to the correct item or somthing?). Here's the code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
       struct integerNode{
    	char value;
    	struct integerNode *next;
    	};
    
    	typedef struct integerNode ELEMENT;    /* Just easily refrences the structure */
    	typedef struct integerNode *integer;   /* Used to build up the linked list */
    
    
    
    integer ReadInteger();
    void PrintInteger(integer n);
    int compare(integer n1, integer n2);
    integer AddTwoIntegers(integer n1, integer n2);
    integer SubtractIntegers(integer n1, integer n2);
    integer RecycleInteger(integer n);
    
    
    
    
    
    main(void)
    {
    	ELEMENT *n1;  /* The first large integer */
    	ELEMENT *n2;  /* The second large integer */
    	ELEMENT *n;  /* The answer after the addition or subtraction */
    	int i=0;
    
    
    
    
    	while(i!=3)
    	{
    	printf("1)Add\n2)Subtract\n3)Quit\n");
    	printf("Please enter the number of the option you wish to perform\n");
    	scanf("%d", &i);
    	  getchar();
       switch(i)
    	{
    	case 1:
    	printf("Please enter your first of two large integers\n");
    	n1=ReadInteger();
    	printf("Please enter your second of two large integers\n");
    	n2=ReadInteger();
    	AddTwoIntegers(n1, n2);
    	PrintInteger(n);
    	case 2:
    	SubtractIntegers(n1, n2);
    	case 3:
    	exit(0);
    	}
         }
    }
    
    integer ReadInteger( )
    {
    
    char ch;
    ELEMENT *head = NULL;       /* The head of the linked list always has a null char hence the null*/
    ELEMENT *new;		/* A pointer to the linked list */
    
    	while((ch=getchar())!='\n')
    	{
    	if(   (ch>='0' ) && (ch<='9') ){
    	new=malloc(sizeof(ELEMENT));
    	new->value=ch;
    	new->next=head;
    	head=new;
    	  }
    	}
    	return head;
    	printf("\n");
    }
    
    integer AddTwoIntegers(integer n1, integer n2)
    {
    int val1, val2, total;
    ELEMENT *new;   /*A pointer structure for the an answer slot/node */
    ELEMENT *head;  /* A node for the head of the answer link list */
    
    	while((n1!=NULL) && (n2!=NULL))
    	{
    	new=malloc(sizeof(ELEMENT));
    	val1=n1->value-48;
    	val2=n2->value-48;
    	total=val1+val2;
    
    	new->value=total;
    	new->next=head;
    	head=new;
    	n1=n1->next;
    	n2=n2->next;
    	}
    /*printf("%d", total); */
    }
    
    
    integer SubtractIntegers(integer n1, integer n2)
    {
    
    }
    
    integer RecycleInteger(integer n)
    {
    
    }
    
    
    void PrintInteger(integer n)
    {
    
    if(n->next!=NULL)
    {
    PrintInteger(n->next);
    printf("%c", n->value);
    }
    else
    printf("%c", n->value);
    
    
    }
    Any help would be greatly appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Appending or prepending lists is easy. I really prefer double linked lists, but for now I'll just use single.

    First, make a function that returns the last node in a list. I'll do an easy version:
    Code:
    struct node * GetLast( struct node * n )
    {
        struct node *e, *x;
    
        for( e = n; e && e->next; e = e->next );
    
        return e;
    }
    Assume that the pointers point to the first node in a list.

    Prepend ListA to ListB:
    Code:
    struct node *ListA, *ListB;
    struct node *temp;
    
    temp = GetLast( ListA );
    if( temp )
        temp->next = ListB;
    Now the last node in ListA points to the first in ListB.

    Assume that the pointers point to the first node in a list.

    Append ListA to ListB:
    Code:
    struct node *ListA, *ListB;
    struct node *temp;
    
    temp = GetLast( ListB );
    if( temp )
        ListB->next = ListA;
    Pretty simple stuff.

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

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    5
    No, I'm not trying to append a link list. I'm trying to take the contents of one link list node and the contents of a different link list (node) and place that answer from those 2 nodes in their respective linked lists into another linked list.

    Here's the specs for the add function:

    /*

    * Function: AddTwoIntegers

    * Usage: result = AddTwoIntegers(n1, n2);

    * ---------------------------------------

    * The function gets two integers represented as linked

    * lists and adds them up and allocates and returns the

    * resulting integer.

    */

    I don't think the specs allow me to append one of the linked lists.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, so what's the problem? Assume the following:

    nodeA has the first value we want.
    nodeB has the second value we want.
    Code:
    struct node
    {
        struct node *next;
        int data;
    }
    
    struct node *nodeA, *nodeB, *nodeC;
    
    nodeC = add( nodeA, nodeB );
    
    ... stuff ...
    
    struct node *add( struct node *a, struct node * b )
    {
        struct node* c = malloc( sizeof (struct node) );
        c->data = a->data + b->data;
        return c;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly linked lists
    By mohanlon in forum C Programming
    Replies: 8
    Last Post: 12-08-2010, 01:01 AM
  2. Freeing Linked Lists
    By mrpickle in forum C Programming
    Replies: 4
    Last Post: 01-21-2004, 07:57 PM
  3. Questions on Linked Lists
    By Weng in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2003, 01:17 AM
  4. One more question about linked lists?
    By shaeng in forum C Programming
    Replies: 2
    Last Post: 06-24-2003, 08:36 AM
  5. Linked Lists -- Again!!! Help!!
    By Leeman_s in forum C++ Programming
    Replies: 4
    Last Post: 01-22-2002, 08:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21