can anyone help me get an idea on how to add or subtract numbers in linked list
for example i input the number 1,3,5 in order
the display will 135
then i input the plus sign(+)
then the input will 45
it will always add the 1st and 2nd number from the left
here's my work so far:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
#include <string>
using namespace std;
struct node{
string o;
int data;
struct node *link;
};
typedef struct node *nodepointer;
nodepointer add(nodepointer &head, int num);
void del(nodepointer &head);
nodepointer ex(nodepointer head);
void display(nodepointer head);
int main()
{
nodepointer head=NULL;
int a,num,sum=0;
string o;
do{
system("CLS");
printf("[1]Add a number in front \n");
printf("[2]Mathematical Operation \n");
printf("[3]Display \n");
printf("[0]Exit\n");
printf("Enter your choice: ");
scanf("%i",&a);
switch(a)
{
case 1:
printf("Enter a number: ");
scanf("%i",&num);
head = add(head,num);
getch();
break;
case 2:
printf("Enter an operator: ");// +,-,*,/
scanf("%s",&o);
head = add(head,num);
getch();
break;
case 3:
display(head);
getch();
break;
case 0:
break;
}
}while(a!=0);
getch();
}
nodepointer add(nodepointer &head, int num)
{
nodepointer newnode;
newnode=(nodepointer)malloc(sizeof(struct node));
newnode->data=num;
newnode->link=head;
head=newnode;
return head;
}
/*
nodepointer ex(nodepointer head)
{
if(head == NULL)
{
printf("Invalid");
}
else
{
while(head->link!=NULL)
{
head+= newnode;
}
}
}
*/
void display(nodepointer head)
{
if(head==NULL)
{
printf("\n no data");
}
else
{
while(head->link!=NULL)
{
printf("%i",head->data);
head=head->link;
}
printf("%i",head->data);
}
}