0% found this document useful (0 votes)
163 views

Lab Exercise 3 Linked List, Stack and Queue

This document describes a lab exercise involving linked lists, stacks, and queues. The objectives are to develop C++ programs applying these data structures. For task 1, students are asked to write a program to insert alphabetic characters into a linked list in ascending order. For task 2, students must create a program to store student records in a queue, with name and height, and find the tallest student. The document provides code templates to help complete the tasks.

Uploaded by

Zhen Wei
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views

Lab Exercise 3 Linked List, Stack and Queue

This document describes a lab exercise involving linked lists, stacks, and queues. The objectives are to develop C++ programs applying these data structures. For task 1, students are asked to write a program to insert alphabetic characters into a linked list in ascending order. For task 2, students must create a program to store student records in a queue, with name and height, and find the tallest student. The document provides code templates to help complete the tasks.

Uploaded by

Zhen Wei
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

CCS20503 / DCS20303 / TCS21203 Lab Exercise 3

LAB EXERCISE 3

LINKED LIST, STACK AND QUEUE

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

‘A’ ‘B’ ‘E’ ‘G’ ‘Z’

struct node
{
char alphabet;
struct node *next;
};

Figure 1: Linked list and node structure definition

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)

IDHAM IZZUDIN BIN SHAMSUL BAHRI (012019070072)


CCS20503 / DCS20303 / TCS21203 Lab Exercise 3

Source Code for Question 1

#include <iostream>
using namespace std;

struct node
{
char alphabet;
struct node* next;
};

// to hold first node


node* first;
// to hold second node
node* last;

// to create new node


node* newNode;

void createList()
{
// to create list
first = NULL;
last = NULL;
}

void insertNewNode()
{
int answer;
do {

//to create a new node


newNode = new node;

// to get data from user input


cout << "Please Enter an Alphabet: ";
cin >> newNode->alphabet;

// to set link of the new node to NULL


newNode->next = NULL;

// to add new node to the list


// to check list current status
if (first == NULL)

{
first = newNode;
}
else {

last->next = newNode;

IDHAM IZZUDIN BIN SHAMSUL BAHRI (012019070072)


CCS20503 / DCS20303 / TCS21203 Lab Exercise 3

last = newNode;

cout << "Do You Want to Add Another Alphabet? Press 1 for YES, 0 for NO: ";
cin >> answer;
cout << endl;

} while (answer == 1);


system("cls");
}

void displayList()

{
//traversal
node* temp;
temp = first;

// to check and inform is queue is empty


if (first == NULL) {
cout << "The List is Empty.\n" << endl;

}
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;

cout << "Please Enter your selection for operation: ";


cin >> operation;
system("cls");

IDHAM IZZUDIN BIN SHAMSUL BAHRI (012019070072)


CCS20503 / DCS20303 / TCS21203 Lab Exercise 3

switch (operation)

case 1: insertNewNode();
break;

case 2: displayList();
break;

case 0: cout << "Exiting Program...";


break;

default: cout << "Invalid opetion, please select a valid


operation.";
}
} while (operation != 0);

return 0;
}

Sample Output

IDHAM IZZUDIN BIN SHAMSUL BAHRI (012019070072)


CCS20503 / DCS20303 / TCS21203 Lab Exercise 3

IDHAM IZZUDIN BIN SHAMSUL BAHRI (012019070072)


CCS20503 / DCS20303 / TCS21203 Lab Exercise 3

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)

Source Code for Task 2

#include <iomanip>
#include<iostream>
using namespace std;

struct student
{
char name[15];
int height, i = 0;
struct student* next;
};

struct headStructure {

struct student* front;


struct student* rear;
};

headStructure queue;
student* newStudent;
// for traversal
student* temp;

void enqueue() {
int answer, j = 0;
do {
// to create a new node
newStudent = new student;

// to get data input from user


cout << "Enter students's name: ";
cin >> ws;
cin.getline(newStudent->name, 20);
cout << "Enter student's height: ";
cin >> newStudent->height;
j++;
newStudent->i = j;
newStudent->next = NULL;

if (queue.front == NULL) //when queue is empty

IDHAM IZZUDIN BIN SHAMSUL BAHRI (012019070072)


CCS20503 / DCS20303 / TCS21203 Lab Exercise 3

{
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;

} while (answer == 1);

system("pause");
system("cls");

// to search and find the tallest student within the queue


void tallest() {

int highest;
string highestStudent;

if (queue.front == NULL) {

cout << "Queue is empty. There is nothing to show.\n" << endl;

else {

temp = queue.front;
while (temp != NULL) {

if (temp->i == 1) {
highestStudent = temp->name;
highest = temp->height;
}

else if (highest < temp->height) {


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;

}
}

IDHAM IZZUDIN BIN SHAMSUL BAHRI (012019070072)


CCS20503 / DCS20303 / TCS21203 Lab Exercise 3

int main() {

int operation;

// selection of options that user can choose


do {
cout << " -: MAIN MENU :- " << endl;
cout << "1 - Add Data of Students" << endl;
cout << "2 - View The Tallest Student " << endl;
cout << "0 - Exit Program " << endl;
cout << "Please Select Your Operation: ";
cin >> operation;
system("cls");
switch (operation)
{
case 1: enqueue();
break;

case 2: tallest();
break;

case 0: cout << "Exiting Program..." << endl;


break;

default: cout << "Invalid Operation, Please Select a Valid Operation." <<
endl;
system("pause");
system("cls");

} while (operation != 0);

return 0;

IDHAM IZZUDIN BIN SHAMSUL BAHRI (012019070072)


CCS20503 / DCS20303 / TCS21203 Lab Exercise 3

Sample Output

Add student name and height.

View the Tallest Students.

IDHAM IZZUDIN BIN SHAMSUL BAHRI (012019070072)


CCS20503 / DCS20303 / TCS21203 Lab Exercise 3

Use Previous Submission Template file for submission.


Answer for each Task must start at new page.
Make sure in the footer section you write your full name.

Submission Date: 7 May 2020


Submit to me through Google Classroom

Your Task will be evaluated using the following scheme:


Task 1 – 25 marks
Task 2 – 25 marks
Time line of submission – 10 marks

IDHAM IZZUDIN BIN SHAMSUL BAHRI (012019070072)

You might also like