Programming Language:C++
Could someone give me a quick example of implementing a stack<string> into a double link list with an explanation. Also if possible how to be able to add, remove, or modify the strings of the stack inside the link list thank you
Implemetation of Stack using double link list.
Please hit that like button or thumbs-up button to motivate me.(It really helps, just take 1-2 seconds hope you will do that)>3
Thank you!!
I have done this implementation with add, remove, or display.
Stack.cpp
#include<iostream>
#include <stdio.h>
#include<stdlib.h>
using namespace std;
struct node
{
int num;
struct node *left;
struct node *right;
};
struct node* receivenode( )
{
return ( struct node *)malloc( sizeof( struct node ));
};
struct node* top; int c=0;
struct node* add( struct node *);
struct node* del( struct node *);
void display( struct node *);
int main()
{
struct node *A;
int ch;
A = NULL; top=A;
while( 1 )
{
// User menu to show what to do
printf("1.Add elements\n");
printf("2.Delete element\n");
printf("3.Display\n");
printf("4.Exit \n");
printf("Enter your Choice:");
scanf("%d", &ch );
if( ch == 1 )
A = add ( A );
else if( ch == 2 )
A=del(A);
else if(ch==3)
display( A );
else if( ch == 4 )
exit(0);
}
}
// Adding numbers in stack using Double Linked list
struct node* add( struct node *START)
{
struct node *B;
if( START == NULL )
{
START = receivenode();
B =top=START;
B->left=NULL;
}
else
{ B=receivenode();
top->right=B; B->left=top;
top=B;
}
printf("Enter a number to add:\n"); c++;
scanf("%d",&B->num );
B->right= NULL;
return START;
}
// Deleting numbers in stack using Double Linked list
struct node* del( struct node *START)
{
struct node *B;
if(( START== NULL)&&(c<1))
{
printf("Underflow\n");
return START;
}
B=top;
printf("The deleted number is : %d\n",B->num);
if(c!=1)
{top=B->left;
B->left= NULL; top->right=NULL;}
else if(c==1)
{START=NULL;}
c--;
return START;
}
// Display numbers which available in stack
void display( struct node *B)
{ if(B==NULL)
{
printf("Nothing Found!\n");
return;
}
while( B != NULL )
{
printf("%d ",B->num);
B = B->right;
}
printf("\n");
}
Output:-
Please refer to screenshot
Again,Please hit that like button.
Thank you!!
Get Answers For Free
Most questions answered within 1 hours.