Assignment 1 - DSA
Assignment 1 - DSA
Assignment 1 - DSA
COMPUTER ENGINEERING
(BCE-3B)
ASSIGNMENT
Student Name: Eesha Jamil, Zaima Ansar
Unlike arrays, where elements are stored in contiguous memory locations, the
elements (or nodes) of a linked list can be stored at different, non-contiguous
locations in memory. This makes linked lists dynamic, as their size can grow or
shrink during runtime by adding or removing nodes.
//DELETION AT
OUTPUT:
CIRCULAR SINGLY LINKED LIST
#include<iostream> //INSERTION AT START
using namespace std; void insertAtStart(int val)
class Node{ {
public: Node*new_node = new Node(val);
int val;//object or data if(head==NULL)
Node*next;//pointer that points to {
next head=new_node;
//constructor new_node->next = head;//circular
Node(int data) linked list
{ }
val=data; Node*tail = head;
next=NULL; while(tail->next != head)
} {
}; tail = tail->next;
class CircularLinkedList{ }
public: //now tail is pointing to the last node
Node*head; tail->next = new_node;
CircularLinkedList() new_node->next = head;
{ head = new_node;
head = NULL; }
}
void display() //INSERTION AT END
{
void insertAtEnd(int val)
Node*temp = head;
{
do
Node*new_node = new Node(val);
{
if(head==NULL)
cout<<temp->val<<"->";
{
temp = temp->next;
head=new_node;
}
new_node->next = head;//circular
while(temp!=head);
linked list
cout<<endl;
}
}
Node*tail = head;
void printCircular()
while(tail->next != head)
{
{
Node*temp=head;
tail = tail->next;
for(int i=0;i<15;i++)
}
{
//tail will be pointing to the last node
cout<<temp->val<<"->";
tail->next = new_node;
temp = temp->next;
new_node->next = head;
}
}
cout<<endl;
}
//INSERTION IN MIDDLE tail=tail->next;
void insertAtPosition(int val,int pos) //now tail points to second last next node
{ }
if(pos==0) Node*temp = tail->next; //node to be
{ deleted
insertAtStart(val); tail->next=head;
return; delete temp;
} }
Node*new_node=new Node(val); //DELETION AT MIDDLE
Node*temp=head; void deleteAtPosition(int pos)
int current_pos=0; {
if(pos==0)
while(current_pos!=pos-1) {
{ deleteAtStart();
temp=temp->next; return;
current_pos++; }
} int curr_pos=0;
//temp is pointing to node at pos-1 Node*prev=head;
new_node->next=temp->next; while(curr_pos!=pos-1)
temp->next=new_node; {
} prev = prev->next;
curr_pos++;
//DELETION AT START }
void deleteAtStart() //prev is pointing to node at pos-1
{ Node*temp=prev->next;
if(head==NULL) prev->next=prev->next->next;
{ delete temp;
return; }
} };
Node*temp = head; int main()
Node*tail = head; {
while(tail->next!=head) CircularLinkedList cll;
{
tail = tail->next; //insertion
} cll.insertAtStart(1);
head = head->next; cll.display();
tail->next = head; cll.insertAtStart(2);
delete head; cll.display();
} cll.insertAtStart(3);
//DELETION AT END cll.display();
cll.insertAtStart(4);
void deleteAtEnd()
cll.display();
{
cout<<endl<<endl;
if(head==NULL)
{ return;}
cll.insertAtEnd(8);
Node*tail=head;
cll.display();
while(tail->next->next!=NULL)
//cll.printCircular();
{
cout<<endl;
cll.insertAtPosition(3,2);
cll.display();
cout<<endl;
//deletion
cll.deleteAtStart();
cll.display();
cll.deleteAtPosition(1);
cll.display();
cll.deleteAtEnd();
cll.display();
return 0;
}
OUTPUT:
CIRCULAR DOUBLY LINKED LIST
#include <iostream> void display()
using namespace std; {
Node* temp = head;
class Node{ do
public: {
int val; cout << temp->val << " ";
Node*prev; temp = temp->next;
Node*next; }
while (temp != head);
Node(int data) cout << endl;
{ }
val = data;
prev = NULL; // INSERTION AT HEAD
next = NULL; void insertAtStart(int val)
} {
}; Node* new_node = new Node(val);
if (head == NULL)
class CircularDoublyLinkedList{ {
head = new_node;
public: tail = new_node;
Node* head; new_node->next = new_node;
Node* tail; new_node->prev = new_node;
}
CircularDoublyLinkedList() else
{ {
head = NULL; new_node->next = head;
tail = NULL; new_node->prev = tail;
} head->prev = new_node;
tail->next = new_node;
void printCircular() head = new_node;
{ }
Node*temp=head; }
for(int i=0;i<15;i++) // INSERTION AT POSITION
{
void insertAtPosition(int val, int k)
cout<<temp->val<<"<->";
{
temp = temp->next;
Node* temp = head;
}
int count = 1;
cout<<endl;
while (count < k)
}
{
temp = temp->next;
count++;}
void display()
{
Node* new_node = new Node(val); head->prev = tail;
new_node->next = temp->next; delete temp;
new_node->prev = temp; }
temp->next->prev = new_node; }
temp->next = new_node; // DELETION AT TAIL
void deleteAtEnd()
if (temp == tail) {
{ if (head == NULL)
tail = new_node; {
} return;
} }
// INSERTION AT TAIL
void insertAtEnd(int val) if (head == tail)
{ {
Node* new_node = new Node(val); delete head;
if (tail == NULL) head = NULL;
{ tail = NULL;
head = new_node; }
tail = new_node; else
new_node->next = new_node; {
new_node->prev = new_node; Node* temp = tail;
} tail = tail->prev;
else tail->next = head;
{ head->prev = tail;
new_node->next = head; delete temp;
new_node->prev = tail; }
head->prev = new_node; }
tail->next = new_node;
tail = new_node; // DELETION AT POSITION
} void deleteAtPosition(int k)
} {
// DELETION AT HEAD Node* temp = head;
void deleteAtStart() int count = 1;
{ while (count < k)
if (head == NULL) {
{ return; } temp = temp->next;
if (head == tail) count++;
{ }
delete head; if (temp == head) {
head = NULL; deleteAtStart();
tail = NULL; } }
else else if (temp == tail) {
{ deleteAtEnd();}
Node* temp = head; else
head = head->next; {
tail->next = head; temp->prev->next = temp->next;
// INSERTION cout<<endl<<endl;
cdll.insertAtStart(1); cdll.printCircular();
cdll.display(); // DELETION
cdll.deleteAtStart();
cdll.insertAtStart(2); cdll.display();
cdll.display();
cdll.deleteAtEnd();
cdll.insertAtStart(3); cdll.display();
cdll.display();
cdll.deleteAtPosition(3);
cout<<endl<<endl; cdll.display();
cdll.insertAtEnd(4); return 0;
cdll.display(); }
cdll.insertAtEnd(5);
cdll.display();
OUTPUT:
cdll.insertAtEnd(6);
cdll.display();
cout<<endl<<endl;
cdll.insertAtPosition(4,3);
cdll.display();
cout<<endl<<endl;
cdll.printCircular();
// DELETION
cdll.deleteAtStart();
cdll.display();
cdll.deleteAtEnd();
cdll.display();
cdll.deleteAtPosition(3);
cdll.display();
return 0;
}