Please....I need help with this.... in C++ *************************************
ID: 3798647 • Letter: P
Question
Please....I need help with this.... in C++
****************************************************************
Test it by creating a DRIVER/Test file with int main, and
Test the stack class and its basic functionality.
Program 3
Write a stack for string data with pointers and linked lists.
Hint: Add(push) the new element between the head and the first link.
Remove(pop) the first element..
Stop when head = null.
Test with 10 words... able, bread, corn, dog, elephant, frog, ground, House, Italy, Jam
Test all Functionality.
Explanation / Answer
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
struct Node
{
char *data;
struct Node *next;
}*top = NULL;
void pushString(char value[10])
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf(" Insertion is Success!!! ");
}
void pop()
{
if(top == NULL)
printf(" Stack is Empty!!! ");
else{
struct Node *temp = top;
printf(" Deleted element: %s", temp->data);
top = temp->next;
free(temp);
}
}
void display()
{
if(top == NULL)
printf(" Stack is Empty!!! ");
else{
struct Node *temp = top;
while(temp->next != NULL){
printf("%s--->",temp->data);
temp = temp -> next;
}
printf("%s--->NULL",temp->data);
}
}
int main()
{
int choice;
char value[10];
printf(" :: Stack using Linked List :: ");
while(1){
printf(" ****** MENU ****** ");
printf("1. Push 2. Pop 3. Display ");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%s", &value);
pushString(value);
break;
case 2: pop(); break;
case 3: display(); break;
// case 4: exit(0);
default: printf(" Wrong selection!!! Please try again!!! ");
}
}
}