Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doubly Linked List programs #232

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 245 additions & 0 deletions Double Linkedlist/Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include <iostream>;
#include "Stack.h";
using namespace std;
class linkedlist
{


public:
struct Node
{
Node *next = NULL;
Node *prev = NULL;
double val ;

};
Node *head=NULL;
Node * trav = NULL;
Node * temp = NULL;




void insert(double key) {

Node *newNode = new Node();
newNode->val = key;
newNode->next = NULL;

Node *forw = NULL;
Node *back = NULL;

if (head == NULL)
{
head = newNode;

}
else
{
forw = head;
while (forw)
{
back = forw;
forw = forw->next;
}
back->next = newNode;
}
}

void removeDuplicate() {

Node *prev = head;
Node *temp = NULL;


while (prev->next!=NULL)
{

if (prev->val==prev->next->val)
{
temp = prev->next;
prev->next = temp->next;
delete temp;
}
else
{
prev = prev->next;

}


}


}

/*void insert(double key) {

Node *newNode = new Node();
newNode->val = key;
newNode->next = NULL;

Node *forw = NULL;
Node *back = NULL;

if (head==NULL)
{
head = newNode;

}
else
{
forw = head;

while (forw!=NULL&&forw->val < key)
{
back = forw;
forw = forw->next;
}

if (back==NULL)
{
head = newNode;
newNode->next = forw;
forw->prev = newNode;
}
else if (forw!= NULL)
{
back->next = newNode;
newNode->next = forw;
forw->prev = newNode;
}
else
{
back->next = newNode;

}


}


}*/


void display() {

Node *trav = head;
while (trav != NULL)
{
std::cout << trav->val << ">>";
trav = trav->next;
}


}
void deletenode(double key) {

Node * frwd=head;
Node * prev = NULL;
Node * store = NULL;


while (frwd->val!=key &&frwd!= NULL)
{
prev = frwd;
frwd = frwd->next;
}

if (frwd->val==head->val)
{
store = head;
head = frwd->next;
delete store;
}
else if (frwd->next!= NULL)
{
store = frwd;
frwd = frwd->next;
prev->next = frwd;
frwd->prev = prev;
delete store;
}
else
{
prev->next = NULL;
}


}
Node* detectloop(Node *head) {

Node *p = head;
Node *q = head;

while (p&&q&&q->next)
{
p = p->next;
q = q->next->next;
if (p==q)
{
cout << "loop Detected";
return p;
}
}
cout << "No loop Detected";

return NULL;
}


void middleNode(Node *head) {

Node *slow = head;
Node *fast = head;

while (fast!=NULL&& fast->next!=NULL)
{

fast = fast->next->next;
slow = slow->next;


}

cout << "Middle Node is :" << slow->val;
}

void removeLoop(Node *p, Node *head) {

Node *q = head;

while (p->next!=q->next)
{
p = p->next;
q = q->next;
}

p->next = NULL;

}


void reverseLL(Node *head) {


Node *tmp = NULL;
Node *ptr = head;
while (ptr->val != 10) {
tmp = ptr->next;
ptr->next = ptr->prev;
ptr->prev = tmp;
if (tmp==NULL) {
head = ptr;
break;
}

ptr = tmp;
}
display();
}
};
#endif
9 changes: 9 additions & 0 deletions Double Linkedlist/New Text Document.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
This program contain all double linked ist problems listed below...

.) Delete node from start, End or middle
.) Check loop in a linked list, looped index nad removal of loop
.) Add a node
.) inorder, preorder,postorder
.) Finding middle node
.) finding given node
.) No of occurance of a value
91 changes: 91 additions & 0 deletions Double Linkedlist/Stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

#ifndef INTSTACK_H
#define INTSTACK_H
#include <iostream>;
using namespace std;

class IntStack
{
private:
int *stackArray; // Pointer to the stack array
int stackSize; // The stack size
// Indicates the top of the stack

public:
int top;
// Constructor
IntStack(int size =10)
{
stackArray = new int[7];
stackSize = size;
top = -1;

}




// Destructor
~IntStack()
{
delete[] stackArray;

}

// Stack operations
void push(int num)
{
if (isFull())
{
cout << "The stack is full.\n";
}
else
{
top++;
stackArray[top] = num;
}

}
int pop()

{
int num;
if (isEmpty())
{
cout << "The stack is empty.\n";
}
else
{
num = stackArray[top];
top--;
}
return num;

}

bool isFull() const
{
bool status;

if (top == stackSize -1)
status = true;
else
status = false;

return status;

}
bool isEmpty() const
{
bool status;

if (top == -1)
status = true;
else
status = false;

return status;
}
};
#endif

Loading