C++ Simple Linked List with Enqueue, Dequeue, and Display, Exercises of Data Structures and Algorithms

The c++ code for implementing a simple linked list with the ability to enqueue, dequeue, and display elements. The code includes a struct definition for a student with id and name fields, a main function with a while loop for user input, and switch statements for each function. The user can interact with the list by pressing keys corresponding to the desired function. This document could be useful for students studying data structures and algorithms, particularly linked lists, in a computer science or engineering program.

Typology: Exercises

2011/2012

Uploaded on 07/30/2012

dhanvantari
dhanvantari 🇮🇳

2

(2)

45 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 enque press 1:";
cout<<"\n To deque press 2:";
cout<<"\nFor display press3";
cout<<"\nTo exit press4:" ;
cout<<"\n";
cin >> b;
switch (b)
{
//******insert in end*****
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 start
case 2:
docsity.com
pf2

Partial preview of the text

Download C++ Simple Linked List with Enqueue, Dequeue, and Display 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 enque press 1:"; cout<<"\n To deque press 2:"; cout<<"\nFor display press3"; cout<<"\nTo exit press4:" ; cout<<"\n"; cin >> b; switch (b) { //******insert in end***** 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 start case 2:

docsity.com

struct stu *p; p=first; if(first==last) { cout<<"\nque is empty\n"; break; } else { if(first->next==0) { delete(p); } else { p=p->next; first->next=0; delete(first); first=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\n"; p = p -> next; } break; } //*****exit case 4: { exit(0); break; } } getch(); } }

docsity.com