Dsa Assignment - I: DR, Iddrees
Dsa Assignment - I: DR, Iddrees
Dr, Iddrees
NAME : NOMAN
ROLL : 564
Section : B
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();
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;
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
}
}
if (!Head)
return;
void List::DeleteCircleListEvenNodes() {
if (!Head)
return;
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();
};
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++;
}
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();
};
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 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;
}