Lab Exercise 3 Linked List, Stack and Queue
Lab Exercise 3 Linked List, Stack and Queue
LAB EXERCISE 3
Objective:
To develop C++ program which apply lined list, stack and queue concept.
Learning outcomes:
End the end of the lab session, student should be able to:
1. Apply linked list, stack and queue concept in solving computing problem.
Task 1
Consider the linked list of alphabet represented by the following diagram and the
node structure in Figure 1. Assume that the linked list will store the alphabet in
ascending order. first and last are pointers of type node where first will
always pointed to the first node in the linked list and last will always pointed to
the last node in the linked list.
first last
struct node
{
char alphabet;
struct node *next;
};
You are required to write a complete C++ program to get a list of alphabet from
the user and store into the linked. Each time user enter an alphabet make sure
your program is able to insert the alphabet into the correct position in the linked
list. Remember, the linked list is used to store a list of alphabet in ascending
order.
(25 marks)
#include <iostream>
using namespace std;
struct node
{
char alphabet;
struct node* next;
};
void createList()
{
// to create list
first = NULL;
last = NULL;
}
void insertNewNode()
{
int answer;
do {
{
first = newNode;
}
else {
last->next = newNode;
last = newNode;
cout << "Do You Want to Add Another Alphabet? Press 1 for YES, 0 for NO: ";
cin >> answer;
cout << endl;
void displayList()
{
//traversal
node* temp;
temp = first;
}
else {
cout << "List of All Alphabet Inserted: " << endl;
while (temp != NULL)
{
cout << temp->alphabet;
temp = temp->next;
if (temp != NULL)
cout << ", ";
}
cout << endl;
}
system("pause");
system("cls");
int main()
{
int operation;
createList();
do {
cout << "-: MAIN MENU :-" << endl;
cout << "1. Add Alphabet" << endl;
cout << "2. View list" << endl;
cout << "0. End Program" << endl;
switch (operation)
case 1: insertNewNode();
break;
case 2: displayList();
break;
return 0;
}
Sample Output
Task 2
You are about to create a list of student’s records by applying queue concept.
The queue will be implemented using linked list. Write a complete C++ program
to read student’s name and her/his body height in centimeter (cm) and store into
a queue. The program will keep repeating the input process as long as the user
have record to be input. Then the program will find and display the student name
with the highest height.
(25 marks)
#include <iomanip>
#include<iostream>
using namespace std;
struct student
{
char name[15];
int height, i = 0;
struct student* next;
};
struct headStructure {
headStructure queue;
student* newStudent;
// for traversal
student* temp;
void enqueue() {
int answer, j = 0;
do {
// to create a new node
newStudent = new student;
{
queue.front = newStudent;
}
else {
queue.rear->next = newStudent;
}
queue.rear = newStudent;
cout << "Add another student? Press 1 for YES and 0 for NO: ";
cin >> answer;
cout << endl;
system("pause");
system("cls");
int highest;
string highestStudent;
if (queue.front == NULL) {
else {
temp = queue.front;
while (temp != NULL) {
if (temp->i == 1) {
highestStudent = temp->name;
highest = temp->height;
}
temp = temp->next;
cout << left << setw(20) << "Student's Name" << setw(15) << "Body Height
(cm)" << endl;
cout << setw(20) << highestStudent << setw(15) << highest << endl;
}
}
int main() {
int operation;
case 2: tallest();
break;
default: cout << "Invalid Operation, Please Select a Valid Operation." <<
endl;
system("pause");
system("cls");
return 0;
Sample Output