Hi this is RPN i would like to ask why the free(after) does not remove the after..im still confuse but whenever i print the value of after it is the last node but when im trying to remove it doesn't.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define p printf
#define s scanf
#include<conio.h>
#include<windows.h>
struct node
{
int data;
struct node *link;
};typedef struct node *nodepointer;
void delete_at_end(nodepointer &head)
{
nodepointer before,after;
if(head==NULL)
p("\nLinked list is empty!..Cannot delete");
else
{
before=NULL;
after=head;
while(after->link!=NULL)
{
before=after;
after=after->link;
}
if(head->link==NULL)
head=NULL;
else
before->link=NULL;
free(after);
}
}
void push(nodepointer &top,int number)
{
nodepointer newnode,temp;
newnode=(nodepointer)malloc(sizeof(struct node));
newnode->data=number;
newnode->link=NULL;
if(top==NULL)
{
top=newnode;
}
else
{
temp=top;
while(temp->link!=NULL)
temp=temp->link;
temp->link=newnode;
}
}
void display(nodepointer top)
{
int index=1;
if(top==NULL)
{
p("\nNothing to display");
}
else if(top!=NULL)
{
while(top->link!=NULL)
{
p("Input %i: %i\n",index,top->data);
top=top->link;
index++;
}
p("Input %i: %i",index,top->data);
index++;
}
}
void pop_add(nodepointer top)
{
nodepointer after;
nodepointer before;
if(top==NULL || top->link==NULL)
{
p("List is empty or must have two operands first before inserting operator...");
getch();
}
else
{
before=NULL;
after=top;
while(after->link!=NULL)
{
before=after;
after=after->link;
}
before->data=before->data+after->data;
before->link=NULL;
free(after);
}
}
main()
{
nodepointer top;
top=NULL;
int number;
char choice;
char operators;
char str[20];
do
{
getch();
system("cls");
p("\n[1]Push");
p("\n[2]Display");
p("\nKey choice: ");
fflush(stdin);
s("%c",&choice);
switch(choice)
{
case '1':
p("\nEnter number: ");
fflush(stdin);
gets(str);
number=atoi(str);
if(strcmpi(str,"+")==0)
{
pop_add(top);
}
push(top,number);
break;
case '2':
display(top);
break;
case '3':
delete_at_end(top);
break;
}
}while(1==1);
system("pause");
}
ilovephil 0 Light Poster
savoie 0 Newbie Poster
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.