How to add the numbers between nodes
example. input 1*2+3 first 2 and 3 will add and it will become 1 5 nodes then 1 * 5 = 5 then node will becaome 5 only
this is my sample program

#include<stdio.h>
#include<stdlib.h>
#define p printf
#define s scanf
struct node
{
       int data;
       struct node *link;
};typedef struct node *nodepointer;
void push(nodepointer &top,int num)
{
     nodepointer newnode;
     newnode=(nodepointer)malloc(sizeof(struct node));
     newnode->data=num;
     newnode->link=top;
     top=newnode;
}
void display(nodepointer top)
{
if(top==NULL)
{
p("\nNothing to display");
}
else if(top!=NULL)
{
while(top->link!=NULL)
{
p("\n%i",top->data);
top=top->link;
}
p("\n%i",top->data);
}
}
void pop(nodepointer top)
{
     int sum;
     nodepointer after;
     nodepointer before;
     if(top==NULL)
     p("\nList is empty...");
     else
     {
         while(after->link!=NULL)
         {
         before=after;
         after=after->link;
         sum=before->data+after->data;
         }
     }
}
main()
{
      nodepointer top;
      top=NULL;
      int number;
      int choice;
      char operators;
      do
      {
          p("\n[1].Push");
          p("\n[2].Display");
          p("\n[3].Add operator");
          p("\nKey choice: ");
          s("%i",&choice);
          switch(choice)
          {
      case 1:
           p("\nEnter number: ");
           s("%i",&number);
           push(top,number);
           break;
      case 2:
           display(top);
           break;
      case 3:
            p("\nenter operator");
            fflush(stdin);
            s("%c",&operators);
            if(operators=='+')
            pop(top);
            break;
           }
           }while(1==1);
           system("pause");
           }
Member Avatar for begame

Well, I think you may want to add the numbers of the last two nodes only at a time. If that is so, when you are popping the elements, you are not adding just the last two elements but much much more. So first you should go to the second last element in you list, and then add the data in the 2nd last element and the last element. Moreover you may set the pointer of the 3rd last element to a new node containing the sum of the data of the last two nodes. And you must free the memory allocated to the last two nodes.(Alternatively you could just simple change the data of the 2nd last node to the sum and set it's pointer to null. But only after deallocating the memory of the last node.)

Come back if you have more problems :)(but remember you learning is the most important thing, so try to do everything on your own to maximize learning).
Regards,
Digvijay

How to add the numbers between nodes
example. input 1*2+3 first 2 and 3 will add and it will become 1 5 nodes then 1 * 5 = 5 then node will becaome 5 only
this is my sample program

#include<stdio.h>
#include<stdlib.h>
#define p printf
#define s scanf
struct node
{
       int data;
       struct node *link;
};typedef struct node *nodepointer;
void push(nodepointer &top,int num)
{
     nodepointer newnode;
     newnode=(nodepointer)malloc(sizeof(struct node));
     newnode->data=num;
     newnode->link=top;
     top=newnode;
}
void display(nodepointer top)
{
if(top==NULL)
{
p("\nNothing to display");
}
else if(top!=NULL)
{
while(top->link!=NULL)
{
p("\n%i",top->data);
top=top->link;
}
p("\n%i",top->data);
}
}
void pop(nodepointer top)
{
     int sum;
     nodepointer after;
     nodepointer before;
     if(top==NULL)
     p("\nList is empty...");
     else
     {
         while(after->link!=NULL)
         {
         before=after;
         after=after->link;
         sum=before->data+after->data;
         }
     }
}
main()
{
      nodepointer top;
      top=NULL;
      int number;
      int choice;
      char operators;
      do
      {
          p("\n[1].Push");
          p("\n[2].Display");
          p("\n[3].Add operator");
          p("\nKey choice: ");
          s("%i",&choice);
          switch(choice)
          {
      case 1:
           p("\nEnter number: ");
           s("%i",&number);
           push(top,number);
           break;
      case 2:
           display(top);
           break;
      case 3:
            p("\nenter operator");
            fflush(stdin);
            s("%c",&operators);
            if(operators=='+')
            pop(top);
            break;
           }
           }while(1==1);
           system("pause");
           }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.