You are on page 1of 16

CSL-221 Data structure &

Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A

Lab 7:Implementation of Single Link list

Objective(s): Upon completion of this lab session, students will be able to:

Insert elements in a single link list


Traverse the single link list
Delete elements from single link list

Exercise 1
Write a menu driven program that implements single list with integer values and having
following functions.
a. Insert an element at beginning
b. Insert an elemt at the end of the link list
c. Insert an element at position provided by user
d. Search an element
e. Traverse the list
f. Delete an elment from beginning
g. Delete an elment from end
h. Delete an elment from position provided by user.

CODE:

#include<iostream>
using namespace std;

struct node
{
int info;
node *next;
};
node *start;

void InsertBegin(int data)


{
node* tempnode;

tempnode = new node();


tempnode->info = data;
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A

tempnode->next = start;
start = tempnode;
}

void InsertEnd(int data)


{
node *ptr,*tempnode;
ptr = start;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
tempnode = new node();
tempnode->info=data;
tempnode->next = NULL;
ptr->next = tempnode;
}

void InsertPos(int data, int pos)


{
node* tempnode, * ptr;
ptr = start;
for (int i = 1; i < pos; i++)
{
if (ptr == NULL)
{
cout << "Create the List First";
return;
}
ptr = ptr->next;
}

if(ptr->next==NULL)
{
InsertEnd(data);
}
else
{
tempnode = new node();
tempnode->info = data;
tempnode->next = ptr->next;
ptr->next = tempnode;
}
}

void PrintList()
{
node *ptr;
ptr = start;
cout << "LIST: ";
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A

while (ptr != NULL)


{
cout << ptr->info<<" ";
ptr = ptr->next;
}
cout << endl;
}

void Search(int num) {

if (start == NULL) {
cout << "\nNo List Found";
return;
}

node* temp = start;


bool found = false;
int iteration = 0;

while (temp != NULL) {


if (temp->info == num) {
found = true;
}
iteration++;
temp = temp->next;
}

if (found) {
cout << "Element Exists At: " << iteration<<endl;
}
else {
cout << "Element Not Found"<<endl;
}
}

void DeleteBegin()
{
node* temp;
temp = start;
start = temp->next;
free(temp);
}

void DeleteEnd()
{
node* ptr, * pnode=NULL;
ptr = start;
while (ptr->next != NULL)
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A

{
pnode = ptr;
ptr = ptr->next;
}
if (ptr == start)
{
start = NULL;
free(ptr);
}
else
{
pnode->next = NULL;
free(ptr);
}
}

void DeletePos()
{
int pos, i = 1;
node* temp, * temp1;
temp = start;
cout << "Enter the Position: ";
cin >> pos;
while (i < pos)
{
temp = temp->next;
i++;
}
temp1 = temp->next;
temp->next = temp1->next;
free(temp1);
}

void main()
{
int opt,data,pos;
bool terminate = false;

cout << "----------------SINGLE LINK


LIST----------------"<<endl<<endl;
cout << "1) Insert an element at beginning." << endl;
cout << "2) Insert an element at end." << endl;
cout << "3) Insert an element at user given position." << endl;
cout << "4) Search an element." << endl;
cout << "5) Traverse the list." << endl;
cout << "6) Delete an element from beginning." << endl;
cout << "7) Delete an element from end." << endl;
cout << "8) Delete an element from user given position." << endl;
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A

cout << "9) Exit." << endl;


cout << "------------------------------------------------"<<endl;

do
{
cout << endl;
cout << "Enter Operation: ";
cin >> opt;

switch (opt)
{
case 1:
{
cout << "Enter Data: ";
cin >> data;
InsertBegin(data);
break;
}
case 2:
{
cout << "Enter Data: ";
cin >> data;
InsertEnd(data);
break;
}
case 3:
{
cout << "Enter Data: ";
cin >> data;
cout << "Enter Position: ";
cin >> pos;
InsertPos(data,pos);
break;
}
case 4:
{
cout << "Enter Data to Search: ";
cin >> data;
Search(data);
break;
}
case 5:
{
PrintList();
break;
}
case 6:
{
DeleteBegin();
break;
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A

}
case 7:
{
DeleteEnd();
break;
}
case 8:
{
DeletePos();
break;
}
case 9:
{
terminate = true;
cout << endl;
break;
}
default:
{
cout << "Invalid Operation Entered.";
}
}
} while (terminate==false);

system("pause");

OUTPUT:
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A

Exercise 2

Write a program in which a pile of books are arranged on a shelf of library .Following
information must be stored
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A

 Book Id
 Title of Book
 Author
Books must be arranged in LIFO fashion using single link list.Your program must display
at least 10 books.

CODE:

#include<iostream>
using namespace std;

struct node
{
int id;
string bkname;
string author;
node *next;
};
node *top;

void Push(int id, string bkname, string author)


{
node* tempnode;

tempnode = new node();


tempnode->id = id;
tempnode->bkname = bkname;
tempnode->author = author;
tempnode->next = top;
top = tempnode;
}

void Pop()
{
node* temp;
temp = top;
top = temp->next;
free(temp);
cout << "Book Removed."<<endl;
}

void Display()
{
node *ptr;
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A

ptr = top;
int count = 1;
cout << "PILE: "<<endl;
cout <<
"............................................................................"
<<endl;
while (ptr != NULL)
{
cout <<count<<") "<< "ID: " << ptr->id<<" "<<"Title : "<<ptr-
>bkname<<" "<<"Author : "<<ptr->author<<endl;
ptr = ptr->next;
count++;
}
cout <<
"............................................................................"
<< endl;
cout << endl;
}

void main()
{
int id,opt;
string bkname, author;
bool terminate = false;

cout << "------------OPERATIONS------------"<<endl;


cout << "1) Insert Book." << endl;
cout << "2) Remove Book." << endl;
cout << "3) Display Pile." << endl;
cout << "4) Exit." << endl;
cout << "----------------------------------"<<endl;

do
{
cout << endl;
cout << "Enter Operation: ";
cin >> opt;

switch (opt)
{
case 1:
{
cout << "Enter ID: ";
cin >>id;
cout << "Enter Book Title: ";
cin >> bkname;
cout << "Enter Author: ";
cin >> author;
Push(id,bkname,author);
break;
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A

}
case 2:
{
Pop();
break;
}
case 3:
{
Display();
break;
}
case 4:
{
terminate = true;
cout << endl;
break;
}
default:
{
cout << "Invalid Operation Entered.";
}
}
} while (terminate==false);

system("pause");

}
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A

OUTPUT:
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A

Exercise 3

The appointment scheduling with the doctor requires tokens for checkup. Each patient
visits the doctor according to their token number. Once the patient has visited the doctor
his/her token number gets deleted from the list. Write a program that implements the
scenario using single list. Using Queue along with linked list for its implementation.

CODE:

#include<iostream>
using namespace std;

struct node
{
int token;
node *next;
};
node *Front;
node* Rear;
int flag = 1;

void Push(int token)


{
node* ptr, * tempnode;
ptr = Rear;
if (Rear==NULL)
{
tempnode = new node();
tempnode->token = token;
Rear = tempnode;
Front = tempnode;
flag++;
}
else {
while (ptr->next != NULL)
{
ptr = ptr->next;
}
tempnode = new node();
tempnode->token = token;
tempnode->next = NULL;
ptr->next = tempnode;
}
}

void Pop()
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A

{
node* temp;
temp = Front;
Front = temp->next;
free(temp);
cout << "Token Removed."<<endl;
}

void Display()
{
node *ptr;
ptr = Front;
cout << "Queue: ";
while (ptr != NULL)
{
cout << ptr->token<<" ";
ptr = ptr->next;
}
cout << endl;
}

void main()
{
int token,opt;
bool terminate = false;

cout << "------------CLINIC------------"<<endl;


cout << "1) Generate Token." << endl;
cout << "2) Remove Token." << endl;
cout << "3) Display Remaining Queue." << endl;
cout << "4) Exit." << endl;
cout << "------------------------------"<<endl;

do
{
cout << endl;
cout << "Enter Operation: ";
cin >> opt;

switch (opt)
{
case 1:
{
cout << "Enter Token: ";
cin >> token;
Push(token);
break;
}
case 2:
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A

{
Pop();
break;
}
case 3:
{
Display();
break;
}
case 4:
{
terminate = true;
cout << endl;
break;
}
default:
{
cout << "Invalid Operation Entered.";
}
}
} while (terminate==false);

system("pause");

}
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A

OUTPUT:

You might also like