Computer Science Homework Solutions
Problem
#11207

Write a function to add Long Integers in a 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
#include
#include

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()
{





}

Attached file(s):
Attachments
Linked list calculator.doc  View File

Attachment Content Summary (Note: view attachment at the above link before purchasing. Actual attachment content may vary slightly from that shown below.)

Linked list calculator.doc
#include

#include

#include

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 RUN\n");

}



}// 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 LIST\n");



}



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 1\n",n);

getchar();



getinput2( &m);

showlist("List 2\n",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()

{

}

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.

Solution
What is this?
By OTA - Overall OTA Rating
Purchase Cost Now
$2.19 CAD
Included in Download
  • Plain text response
  • Attached file(s):
    • 11186.C
$2.19 Instant Download
Add to Cart
Why you can trust BrainMass.com
  • Your Information is Secure
  • Best Online Academic Help Service
  • Students find real academic Success
Related Solutions
Browse