Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a stack with linked lists using the Linked List implementation I supplied. For push use insert end, for pop use remove from end. //

Implement a stack with linked lists using the Linked List implementation I supplied. For push use insert end, for pop use remove from end.

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
// LinkedList.cpp : Defines the entry point for the console application. II \#include "stdafx.h" \#include \#include \#include \#include \#include "conio. h" \#include struct Node { int data; struct Node * Next; \}i//declaration of a Node struct Node *head = NULL;// two pointers to struct Node type of data //global definition of head void insertFirst(int d)// if the head was not globally defined it should be coming with parameter struct Node "hd \{ struct Node newNode =( struct Node ) malloc(sizeof(struct Node)); newNode data = d; newNode Next = head > Next; head Next = newNode; printf(")n new node inserted in the first placeln"); 3 void insertLast(int d, struct Node nnod) \{ struct Node * temp = (struct Node* ) malloc(sizeof(struct Node)); temp = head; while(temp->Next !=NULL) temp = temp > Next; I/when the loop terminates it means we found last node /ow temp is the last node with Next=null temp > Next = nnod; nnod > Next = NULL; nnod data =d; printf("|n new node inserted in the last placeln" ); 3 void insertAfterNodeWith_d(int d, int newdata) //when loop terminates we have last node in temp, the previos of last in prev prev->Next = NULL; // we do not need the last node any more free ( temp); printf("In last node is deleted (n); 3 void deleteNodeWithData(int d)//deleting the node having data d \{ struct Node *temp =( struct Node ) malloc(sizeof(struct Node)); temp = head; struct Node * prev = NULL;//to hold the node before the one with data d while (( temp->Next != NULL) &( temp > data !=d)){ prev = temp; temp = temp > Next; } //when the loop terminates it means we found last node or the node with data=d if (temp data == d) {/ now temp is the node with data=d, prev is the node before it prev->Next = temp->Next; free(temp); printf("In node with data \%d is deleted , d); 3/ we do not need the node with data =4 any more else printf("In node with data \%d could not be found In", d); 3 int searchData(int d) \{ struct Node *temp =( struct Node ) malloc(sizeof(struct Node)); temp = head; int index =0; while (( temp->Next != NULL) &( temp->data != d) ) \{temp = temp Next; index++; \} if (temp->data ==d ) \{ return index

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is Working Capital ? Explain its types.

Answered: 1 week ago