

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 2
This page cannot be seen from the preview
Don't miss anything!
#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;
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(); } }