Sie sind auf Seite 1von 8

Department of Software Engineering

CS 250: Data Structures and Algorithms


Class: BEE-7AB
Lab 03: Singly Linked List
CLO1: < Please write CLO>

Date: October 06, 2016


Time: 10:00 am -1:00pm, 2:00pm 5:00pm
Instructor: Dr. Muhammad Shehzad

CS 250: Data Structures and AlgorithmsPage 1

Lab 3: Singly Linked List


Introduction
This lab will introduce students with the practical implementation of linked list with its
operations.

Objectives
Objective of this lab is to get familiar with singly linked list and implement them in C++.

Tools/Software Requirement
Visual Studio C++

Description
Singly Linked List
A Linked List, is a data structure consisting of a group of nodes which together represent a
sequence. Under the simplest form, each node is composed of two parts i.e. data part and a
reference part (also known as, a link) to the next node in the sequence. This structure allows
efficient insertion or removal of elements from any position in the sequence.

The basic operation consist of


Creating the list.
Initialize pointers to NULL.
Inserting nodes at beginning, last and specified location.
Delete nodes from beginning, last and specified location.
Traversing the list.
Destroying the list.

CS 250: Data Structures and AlgorithmsPage 2

Lab Tasks
Write a C++ program that can
1. Create a simple linked list using function, by inserting nodes at head.
2. Make a function that can insert another node at 3rd location.
3. Make a function that can display the lists made in 1 and 2.
4. Write a function that can delete node from the linked list selected by the user. Display
it as well.
5. Write a function that can count the number of nodes present in list.
6. Create menu in main function to give call to all of the above functions depending upon
users input.
Hint: First you will create the relevant classes, and the functions will belong to the List class.
//class of node
class node
{
public:
int value;
node *next;
};

Required functions for list class are:


void
void
void
void
void

insert_at_beginning(int new_value)
insert_at_loc(int location,int new_value)
del(int del_value)
display()
count()

#include <iostream>
using namespace std;
class LinkedList
{
private:
struct node
{
int data;
struct node* next;
node()
{
data = 0; next = NULL;
}
};
node* head;

CS 250: Data Structures and AlgorithmsPage 3

public:
LinkedList(void)
{
head = NULL;
}
void insert_at_end(int new_value)
{
//Create new node and store data in it
node* newNode; //Points to a new node
node* nodePtr; //To iterate through the list
newNode = new node();
newNode->data = new_value;
newNode->next = NULL;
if (head == NULL)
head = newNode;
else
{
nodePtr = head;
//Find the last node
while (nodePtr->next != NULL)
nodePtr = nodePtr->next;
//Point the last node towards the new node
nodePtr->next = newNode;
}
}
void insert_at_beginning(int new_value)
{
//Create new node and store data in it
node* newNode; //Points to a new node
node* nodePtr; //To iterate through the list
newNode = new node();
newNode->data = new_value;
newNode->next = NULL;
//If list is empty, insert node at the beginning
if (head == NULL)
head = newNode;
else // Otherwise create a pointer to store address of 2nd node and make head
point to newNode
{
newNode->next = head;
head = newNode;
}

CS 250: Data Structures and AlgorithmsPage 4

}
void insert_at_loc(int location, int new_value)
{
int currentPosition = 0;
node* nodePtr, *nextNode, *previousNode;
node* newNode; //Points to a new node
nodePtr = head;
//Create new node and store data in it
newNode = new node();
newNode->data = new_value;
newNode->next = NULL;
//If list is empty, insert node at the beginning
if (head == NULL)
head = newNode;
else
{
while (nodePtr != NULL)
{
if (location == currentPosition + 1)//Stop 1 position before
the location you want to enter value
{
nextNode = nodePtr->next;
nodePtr->next = newNode;
newNode->next = nextNode;
}
nodePtr = nodePtr->next;
currentPosition++;
}
}
}
void del(int del_value)
{
node* nodePtr;
node* previousNode;
nodePtr = head;
//If list is empty, do nothing
if (head == NULL)
return;
//If first node is the one to delete
if (head->data == del_value)
{

CS 250: Data Structures and AlgorithmsPage 5

nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
nodePtr = head;
while (nodePtr->data != del_value && nodePtr != NULL)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
//Make previous node point to next node and delete node in the middle
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
void display()
{
node* nodePtr = head; // Pointer to traverse list
while (nodePtr != NULL)
{
cout << nodePtr->data << endl;
nodePtr = nodePtr->next;
}
}
int count()
{
int count = 0;
node* nodePtr = head; // Pointer to traverse list
while (nodePtr != NULL)
{
count++;
nodePtr = nodePtr->next;
}
return count;
}
~LinkedList(void){}
};
int main()
{
LinkedList l1;
int option = 0, value, location;
do{
cout << "********************* LIST **********************" << endl;

CS 250: Data Structures and AlgorithmsPage 6

l1.display();
cout << "********************* Choose an option **********************" << endl;
cout << "Choose an option" << endl;
cout << "1. Insert value at beginning" << endl << "2. Insert value at end" <<
endl << "3. Insert value at any location other than beginning or end" << endl
<< "4. Delete a value" << endl <<"5. Count members" <<endl<<"0.
Exit" << endl;
cin >> option;
cin.ignore();
switch (option)
{
case 1:
cout << "Value = "; cin >> value; cin.ignore();
l1.insert_at_beginning(value);
break;
case 2:
cout << "Value = "; cin >> value; cin.ignore();
l1.insert_at_end(value);
break;
case 3:
cout << "Value = "; cin >> value; cin.ignore();
cout << "Location = "; cin >> location; cin.ignore();
if (location<=0 || location>l1.count() - 1)
{
cout << "Invalid Location" << endl;
getchar();
}
else
l1.insert_at_loc(location, value);
break;
case 4:
cout << "Value = "; cin >> value; cin.ignore();
l1.del(value);
break;
case 5:
cout << "Count = " << l1.count();
getchar();
break;
}
system("CLS");
} while (option != 0);
}

CS 250: Data Structures and AlgorithmsPage 7

Deliverables
Students are required to upload the lab on LMS before deadline.
Note: Use proper indentation and comments. Lack of comments and indentation will result in
deduction of marks. You will submit your working .cpp files in one (.zip) folder. The name of
files and folder should follow this format. i.e. YOUR_NAME_Lab#

CS 250: Data Structures and AlgorithmsPage 8

Das könnte Ihnen auch gefallen