Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Mahzaib Fatima

824-FOC/BSIT/F-22
Lab Manual for Data Structures
Lab-3
Linked list implementation
Task-1
Compare linear linked list with circular linked list and provide three differences
between the two structures.
Both linear linked lists and circular linked lists are fundamental data structures that store
elements in a sequential order. However, they differ in how the elements are connected:
1. Structure:
Linear Linked List: Elements (nodes) are connected in a linear chain. The first
node has a pointer to the second, the second to the third, and so on. The last node
has a null pointer, signifying the end of the list.
Circular Linked List: Elements form a closed loop. The last node's pointer points
back to the first node, creating a circular structure. There's no designated "end" since
traversal continues indefinitely.
2. Traversal:
Linear Linked List: You can only traverse the list in one direction, starting from
the head and following the next pointers until you reach the null pointer at the end.
Circular Linked List: Traversal can happen in both directions because there's no
defined start or end. You can begin at any node and keep following the next pointers
to loop around the entire list.
3. Memory Usage:
Linear Linked List: Requires an additional pointer (head) to store the address of
the first node.
Circular Linked List: Potentially more memory efficient. Only the head pointer is
needed to access the entire list since traversal can start from there and continue
indefinitely.

Task-2
List three real world examples in which linked list structure can be used
1. Music Playlist: Imagine your music player's playlist. Each song can be considered a
node in a linked list. The node likely stores the song data (title, artist, etc.) and a pointer
to the next song in the playlist. This allows the music player to efficiently play songs in
order by following the chain of pointers from the first song (head of the list) to the last.
2. Web Browser History: When you browse the web, your browser keeps track of visited
websites in a linked list (often singly linked). Each visited site is a node containing the
URL and navigation information. The "back" button functionality relies on this
structure. Clicking "back" essentially traverses the linked list in reverse order, following
the pointers back to the previous webpage.
3. Social Media Feed: Your social media feed, where you see posts from friends and
followed accounts, can be implemented using a linked list. Each post is a node
containing the content and pointers to the next and previous posts in the feed. This
allows for efficient scrolling and navigation through the feed, whether you're going
down to see newer posts or using a "back" button to revisit earlier ones.
4. Undo/Redo Functionality in Text Editors: Text editors often implement undo and
redo functionality using linked lists. Each edit operation performed by the user, such as
typing, deleting, or modifying text, can be stored as a node in two separate linked lists:
one for undo operations and one for redo operations. Each node contains the state of
the document after the edit operation and pointers to the previous and next states. When
the user initiates an undo operation, the editor reverts to the previous state by traversing
backward through the undo list. Conversely, redo operations traverse forward through
the redo list to reapply undone changes. Linked lists provide an efficient way to manage
the sequence of document states and facilitate undo/redo functionality in text editors.
Practice TASK1:
Implement the following functions for linked list:
1) Insert front
2) Insert End
3) Insert after number
4) Delete front
5) Delete End
6) Delete any number from list
7) Display all element
8) Limit user so that he/she can only add 10 numbers in the list. After that give error
that list
is full.
Output:
Practice TASK2:
In task 1 add two functions Remove_Duplicate() and Count() in the class.
Remove_Duplicate() function should remove all the number that are occurring more
than one time. Count() function should count the number of nodes present at any
time in the list.
Practice TASK3:
Manage data of Books (Title, Authors, Edition, Price) using linked list. Add Books in
the list. Now delete
all Books having price more than 2000 from the list.
#include <iostream>
#include <string>
using namespace std;

class Book {
public:
string title;
string authors;
int edition;
double price;

Book(string t, string a, int e, double p) : title(t), authors(a), edition(e),


price(p) {}
};

class Node {
public:
Book* data;
Node* next;

Node(Book* book) : data(book), next(nullptr) {}


};

class BookList {
private:
Node* head;
int size;

public:
BookList() : head(nullptr), size(0) {}

// Function to add a book to the list


void addBook(string title, string authors, int edition, double price) {
Book* newBook = new Book(title, authors, edition, price);
Node* newNode = new Node(newBook);
newNode->next = head;
head = newNode;
size++;
}

// Function to delete books with price more than 2000


void deleteBooksAbovePrice() {
Node* current = head;
Node* prev = nullptr;
while (current != nullptr) {
if (current->data->price > 2000) {
if (prev == nullptr) {
head = current->next;
}
else {
prev->next = current->next;
}
delete current;
size--;
current = (prev == nullptr) ? head : prev->next;
}
else {
prev = current;
current = current->next;
}
}
}

// Function to display all books in the list


void displayBooks() {
Node* current = head;
while (current != nullptr) {
cout << "Title: " << current->data->title << ", Authors: " <<
current->data->authors
<< ", Edition: " << current->data->edition << ", Price: " <<
current->data->price << endl;
current = current->next;
}
}

// Destructor to free memory


~BookList() {
Node* current = head;
while (current != nullptr) {
Node* temp = current;
current = current->next;
delete temp->data;
delete temp;
}
}
};

int main() {
BookList bookList;

int choice;
do {
cout << "\n*********Menu************:\n";
cout << "1. to Add a book\n";
cout << "2. Delete books having price above 2000\n";
cout << "3. to Display all books\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1: {
string title, authors;
int edition;
double price;
cout << "Enter book details:\n";
cout << "Title: ";
cin.ignore();
getline(cin, title);
cout << "Authors: ";
getline(cin, authors);
cout << "Edition: ";
cin >> edition;
cout << "Price: ";
cin >> price;
bookList.addBook(title, authors, edition, price);
cout << "Book added successfully.\n";
break;
}
case 2:
bookList.deleteBooksAbovePrice();
cout << "Books with price above 2000 deleted.\n";
break;
case 3:
cout << "Books in the list:\n";
bookList.displayBooks();
break;
case 4:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice. Please enter a valid option.\n";
}
} while (choice != 4);

return 0;
}

You might also like