Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Implementation of a Simple Linked List Stack, Exercises of Data Structures and Algorithms

The c++ code for implementing a simple linked list stack data structure. The code includes functions for pushing, popping, displaying, and exiting the stack. Users can interactively input their choices to perform these operations.

Typology: Exercises

2011/2012

Uploaded on 07/30/2012

dhanvantari
dhanvantari 🇮🇳

2

(2)

51 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <process.h>
struct stu
{
int std_ID;
char name[20];
struct stu *next;
}
*first=0,*last=0;
void main()
{
clrscr();
int b,key;
while (1)
{
cout << "To push press 1:";
cout<<"\n To pop press 2:";
cout<<"\nFor display press 3:";
cout<<"\nTo exit press 4:" ;
cout<<"\n";
cin >> b;
switch (b)
{
//******insert in end(push)*****
case 1:
{
struct stu *current;
current = new stu;
current -> next = 0;
if ( first== 0 )
{
first= current;
last = current;
}
else
{
last -> next = current;
last = current;
}
cout << "\nStudent ID: ";
cin >> current -> std_ID;
cout << "\nInput Name: ";
gets ( current -> name );
cout<<"\n\n";
break;
}
//****delete from end (pop)******
case 2:
{
struct stu *p;
docsity.com
pf2

Partial preview of the text

Download C++ Implementation of a Simple Linked List Stack and more Exercises Data Structures and Algorithms in PDF only on Docsity!

#include <iostream.h> #include <conio.h> #include <stdio.h> #include <process.h> struct stu { int std_ID; char name[20]; struct stu *next; } first=0,last=0; void main() { clrscr(); int b,key; while (1) { cout << "To push press 1:"; cout<<"\n To pop press 2:"; cout<<"\nFor display press 3:"; cout<<"\nTo exit press 4:" ; cout<<"\n"; cin >> b; switch (b) { //******insert in end(push)***** case 1: { struct stu *current; current = new stu; current -> next = 0; if ( first== 0 ) { first= current; last = current; } else { last -> next = current; last = current; } cout << "\nStudent ID: "; cin >> current -> std_ID; cout << "\nInput Name: "; gets ( current -> name ); cout<<"\n\n"; break; } //****delete from end (pop)****** case 2: { struct stu *p;

docsity.com

p=first; if(first==last) { cout<<"\n stack is empty\n\n"; break; } else { if(first->next==0) { delete(p); } else { while(p->next->next!=0) { p=p->next; } p->next=NULL; delete(last); last=p; } } break; } //****display case 3: { struct stu *p; p = first; while ( p != 0 ) { cout << "\nStudent's ID: " << p -> std_ID << "\nStudent's Name: "; puts ( p -> name ); cout<<"\n"; p = p -> next; } break; } //*****exit case 4: { exit(0); break; } } getch(); } }

docsity.com