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

Dsa Assignment - I: DR, Iddrees

The document contains code for a C++ program to delete nodes from different types of linked lists (single, double, circular) in various ways. It includes functions to: 1) Delete the second last node, even nodes, odd nodes, and all nodes with a given value from a single linked list. 2) Delete nodes from the beginning, end, by value, all of a given value, even nodes, and odd nodes from a double linked list. 3) Delete nodes from the beginning, end, by value, all of a given value, and even nodes from a circular linked list.

Uploaded by

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

Dsa Assignment - I: DR, Iddrees

The document contains code for a C++ program to delete nodes from different types of linked lists (single, double, circular) in various ways. It includes functions to: 1) Delete the second last node, even nodes, odd nodes, and all nodes with a given value from a single linked list. 2) Delete nodes from the beginning, end, by value, all of a given value, even nodes, and odd nodes from a double linked list. 3) Delete nodes from the beginning, end, by value, all of a given value, and even nodes from a circular linked list.

Uploaded by

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

DSA ASSIGNMENT – I

Dr, Iddrees

NAME : NOMAN
ROLL : 564
Section : B

SEPTEMBER 26, 2023


Linked List
Q.1 Write a Program of Delete Value of SSL, DLL, CLL.
In Four Case Use for Each List.
 Case 1: Delete 2nd last node from the link list
 Case 2: Delete all even nodes of link list
 Case 3: Delete all odd nodes of the link list
 Case 4: Delete all nodes from the link list whose info is 11

Code:
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
/*Double linked list
We are also use variable data and pointer next in double List */
Node *previous;
// Constructor
Node(int val) : data(val), previous(nullptr), next(nullptr) {}
};
class List{
private:
Node *Head;
public:
List(){
Head=nullptr;
}
// Delete part
void DeleteSingleListNodeFromEnd();
void DeleteSingleListNodeFromBeginning();
void DeleteSingleListNodeByValue(int);
void DeleteSingleListAllNodesByValue(int);
void DeleteSingleListEvenNodes();
void DeleteSingleListOddNodes();

//=> Delete Function


void DeleteDoubleListNodeAtEnd();
void DeleteDoubleListNodeAtBeginning();
void DeleteDoubleListNodeByValue(int);
void DeleteDoubleListAllNodesByValue(int);
void DeleteDoubleListEvenNodes();
void DeleteDoubleLIstOddNodes();
//=> Circle Delete Function
void DeleteCircleListNodeAtEnd();
void DelteCirceListNodeleteAtBegin();
void DeleteCircleListNodeByValue(int);
void DeleteCircleListNodeAllByValue(int);
void DeleteCircleListEvenNodes();
void DeleteCircleListOddNodes();

//=> Delete Singlelindked list function

void List::DeleteSingleListNodeFromBeginning() {

if(Head==NULL){
cout<<"The Linked list is Empty deletion is not Possible"<<endl;
return;
}
Node *local=Head;
Head= Head->next;
delete local;
}

void List::DeleteSingleListNodeFromEnd() {

if (Head==NULL)
{
cout<<"List is empty. Cannot delete from end." <<endl;
return;
}
else if (Head->next==NULL)
{
delete Head;
Head=nullptr;
return;
}

Node *local=Head;
while (local->next->next!=NULL)
{
local=local->next;
}

delete local->next;
local->next=NULL;
}

void List::DeleteSingleListNodeByValue(int valueToDelete){


if (Head==NULL)
{
cout<<"List is empty. Cannot delete node."<<endl;
return;
}

if (Head->data==valueToDelete)
{
Node *local=Head;
Head=Head->next;
delete local;
return ;
}

Node *current=Head;
Node *Previous=nullptr;
while ((current!=NULL)&&(current->data!=valueToDelete))
{
Previous=current;
current=current->next;
}

if (current==NULL)
{
cout<<"Value " << valueToDelete << " not found in the list."<<endl;
}
else{
Previous->next=current->next;
delete current;

}
}

void List::DeleteSingleListAllNodesByValue(int valueToDelete){

if (Head==NULL)
{
cout<< "List is empty. Cannot delete nodes." <<endl;
return ;
}

while ((Head!=NULL)&&(Head->data==valueToDelete))
{
Node *local=Head;
Head=Head->next;
delete local;
}

Node *current=Head;
Node *Previous;

while (current!=NULL)
{
if (current->data==valueToDelete)
{
Previous->next=current->next;
delete current;
current=Previous->next;
}
else{
Previous=current;
current=current->next;
}
}

void List::DeleteSingleListEvenNodes(){

if ((Head==nullptr)||(Head->next==nullptr))
{
cout<<" The list is empty or has only one node, nothing to delete"<<endl;
return;
}
Node *current=Head;
Node *Previous=NULL;
int possition = 1;

while (current!=nullptr)
{
if (possition % 2==0)
{
Previous->next=current->next;
delete current;
current=Previous->next;
}
else{
Previous=current;
current=current->next;
}
possition++;
}
};

void List::DeleteSingleListOddNodes() {
Node* current = Head;
Node* previous = nullptr;
int position = 1;

while (current!=nullptr) {
if (position % 2 == 1) {
if (previous!=nullptr) {
previous->next = current->next;
delete current;
current = previous->next;
} else {

Head = current->next;
delete current;
current = Head;
}
} else {
previous = current;
current = current->next;
}
position++;
}
}

void List::DeleteDoubleListNodeAtBeginning() {
if (Head == nullptr) {
cout<<"List is empty, nothing to delete"<<endl;
return;
}
Node* current = Head;
Head = Head->next;
if (Head != nullptr) {
Head->previous = nullptr;
}
delete current;
}

void List::DeleteDoubleListNodeAtEnd() {
if (Head == nullptr) {
cout<<"List is empty, nothing to delete"<<endl;
return;
}
if (Head->next == nullptr) {
// List has only one node, delete it
delete Head;
Head = nullptr;
} else {
Node* current = Head;
while (current->next->next != nullptr) {
current = current->next;
}
Node* temp = current->next;
current->next = nullptr;
delete temp;
}
}

void List:: DeleteDoubleListNodeByValue( int targetValue) {


Node* current = Head;
while (current != nullptr) {
if (current->data == targetValue) {
if (current->previous != nullptr) {
current->previous->next = current->next;
} else {
Head = current->next;
}
if (current->next != nullptr) {
current->next->previous = current->previous;
}
delete current;
return;
}
current = current->next;
}
}

void List:: DeleteDoubleListAllNodesByValue( int targetValue) {


Node* current = Head;
while (current != nullptr) {
if (current->data == targetValue) {
Node* temp = current;
if (current->previous != nullptr) {
current->previous->next = current->next;
} else {
Head = current->next;
}
if (current->next != nullptr) {
current->next->previous = current->previous;
}
current = current->next;
delete temp;
} else {
current = current->next;
}
}
}

void List:: DeleteDoubleListEvenNodes() {


Node* current = Head;
while (current != nullptr) {
if (current->data % 2 == 0) {
Node* temp = current;
if (current->previous != nullptr) {
current->previous->next = current->next;
} else {
Head = current->next;
}
if (current->next != nullptr) {
current->next->previous = current->previous;
}
current = current->next;
delete temp;
} else {
current = current->next;
}
}
}

void List:: DeleteDoubleLIstOddNodes() {


Node* current = Head;
while (current != nullptr) {
if (current->data % 2 != 0) {
Node* temp = current;
if (current->previous != nullptr) {
current->previous->next = current->next;
} else {
Head = current->next;
}
if (current->next != nullptr) {
current->next->previous = current->previous;
}
current = current->next;
delete temp;
} else {
current = current->next;
}
}
}

void List:: DelteCirceListNodeleteAtBegin() {


if (!Head)
return;

if (Head->next == Head) {
delete Head;
Head = nullptr;
} else {
Node* temp = Head;
while (temp->next != Head) {
temp = temp->next;
}
Node* temp2 = Head;
Head = Head->next;
temp->next = Head;
delete temp2;
}
}

void List::DeleteCircleListNodeAtEnd() {
if (!Head)
return;

if (Head->next == Head) {
delete Head;
Head = nullptr;
} else {
Node* temp = Head;
Node* previous = nullptr;
while (temp->next != Head) {
previous = temp;
temp = temp->next;
}
previous->next = Head;
delete temp;
}
}

void List:: DeleteCircleListNodeByValue(int val) {


if (!Head)
return;

if (Head->data == val) {
DelteCirceListNodeleteAtBegin();
// Recursively remove all occurrences of val
} else {
Node* current = Head;
while (current->next != Head) {
if (current->next->data == val) {
Node* temp = current->next;
current->next = current->next->next;
delete temp;
} else {
current = current->next;
}
}
}
}

void List::DeleteCircleListNodeAllByValue(int val) {


while (Head && Head->data == val) {
DelteCirceListNodeleteAtBegin();
}

if (!Head)
return;

Node* current = Head;


while (current->next != Head) {
if (current->next->data == val) {
Node* temp = current->next;
current->next = current->next->next;
delete temp;
} else {
current = current->next;
}
}
}

void List::DeleteCircleListEvenNodes() {
if (!Head)
return;

Node* current = Head;


do {
if (current->data % 2 == 0) {
Node* temp = current->next;
delete current;
current = temp;
} else {
current = current->next;
}
} while (current != Head);
}
void List:: DeleteCircleListOddNodes() {
if (!Head)
return;

Node* current = Head;


do {
if (current->data % 2 != 0) {
Node* temp = current->next;
delete current;
current = temp;
} else {
current = current->next;
}
} while (current != Head);
}

int main(){

List SingleList,DoubleList,CircleList,Local;
return 0;
}

Stack
Q 1. Write a Program on Stack using Array, LinkedList
Apply On All Operations.
 Push
 POP
 Peek
 PushAtPosition
 PoPAtPosituion
 Display

 Using Array
#include<iostream>
using namespace std;
const int Size=10;
class Stack
{
private:
int arr[Size];
int top;
public:
Stack():top(-1){};
bool isempty(){
return top==-1;
}
bool isfull(){
return top==Size;
}
int push(int);
void pop();
void topelement();
int addatpos(int,int);
int delatpos(int);
void dispaly();

};

int Stack::push(int value){


if (top<Size-1)
{
arr[++top]=value;
}
else{
cout<<"Stack is Over Flow"<<endl;
}

void Stack::pop(){
if (!isempty())
{
arr[top--];
}
else
{
cout<<"Stack is Empty"<<endl;
}

void Stack::topelement(){
if (isempty())
{
cout<<"Stack is Empty"<<endl;
}
else
{
cout<<arr[top];
}

}
int Stack:: addatpos(int value,int pos){
if ((pos<0)||(pos>top+1))
{
cout<<"invalied Possition"<<endl;
}
if (top<Size-1)
{
for (int i = top; i >pos; i--)
{
arr[i]=arr[i-1];
}
arr[pos]=value;
top++;
}

int Stack:: delatpos(int pos){


if ((isempty()|| pos<0 || pos>top))
{
cout<<"Invalid Possition"<<endl;
}
for (int i = pos; i <top; i++)
{
arr[i]=arr[i+1];
}
top--;
}

void Stack::dispaly(){
if (!isempty())
{
for (int i = 0; i <=top; i++)
{
cout<<arr[i]<<" ";
}

}
else{
cout<<"The Stack is Empty"<<endl;
}

int main(){
Stack obj;
obj.push(1);
obj.push(2);
obj.push(3);
obj.push(4);
obj.push(5);
obj.dispaly();
cout<<endl;
//obj.pop();
//obj.addatpos(2,2);
//obj.topelement();
//obj.delatpos(2);
obj.addatpos(2,2);
obj.dispaly();

return 0;
}

 Using LinkedList

#include<iostream>
using namespace std;

struct Node
{
int data;
Node *next;
Node(int value): data(value),next(nullptr){};
};

class Stack{
private:
Node *top;
public:
Stack():top(nullptr){};
bool isEmpty(){
return top==nullptr;
}
int push(int);
void pop();
int peek();
int pushaddpos(int,int);
int popatpos(int);
void display();

};

int Stack::push(int value){


Node *newnode=new Node(value);
newnode->next=top;
top=newnode;
};

void Stack::pop(){
Node *temp=top;
top=top->next;
delete temp;
};
int Stack:: peek(){
if (isEmpty())
{
cout<<"Stack is Empty "<<endl;
}
return top->data;
};

void Stack::display(){
if (isEmpty())
{
cout<<"Stack is Empty "<<endl;
}
Node* current = top;
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
cout <<endl;
}

int Stack::pushaddpos(int value, int pos){


if (pos<0)
{
cout<<"Invalid Position"<<endl;
}
if (pos==0 && isEmpty())
{
push(value);
}
Node *newnode=new Node(value);
Node *current =top;
for (int i = 1; i <pos && current->next!=nullptr; i++)
{
current=current->next;
}
newnode->next=current->next;
current->next=newnode;
}

int Stack::popatpos(int pos){


if (pos<0)
{
cout<<"Position Invalid"<<endl;
}
if(pos==0){
pop();
}
if (isEmpty())
{
cout<<"Stack is Empty"<<endl;
}
Node *current=top;
Node *previous=nullptr;
int count=0;
while (current!=nullptr && count!=pos)
{
previous=current;
current=current->next;
count++;
}
if(current==nullptr){
cout<<"Positon Not found"<<endl;
}
previous->next=current->next;
delete current;
}

int main(){
Stack obj;
obj.push(1);
obj.push(2);
obj.push(3);
obj.push(4);
obj.push(5);
obj.display();
cout<<obj.isEmpty();

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