Share
Explore BrainMass

Add Function for Long Integer Linked List

Long interger linked list add function

Posting I am working on program assignment in structures (c language)after entering the required intergers the add function will not compile.I receive error messages I am using a Microsoft c, c++ 6.0 compiler. The help required is to provide a working add function in c. Hopefully after that I will be be able to write similar functions to subtract multiply and divide.

The given problem requires that an add function be provided that should add two given lists n and m.

The source code of the problem is attached

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node
{
int data; // char field data
struct node* next; //pointer field next
}NODE,*NODEP,**NODEPP; //defines three common types

// Function prototypes

void addhead(NODEPP listp,int value);

void getinput1( NODEPP );

void getinput2( NODEPP );
void showtext ( char* );
void showlist( char*,NODEP);
void add_list( listp );
void add();
void subtract();
void multiply();
void divide();
void main (void)
{

char cmd;

showtext(" Enter a to add, s to subtract, m to multiply , d to divide,q when finished");
for (;;)
{
scanf("%c",&cmd);

switch(cmd)
{
case 'a': add(); //addition

break;
case 's': subtract(); // subtract
break;
case 'm':multiply(); //multiply
break;
case 'd':divide(); //divide
break;
case 'q':
exit(0);
break;
}
showtext("nEND OF RUNn");
}

}// end of main

// Adds new node at the heaad of the list. puts value in the data field

void addhead(NODEPP listp, int value)
{

NODEP p = (NODEP)malloc( sizeof (NODE));
p->data = value; // Data Field gets value
p->next = *listp; // links p node to old head
*listp = p; // makes p a new head node
}

void getinput1( NODEPP listp ) //creats a linked list containing numbers
{ // entered by the user.(uses add_to_list)

int n;

printf("Enter a series of integers (! when finished ):");

while((scanf("%d",&n))==1)

addhead(listp,n); //Add new node to list

}

void getinput2( NODEPP listp) //creats a linked list containing numbers
{ // entered by the user.(uses add_to_list)

int m;

printf("Enter a second series of integers (! when finished ):");

while((scanf("%d",&m))==1)

addhead(listp,m); //Add new node to list

}

void showtext ( char* string ) //Displays text
{

printf(string);

}

void showlist(char* string, NODEP list) // Prints list to screen
{

int i =1;
showtext(string);
while (list !=NULL)
{
printf( "%5d",list->data);
if(i++% 10 ==0)showtext("n");
list =list->next;
}
showtext("n END OF LISTn");

}

void add(NODEPP listp ) //add function
{
NODEP n= NULL;
NODEP m =NULL;
NODEP t= NULL;

int tPtr;
tPtr=&t;
showtext( "**INITIAL LIST DATA**n");

getinput1( &n);
showlist("List 1n",n);
getchar();

getinput2( &m);
showlist("List 2n",m);

getchar();

while(t!=0)
{
t->data= n->data+m->data;
}

showlist( "The sum of List 1 and List 2 is:n",t);
}
void subtract(NODEPP listp)

{

}

void multiply()
{

}

void divide()
{

}

Attachments

Solution Preview

The given problem requires that an add function be provided that should add two given lists n and m. Please verify that the following steps are done in order to achieve the desired result.

1. getinput1() is used to create the linked list of the first series of integers n.
2. getinput2() is used to create the linked list of the second series of integers m.
3. for each node (i.e each integer) of the first linked list n, create a new node of another list t, such that t->data = n->data + ...

Solution Summary

The given problem requires that an add function be provided that should add two given lists n and m. Also use it to subtract, multiply and divide, exactly the same way, only in division check that you do not divide by zero.

$2.19