Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Assignment 1 - DSA

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

DEPARTMENT OF ELECTRICAL AND

COMPUTER ENGINEERING
(BCE-3B)

ASSIGNMENT
Student Name: Eesha Jamil, Zaima Ansar

Reg ID: 230655, 233050

Course Name: Data Structures and Algorithms

Instructor: Ma’am Zoafshan

Submission Date: 9th October 2024


LINKED LIST
Linked List:
A linked list is a data structure consisting of a sequence of elements, where each
element (called a "node") contains two parts:

1. Data: Stores the actual information.


2. Pointer/Reference: Points to the next node in the sequence (or in some
cases, to the previous node, depending on the type of linked list).

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.

TYPES OF LINKED LISTS

There are several types of linked lists:


1. Singly Linked List
2. Doubly Linked List
3. Circularly Linked List

Singly Linked List:


A singly linked list is the most basic type of linked list. It consists of a
sequence of nodes, each of which contains a value and a reference (i.e., a "link") to
the next node in the sequence. The last node in the sequence has a null reference,
indicating the end of the list.
Node1 -> Node2 -> Node3 -> Node4 -> null

Doubly Linked List:


A doubly linked list is a variation of the singly linked list, where each node has
references to both the previous and next nodes in the sequence. This allows for
efficient insertion and deletion of nodes at any position in the list.
null <- Node1 <-> Node2 <-> Node3 <-> Node4 -> null

Circularly Linked List:


A circularly linked list is a variation of the singly linked list, where the last node
points back to the first node, forming a circle. This allows for efficient traversal of the
list in a circular fashion.

Node1 -> Node2 -> Node3 -> Node4 -> Node1


SINGLY LINKED LIST
#include<iostream> temp=temp->next;
using namespace std; current_pos++;
}
class Node{ //temp is pointing to node at pos-1
public: new_node->next=temp->next;
int val;//object or data temp->next=new_node;
Node*next;//pointer that points to next }
//constructor
Node(int data) //INSERTION AT TAIL
{ val=data;
void insertAtTail(int value)
next=NULL;
{
}
Node*new_node = new Node(value);
};
if (head==NULL)
{
class LinkedList{
head = new_node;
public:
return;
Node*head;
}
LinkedList()
{
Node*temp=head;
head=NULL;
while(temp->next!=NULL)
}
{
//INSERTION AT HEAD temp=temp->next;
void insertAtHead(int value) }
{ temp->next=new_node;
Node*new_node = new Node(value); }
new_node->next=head;
head = new_node; //DELETION AT HEAD
}
void deleteAtHead()
{
//INSERTION AT POSITION Node*temp=head;
void insertAtPosition(int value,int pos) //Node to be deleted
{ head = head->next;
if(pos==0) delete temp;//built-in function used
{ to free up the memory
insertAtHead(value); }
return;
}
Node*new_node=new Node(value);
Node*temp=head;
int current_pos=0;
while(current_pos!=pos-1)
{
//DELETION AT MIDDLE int main()
{
void deleteAtPosition(int pos)
LinkedList list;
{
if(pos==0)
//INSERTION
{
list.insertAtTail(1);
deleteAtHead();
list.display();
return;
list.insertAtTail(2);
}
list.display();
int curr_pos=0;
list.insertAtTail(3);
Node*prev=head;
list.display();
while(curr_pos!=pos-1)
cout<<endl<<endl;
{
prev = prev->next;
list.insertAtHead(4);
curr_pos++;
list.display();
}
list.insertAtHead(5);
//prev is pointing to node at pos-1
list.display();
Node*temp=prev->next;
cout<<endl<<endl;
prev->next=prev->next->next;
delete temp;
list.insertAtPosition(4,2);
}
list.display();
//DELETION AT TAIL list.insertAtPosition(2,3);
void deleteAtTail() list.display();
{ cout<<endl<<endl;
Node*second_last=head;
while(second_last->next->next!=NULL) //DELETION
{ list.deleteAtTail();
second_last=second_last->next; list.display();
//now second last points to second list.deleteAtHead();
last next node list.display();
} list.deleteAtPosition(2);
Node*temp = second_last->next; //node to list.display();
be deleted cout<<endl<<endl;
second_last->next=NULL;
delete temp; return 0;
} }

//function to display the nodes of linked list


void display()
{
Node*temp=head;
while(temp!=NULL)
{
cout<<temp->val<<"->";
temp=temp->next;
}
cout<<"NULL"<<endl;
}};
OUTPUT:
DOUBLY LINKED LIST
#include<iostream> //INSERTION AT HEAD
using namespace std;
void insertAtStart(int val)
{
class Node{
Node*new_node = new Node(val);
public:
if(head==NULL)//if head is equal to
int val;
null it means our linked list is empty
Node*prev;
{
//this pointer will point to another node
head = new_node;
Node*next;
tail = new_node;
return;
//CONSTRUCTOR
}
Node(int data){
new_node->next=head;
val = data;
head->prev = new_node;
prev=NULL;
head = new_node;
next=NULL;
}
}
};
//INSERTION AT POSITION
class DoublyLinkedList{ void insertAtPosition(int val,int k)
public: {
Node*head; //assuming k is less than or equal to length
Node*tail; of doubly linked list
DoublyLinkedList(){ Node*temp = head;
head=NULL; int count = 1;//first node position
tail=NULL;
} while(count<(k-1)){
//display function temp=temp->next;
void display() count++;
{ }
Node*temp=head;
while(temp!=NULL) //temp will be pointing to the node at
{ (k-1)th position
cout<<temp->val<<" "; Node*new_node = new Node(val);
temp=temp->next; new_node->next = temp->next;
} temp->next = new_node;
cout<<endl; new_node->prev=temp;
} new_node->next->prev=new_node;
}
//INSERTION AT TAIL //DELETION AT POSITION
void insertAtEnd(int val) void deleteAtPosition(int k)
{ { //assuming k is less than or equal to
Node*new_node = new Node(val); length of dll
if (tail==NULL) Node*temp=head;
{ int counter=1;//first node position
head = new_node; while(counter<k)
tail = new_node; { temp=temp->next;
return; counter++;}
} //now temp is pointing to node at kth
tail->next = new_node; position
new_node->prev = tail; temp->prev->next = temp->next;
tail = new_node; temp->next->prev = temp->prev;
} delete temp;
}
//DELETION AT HEAD };
int main()
void deleteAtStart()
{
{
Node*new_node = new Node(3);
if(head==NULL)
DoublyLinkedList dll;
{ return;}
//INSERTION
Node*temp = head;
dll.insertAtStart(1);
head = head->next;
dll.display();
dll.insertAtStart(2);
if(head==NULL)//if doubly linked list
dll.display();
has only 1 node
dll.insertAtStart(3);
{
dll.display();
tail=NULL;
cout<<endl<<endl;
}
dll.insertAtEnd(4);
else
dll.display();
{
dll.insertAtEnd(5);
head->prev = NULL;
dll.display();
}
dll.insertAtEnd(6);
delete temp;
dll.display();
}
cout<<endl<<endl;
//DELETION AT TAIL dll.insertAtPosition(4,3);
void deleteAtEnd() dll.display();
{ cout<<endl<<endl;
if(head==NULL) //DELETION
{ return;} dll.deleteAtStart();
Node*temp = tail; dll.display();
tail = tail->prev; dll.deleteAtEnd();
if(tail==NULL) dll.display();
{ head=NULL; } dll.deleteAtPosition(3);
else dll.display();
{ tail->next = NULL; } return 0;
delete temp; }
}

//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;

head->prev = tail; temp->next->prev = temp->prev;


delete temp; delete temp;
temp->next->prev = temp->prev;
delete temp; } cdll.insertAtEnd(6);
} cdll.display();
};
cout<<endl<<endl;
int main()
{ cdll.insertAtPosition(4,3);
CircularDoublyLinkedList cdll; cdll.display();

// 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;
}

You might also like