Google PlusFacebookTwitter

Queues : Implementation Using Linked Lists

By on Dec 15, 2007 in On A Whim | 2 comments

Share On GoogleShare On FacebookShare On Twitter

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html */

/* Linked List implementation of a Queue */

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <process.h>

class student
{
int rno;
char nm[42];
float perc;
public:
student *next;
void getdata()
{
cout<<endl<<“Enter roll number: “;
cin>>rno;
cout<<“Enter name: “;
gets(nm);
cout<<“Enter percentage: “;
cin>>perc;
next = NULL;
}
void showdata()
{
cout<<endl<<“Roll number: “<<rno
<<endl<<“Name: “<<nm
<<endl<<“Percentage: “<<perc<<endl;
}
};

void main()
{
int ch;
char choice;
student *newptr, *front = NULL, *rear = NULL, *ptr;
do
{
clrscr();
cout<<endl<<“Program Menu”
<<endl<<“1. Push (insert)”
<<endl<<“2. Pop (delete)”
<<endl<<“3. Traverse (show whole list)”
<<endl<<“4. Exit program”
<<endl<<“Enter your choice: “;
cin>>ch;
switch(ch)
{
case 1 : clrscr();
newptr = new student;
if (!newptr)
{
cout<<endl<<“Memory allocation error! Exiting program”;
exit(0);
}
newptr->getdata();
if (front == NULL)
{
front = newptr;
rear = newptr;
}
else
{
rear->next = newptr;
rear = newptr;
}
break;
case 2 : if (front == NULL)
cout<<endl<<“Error! Queue does not exist!”;
else
{
if (front == rear)
{
ptr = front;
front = NULL;
rear = NULL;
}
else
{
ptr = front;
front = front->next;
}
cout<<endl<<“Showing details of deleted element: “;
ptr->showdata();
delete ptr;
}
break;
case 3 : ptr = front;
if (front == NULL)
cout<<endl<<“Error! Queue does not exist!”;
else
{
while (ptr != NULL)
{
ptr->showdata();
ptr = ptr->next;
}
}
break;
case 4 : cout<<endl<<“Exiting program”<<endl;
exit(0);
default : cout<<endl<<“You entered an invalid choice!”;
}
cout<<endl<<“Do you wish to continue? (y/n): “;
cin>>choice;
}
while (choice == ‘Y’ || choice == ‘y’);
}

/* Output */

Program Menu
1. Push (insert)
2. Pop (delete)
3. Traverse (show whole list)
4. Exit program
Enter your choice: 1

Enter roll number: 42
Enter name: Ankur Banerjee
Enter percentage: 42

Do you wish to continue? (y/n): y

Program Menu
1. Push (insert)
2. Pop (delete)
3. Traverse (show whole list)
4. Exit program
Enter your choice: 1

Enter roll number: 41
Enter name: Arthur Dent
Enter percentage: 75

Do you wish to continue? (y/n): y

Program Menu
1. Push (insert)
2. Pop (delete)
3. Traverse (show whole list)
4. Exit program
Enter your choice: 1

Enter roll number: 45
Enter name: Ford Prefect
Enter percentage: 65

Do you wish to continue? (y/n): y

Program Menu
1. Push (insert)
2. Pop (delete)
3. Traverse (show whole list)
4. Exit program
Enter your choice: 3

Roll number: 42
Name:
Ankur Banerjee
Percentage: 42

Roll number: 41
Name:
Arthur Dent
Percentage: 75

Roll number: 45
Name:
Ford Prefect
Percentage: 65

Do you wish to continue? (y/n): y

Program Menu
1. Push (insert)
2. Pop (delete)
3. Traverse (show whole list)
4. Exit program
Enter your choice: 2

Showing details of deleted element:
Roll number: 42
Name:
Ankur Banerjee
Percentage: 42

Do you wish to continue? (y/n): y

Program Menu
1. Push (insert)
2. Pop (delete)
3. Traverse (show whole list)
4. Exit program
Enter your choice: 3

Roll number: 41
Name:
Arthur Dent
Percentage: 75

Roll number: 45
Name:
Ford Prefect
Percentage: 65

Do you wish to continue? (y/n): y

Program Menu
1. Push (insert)
2. Pop (delete)
3. Traverse (show whole list)
4. Exit program
Enter your choice: 4

Exiting program



2 Comments

  1. rach

    December 15, 2007

    Post a Reply

    Never thought that I‘d say this, but your archive (read: this program from your archive) really came in handy!!! Thanks!

  2. GQ

    December 15, 2007

    Post a Reply

    @Rach: Muhahaha, at last, you’ve been drawn to the Dark Side of the Force…

Submit a Comment

Your email address will not be published. Required fields are marked *