0% found this document useful (0 votes)
38 views15 pages

DS Lab Assignment 3

The document discusses circular linked lists and their implementation in C++. It describes operations like append, prepend, insert, delete, update, display and count for a circular linked list. It also includes code snippets to implement these operations.

Uploaded by

hareem aman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views15 pages

DS Lab Assignment 3

The document discusses circular linked lists and their implementation in C++. It describes operations like append, prepend, insert, delete, update, display and count for a circular linked list. It also includes code snippets to implement these operations.

Uploaded by

hareem aman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

COMSATS UNIVERSITY, ISLAMABAD

Sahiwal Campus.

Assignment 3

Assignment Title:
“Circular linked list”

Submitted To:
Sir Sanan

Submitted By:

Name: Hareem Aman


Registration #: SP20-BSI-002
Course: Data Structure
Course code: CSC211
Degree: BS Bioinformatics
Semester-3 Batch-22

Date: 1st May, 2021


Circular Linked list:
A circular linked list is a sequence of elements in which every element has a link to its next
element in the sequence and the last element has a link to the first element. That means circular
linked list is similar to the single linked list except that the last node points to the first node in
the list.

Operations in circular linked list:


Following are the important operations supported by a circular list.
 Traverse
Iterate through the nodes in the linked list starting from the head node.
 Append
Attach a new node (to the end) of a list
 Prepend
Attach a new node (to the beginning) of the list
 Insert
Attach a new node to a specific position on the list
 Delete
Remove/Delink a node from the list
 Update
Update data of new node with specific key value
 Count
Returns the no of nodes in linked list

C++ code for circular linked list implementation


#include<iostream>
using namespace std;
class Node
{
public:
int key; // uniquely identifies a node
int data; // value stored in the node
Node * next; // pointer that references to the next node in the list
Node()
{
key = 0;
data = 0;
next = NULL;
}
Node(int key1, int data1)
{
key = key1;
data = data1;
}
};
class CircularLinkedList {
public:
Node * h; // head node
CircularLinkedList()
{
h = NULL;
}
Node * NodeExists(int k) // method to check if the node exists with the particular key value
{
Node * tmp = NULL;
Node * pointer = h;
if (pointer == NULL)
{
return tmp;
}
else {
do
{
if (pointer -> key == k)
{
tmp = pointer;
}
pointer = pointer -> next;
} while (pointer != h);
return tmp;
}
}
void AppendNode(Node * newnode) // appending node in the list
{
if (NodeExists(newnode -> key) != NULL)
{
cout<<"Since node exists with key value : " <<newnode -> key;
cout<<" so, node can't be appended."<<endl;
}
else
{
if (h == NULL)
{
h = newnode;
newnode -> next = h;
cout << "Node appended at the head." << endl;
}
else
{
Node * pointer = h;
while (pointer -> next != h)
{
pointer = pointer -> next;
}
pointer -> next = newnode;
newnode -> next = h;
cout << "Node appended." << endl;
}
}
}
void PrependNode(Node * newnode) //prepending node in the list
{
if (NodeExists(newnode -> key) != NULL)
{
cout<<"Since node exists with key value : " <<newnode -> key;
cout<<" so, node can't be prepended."<<endl;
}
else
{
if (h == NULL)
{
h = newnode;
newnode -> next = h;
cout << "Node prepended at the head." << endl;
}
else
{
Node * pointer = h;
while (pointer -> next != h)
{
pointer = pointer -> next;
}
pointer -> next = newnode;
newnode -> next = h;
h = newnode;
cout << "Node prepended." << endl;
}
}
}
void InsertNode(int k, Node * newnode) //inserting node
{
Node * pointer = NodeExists(k);
if (pointer == NULL)
{
cout << "Node does not exist with key:" << k << endl;
}
else
{
if (NodeExists(newnode -> key) != NULL)
{
cout<<"Since node exists with key value : " <<newnode -> key;
cout<<" so, node can't be inserted."<<endl;
}
else
{
if (pointer -> next == h)
{
newnode -> next = h;
pointer -> next = newnode;
cout << "Node inserted at the end of the list." << endl;
}
else
{
newnode -> next = pointer -> next;
pointer -> next = newnode;
cout << "Node inserted in between the list." << endl;
}
}
}
}
void DeleteNode(int k) // deleting node
{
Node * pointer = NodeExists(k);
if (pointer == NULL)
{
cout << "Node does not exist with key: " << k <<endl;
}
else
{
if (pointer == h)
{
if (h -> next == h)
{
h = NULL;
cout << "Head node is deleted and the list is now empty."<<endl;
}
else
{
Node * pointer1 = h;
while (pointer1 -> next != h)
{
pointer1 = pointer1 -> next;
}
pointer1 -> next = h -> next;
h = h -> next;
cout << "Node deleted with key value : " << k << endl;
}
}
else
{
Node * tmp = NULL;
Node * prevpointer = h;
Node * currentpointer = h -> next;
while (currentpointer != NULL)
{
if (currentpointer -> key == k)
{
tmp = currentpointer;
currentpointer = NULL;
}
else
{
prevpointer = prevpointer -> next;
currentpointer = currentpointer -> next;
}
}
prevpointer -> next = tmp -> next;
cout << "Node deleted with key value : " << k << endl;
}
}
}
void UpdateNode(int k, int newdata) //updating node
{
Node * pointer = NodeExists(k);
if (pointer != NULL)
{
pointer -> data = newdata;
cout << "Node updated." << endl;
}
else
{
cout << "Node doesn't exist with key: " << k << endl;
}
}
void DisplayList()
{
if (h == NULL)
{
cout << "Circular Linked List is empty."<<endl;
}
else
{
cout <<"Head: [" << h -> key << "," << h -> data << "," << h -> next << "]" << endl;
cout << "Circular Linked List values are as follows: " << endl;
Node * tmp = h;
do
{
cout << "[" << tmp -> key << "," << tmp -> data << "," << tmp -> next << "] ----> ";
tmp = tmp -> next;
} while (tmp != h);
}
}
void CountList()
{
int count=0;
if(h==NULL)
{
cout << "Node Count= "<<count<<endl;
}
else
{
Node * tmp =h;
do
{
count++;
tmp = tmp -> next;
} while (tmp != h);
cout<<"Node Count= "<<count<<endl;
}
}
};
int main() // main function
{
CircularLinkedList c;
int op;
int keyvalue, kvalue, datavalue;
do
{
cout<< "\n Select the option to perform on the Circular Linked List."<< endl;
cout<< "1. Append " << endl;
cout<< "2. Prepend " << endl;
cout<< "3. Insert " << endl;
cout<< "4. Delete " << endl;
cout<< "5. Update " << endl;
cout<< "6. Display " << endl;
cout<< "7. Node Count " << endl;
cout<< " Enter 0 to exit " << endl;
cin >> op;
Node * node1 = new Node();
switch (op) {
case 0:
break;
case 1:
cout << "Enter the key value." << endl;
cin >> keyvalue;
cout << "Enter the data value." << endl;
cin >> datavalue;
node1 -> key = keyvalue;
node1 -> data = datavalue;
c.AppendNode(node1);
break;
case 2:
cout << "Enter the key value." << endl;
cin >> keyvalue;
cout << "Enter the data value." << endl;
cin >> datavalue;
node1 -> key = keyvalue;
node1 -> data = datavalue;
c.PrependNode(node1);
break;
case 3:
cout << "Enter the key value after which the node is to be inserted. " << endl;
cin >> kvalue;
cout << "Enter the key value." << endl;
cin >> keyvalue;
cout << "Enter the data value." << endl;
cin >> datavalue;
node1 -> key = keyvalue;
node1 -> data = datavalue;
c.InsertNode(kvalue,node1);
break;
case 4:
cout << "Enter the key value of the node which is to be deleted. " << endl;
cin >> kvalue;
c.DeleteNode(kvalue);
break;
case 5:
cout << "Enter the key value to be updated." << endl;
cin >> keyvalue;
cout << "Enter the data value to be updated." << endl;
cin >> datavalue;
c.UpdateNode(keyvalue, datavalue);
break;
case 6:
c.DisplayList();
break;
case 7:
c.CountList();
break;
default:
cout << "Invalid Choice!" << endl;
}
} while (op!=0);
return 0;
}

Implementation of insertion operation on singly linked list


#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* getNode(int data)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertAtMid(Node** head_ref, int x)
{
if (*head_ref == NULL)
*head_ref = getNode(x);
else {
Node* newNode = getNode(x);
Node* ptr = *head_ref;
int len = 0;
while (ptr != NULL) {
len++;
ptr = ptr->next;
}
int count = ((len % 2) == 0) ? (len / 2) :
(len + 1) / 2;
ptr = *head_ref;
while (count-- > 1)
ptr = ptr->next;
newNode->next = ptr->next;
ptr->next = newNode;
}
}
void display(Node* head)
{
while (head != NULL) {
cout << head->data << " ";
head = head->next;
}
}
int main()
{
Node* head = NULL;
head = getNode(1);
head->next = getNode(2);
head->next->next = getNode(4);
head->next->next->next = getNode(5);
cout << "Linked list before insertion: ";
display(head);
int x = 3;
insertAtMid(&head, x);
cout << "\nLinked list after insertion: ";
display(head);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy