0% found this document useful (0 votes)
38 views37 pages

Data Structures Code Implementation

The document discusses code implementations for various linked list and stack data structures in C++. It includes code for singly linked lists, doubly linked lists, circular linked lists, as well as stack implementations using arrays and linked lists. Functions are provided for creating, traversing, deleting nodes from each linked list type as well as push, pop and display functions for the stack implementations. The code provides a menu driven program to demonstrate the different data structure operations.

Uploaded by

AB Chaudry
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)
38 views37 pages

Data Structures Code Implementation

The document discusses code implementations for various linked list and stack data structures in C++. It includes code for singly linked lists, doubly linked lists, circular linked lists, as well as stack implementations using arrays and linked lists. Functions are provided for creating, traversing, deleting nodes from each linked list type as well as push, pop and display functions for the stack implementations. The code provides a menu driven program to demonstrate the different data structure operations.

Uploaded by

AB Chaudry
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/ 37

LINKED LIST CODES

#include<iostream>

#include<conio.h>

using namespace std;

struct sNode//simply link list node

int data;

sNode* next;

};

struct cNode//circular link list node

int data;

cNode* next;

};

struct dNode//doubly link list node

dNode* prev;

int data;

dNode* next;

};

class list

private:

sNode* sHead;
sNode* sCur;

sNode* st;

dNode* dHead;

dNode* dCur;

dNode* dt;

cNode* cHead;

cNode* cCur;

cNode* ct;

public:

list()

sHead=NULL;

dHead=NULL;

cHead=NULL;

void menu(list &l)

int ch,n;

ty:

system("cls");

cout<<"1.Simply Linked list\n";

cout<<"2.Doubly Linked list\n";

cout<<"3.Circular Linked list\n";

cout<<"4.Exit\n";

cin>>ch;

if(ch==1)

{
int c,d;

sm:

system("cls");

cout<<"1.Create\n";

cout<<"2.Traverse\n";

cout<<"3.Delete\n";

cout<<"4.back\n";

cin>>c;

if(c==1)

cout<<"Enter Value:";

cin>>n;

l.sList(n);

goto sm;

else if(c==2)

l.sTraverse();

getch();

goto sm;

else if(c==3)

cout<<"Enter node to be deleted:";

cin>>d;

l.sDel(d);

goto sm;
}

else if(c==4)

goto ty;

else if(ch==2)

int c,d;

string t;

dm:

system("cls");

cout<<"1.Create\n";

cout<<"2.Traverse\n";

cout<<"3.Delete\n";

cout<<"4.Back\n";

cin>>c;

if(c==1)

cout<<"Enter Value:";

cin>>n;

l.dList(n);

goto dm;

else if(c==2)

cout<<"Traverse Forward(f) or Backward(b):";

cin>>t;
if(t=="f"||t=="F") {l.dTraverse("f");}

else if(t=="b"||t=="b") {l.dTraverse("b");}

getch();

goto dm;

else if(c==3)

cout<<"Enter node to be deleted:";

cin>>d;

l.dDel(d);

goto dm;

else if(c==4)

goto ty;

else if(ch==3)

int c,d;

cm:

system("cls");

cout<<"1.Create\n";

cout<<"2.Traverse\n";

cout<<"3.Delete\n";

cout<<"4.Back\n";
cin>>c;

if(c==1)

cout<<"Enter Value:";

cin>>n;

l.cList(n);

goto cm;

else if(c==2)

l.cTraverse();

goto cm;

else if(c==3)

cout<<"Enter node to be deleted:";

cin>>d;

l.cDel(d);

goto cm;

else if(c==4)

goto ty;

else if(ch==4)

exit(0);

}
}

void sList(int n)//create simply link list

if(sHead==NULL)

sHead=new sNode;

sHead->data=n;

sHead->next=NULL;

else

sCur=sHead;

while(sCur->next!=NULL)

sCur=sCur->next;

st=new sNode;

st->data=n;

st->next=NULL;

sCur->next=st;

void sTraverse()//tarverse simply link list

sCur=sHead;

cout<<"Data of Simply Link list:";

while(sCur->next!=NULL)
{

cout<<sCur->data<<"\t";

sCur=sCur->next;

cout<<sCur->data<<"\t"<<endl;

void sDel(int dk)//delete node of simplylink list

sCur=sHead;

for(int i=1;i<dk;i++)

sCur=sCur->next;

st=sCur->next;

delete sCur;

sCur=sHead;

for(int i=1;i<dk-1;i++)

sCur=sCur->next;

sCur->next=st;

void cList(int n)//create circular link list

if(cHead==NULL)
{

cHead=new cNode;

cHead->data=n;

cHead->next=cHead;

else

cCur=cHead;

while(cCur->next!=cHead)

cCur=cCur->next;

ct=new cNode;

ct->data=n;

ct->next=cHead;

cCur->next=ct;

void cTraverse()//tarverse circular link list

cCur=cHead;

cout<<"Data of Circular Link list:";

while(cCur->next!=cHead)

cout<<cCur->data<<"\t";

cCur=cCur->next;

cout<<cCur->data<<"\t"<<endl;
}

void cDel(int dk)//delete node of circular link list

cCur=cHead;

for(int i=1;i<dk;i++)

cCur=cCur->next;

ct=cCur->next;

delete cCur;

cCur=cHead;

for(int i=1;i<dk-1;i++)

cCur=cCur->next;

cCur->next=ct;

void dList(int n)//create doubly link list

if(dHead==NULL)

dHead=new dNode;

dHead->data=n;

dHead->next=NULL;

dHead->prev=NULL;
}

else

dCur=dHead;

while(dCur->next!=NULL)

dCur=dCur->next;

dt=new dNode;

dt->data=n;

dt->next=NULL;

dt->prev=dCur;

dCur->next=dt;

void dTraverse(string c)//tarverse doubly link list

dCur=dHead;

if(c=="f"||c=="F")

dCur=dHead;

cout<<"Data of Doubly Link list:";

while(dCur->next!=NULL)

cout<<dCur->data<<"\t";
dCur=dCur->next;

cout<<dCur->data<<"\t"<<endl;

else if(c=="b"||c=="B")

dCur=dHead;

cout<<"Data of Doubly Link list:";

while(dCur->next!=NULL)

dCur=dCur->next;

while(dCur->prev!=NULL)

cout<<dCur->data<<"\t";

dCur=dCur->prev;

cout<<dCur->data<<"\t"<<endl;

void dDel(int dk)

dCur=dHead;

for(int i=1;i<dk;i++)
{

dCur=dCur->next;

dt=dCur->next;

delete dCur;

dCur=dHead;

for(int i=1;i<dk-1;i++)

dCur=dCur->next;

dt->prev=dCur;

dCur->next=dt;

};

main()

list l;

l.menu(l);

STACK IMPLEMENTATION USING ARRAY

#include<iostream>

#include<conio.h>

using namespace std;


#define size 5

int stk[size];

int top=-1;

void push(int n)

if(top==-1)

top++;

stk[top]=n;

else if(top<size)

top++;

stk[top]=n;

else

cout<<"stack is full"<<endl;

void pop()

if(top==-1)

cout<<"Emmpty";

else
{

top--;

stk[top];

void disp()

for(int i=top;i>=0;i--)

cout<<stk[i]<<endl;

main()

int n;

for(int i=0;i<7;i++)

cout<<"Enter value::";

cin>>n;

push(n);

disp();

pop();

pop();

cout<<endl<<endl<<endl;

disp();

getch();

}
STACK IMPLEMENTATION USING Linked List
#include<iostream>

using namespace std;

struct node

int data;

node* next;

};

node* top=NULL;

class rStack

private:

node* cur;

node* temp;

public:

void push(int n)

temp=new node;

temp->data=n;

if(top==NULL)

temp->next=NULL;

top=temp;

else
{

temp->next=top;

top=temp;

void pop()

if(top==NULL)

cout<<"Stack is empty!\n";

else

cur=top;

top=top->next;

delete(cur);

void traverse()

if(top==NULL)

cout<<"Stack is empty!\n";

else

cur=top;
while(cur->next!=NULL)

cout<<"Dispalying stack:"<<cur->data<<" ";

cur=cur->next;

cout<<cur->data<<endl;

};

main()

rStack obj;

obj.push(1);

obj.push(2);

obj.traverse();

obj.pop();

obj.pop();

obj.traverse();

QUEUE IMPLEMENTATION USING ARRAY


#include<iostream>

#include<conio.h>

using namespace std;

#define size 3

int que[size];

int front=-1;

int rare=-1;
void enque(int n)

if(front==-1)

front++;

rare=front;

que[rare]=n;

else if(rare>=size)

cout<<"Full"<<endl;

else

rare++;

que[rare]=n;

void deque()

if(rare==-1)

cout<<"Empty";

front=-1;

rare=-1;

else if(front==rare)

{
front=-1;

rare=-1;

else

front++;

void disp()

for(int i=front;i<=rare;i++)

cout<<que[i]<<endl;

main()

enque(5);

enque(6);

enque(7);

enque(8);

enque(9);

disp();

cout<<endl<<endl;

deque();

deque();

deque();

deque();
deque();

disp();

getch();

Circular QUEUE IMPLEMENTATION USING LINKED LIST


#include<iostream>

#include<conio.h>

using namespace std;

struct node

int data; node* next;//link


};

node* front=NULL; node* rear=NULL;

class cQueue

private:

node* cur;

node* temp;

public:

cQueue()

cur=NULL; temp=NULL;

void menu()

int ch; mm:


system("cls"); cout<<"1.Enqueue\n";

cout<<"2.Dequeue\n";

cout<<"3.Traverse\n";

cout<<"4.Front\n";

cout<<"5.Empty\n";
cout<<"6.Exit\n";

cin>>ch;

if(ch==1)
{
int n;

cout<<"Enter value:";

cin>>n;

enQueue(n);

cout<<"Value Recorded succesfully!\nPress any


key to continue!\n";

getch();

goto mm;

else if(ch==2)

cout<<"Front value dequeued!\nPress any key to


continue!\n";
deQueue();

getch();

goto mm;

else if(ch==3)

{
cout<<"Displaying Queue:";

disp();

cout<<"\nPress any key to continue!\n";


getch();

goto mm;

else if(ch==4)

cout<<"Displaying Queue's first value:";

cFront();

cout<<"\nPress any key to continue!\n";

getch();

goto mm;

else if(ch==5)

isEmpty();

cout<<"\nPress any key to continue!\n";

getch();

goto mm;

else if(ch==6)

exit(0);

}
void enQueue(int n)

temp=new node;

temp->data=n;
if(front==NULL)//empty
{

temp->next=temp;

rear=temp;

front=rear;

else

rear->next=temp;

temp->next=front;

rear=temp;

void deQueue()

if(front==NULL)

cout<<"Queue is empty!\n";

if(front==rear)

delete(front);

front=NULL;

rear=NULL;
}

else

cur=front;

front=front->next;

rear->next=front;

delete(cur);

void cFront()

if(front==NULL)

cout<<"Queue is empty!\n";

else

cout<<front->data<<endl;

void isEmpty()

if(front==NULL)

cout<<"Queue is empty!\n";

else
{

cout<<"Queue is not empty\n";

void disp()

if(front==NULL)

cout<<"Queue is Empty!\n";

}
else
{

cur=front; while(cur->next!=front)
{
cout<<cur->data<<"\t"; cur=cur-

>next;

}
cout<<cur->data<<"\t"; cout<<"\n";

}
}

};

main()
{

cQueue obj; obj.menu();

BINARY SEARCH TREE USING LINKED LIST


#include<iostream>

using namespace std;

struct bTree

int data;

bTree *left;

bTree *right;

};

bTree *creat(int data)

bTree *temp;

temp=new bTree;

temp->data=data;

temp->left=NULL;

temp->right=NULL;

return temp;

void Insert(bTree* &Root,int n)

if(Root==NULL)

Root=creat(n);

}
else if(n<Root->data)

Insert(Root->left,n);

else

Insert(Root->right,n);

int binSerch(bTree* &Root,int n)

if(Root->data==n)

cout<<"Found";

else

if(n<Root->data)

while(Root->left!=NULL)

if(Root->left->data==n)

cout<<"Found\n";

break;

else

Root=Root->left;

}
}

else

while(Root->right!=NULL)

if(Root->right->data==n)

cout<<"Found\n";

break;

else

Root=Root->right;

void min(bTree *Root)

cout<<Root->data<<" ";

void preDisp(bTree *Root)

if(Root!=NULL)

preDisp(Root->left);

cout<<Root->data;
preDisp(Root->right);

main()

int n;

bTree *Root;

Root=NULL;

for(int i=1;i<=5;i++)

cin>>n;

Insert(Root,n);

preDisp(Root);

cout<<endl;

binSerch(Root,10);

min(Root);

return 0;

BST IMPLEMENTATION USING LINKED LIST


#include<iostream>

#include<conio.h>

using namespace std;

#define size 100

void inorder(int[],int);

void creatBST(int no[],int data,int index);

main()
{

int no[size];

int i,data;

char c;

for(i=1;i<size;i++)

no[i]=-1;

do

cout<<"Enter value ::";

cin>>data;

creatBST(no,data,1);

cout<<"Wish to continue: y / n \n" ;

c=getch();

while(c=='y'||c=='Y');

inorder(no,1);

void creatBST(int no[],int data,int index)

if(index>=size)

cout<<"Space is not availabe";

else

if(no[index]==-1)

no[index]=data;
}

else

if (data<no[index])

creatBST(no,data,index*2);

else

creatBST(no,data,(index*2)+1);

void inorder(int no[],int index)

if(no[index]!=-1)

inorder(no,index*2);

cout<<no[index]<<endl;

inorder(no,(index*2)+1);

AVL TREE IMPLEMENTATION USING LINKED LIST


#include<iostream>

#include <cstdlib>

using namespace std;

struct node

int key;
node* left;

node* right;

int hight;

};

int high(node* N)

if(N==NULL)

return 0;

return N->hight;

int max(int a,int b)

return (a>b)? a : b;

node *creat(int data)

node *temp;

temp=new node;

malloc (sizeof(node));

temp->key=data;

temp->left=NULL;

temp->right=NULL;

temp->hight=1;

return temp;

node* leftR(node *x)


{

node *y = x->right;

node *T2 = y->left;

x->right=T2;

y->left=x;

x->hight=max(high(x->left),high(x->right))+1;

y->hight=max(high(y->left),high(y->right))+1;

return y;

node* rightR(node *y)

node *x = y->left;

node *T2 = x->right;

x->right=y;

y->left=T2;

x->hight=max(high(x->left),high(x->right))+1;

y->hight=max(high(y->left),high(y->right))+1;

return x;

int getbalance(node* N)

if(N==NULL)

return 0;

return high(N->left) - high(N->right);


}

node* Insert(node* Root,int n)

if(Root==NULL)

return (creat(n));

if(n<Root->key)

Root->left=Insert(Root->left,n);

else if (n>Root->key)

Root->right=Insert(Root->right,n);

else

return Root;

Root->hight=1+ max(high(Root->left),high(Root->right));

int balance= getbalance(Root);

if(balance > 1 && n < Root->left->key)

return rightR(Root);

if(balance < -1 && n > Root->right->key)

return leftR(Root);
if(balance > 1 && n < Root->left->key)

Root->left=leftR(Root->left);

return rightR(Root);

if(balance < -1 && n > Root->right->key)

Root->right=rightR(Root->right);

return leftR(Root);

return Root;

void preDisp(node *Root)

if(Root!=NULL)

preDisp(Root->left);

cout<<Root->key<<endl;

preDisp(Root->right);

main()

node *root;

root=NULL;

root=Insert(root,10);
root=Insert(root,20);

root=Insert(root,30);

root=Insert(root,40);

root=Insert(root,50);

root=Insert(root,25);

preDisp(root);

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