Posts

C++ program for Circular Queue

Image
Circular queue I n a normal Queue Data Structure, we can insert elements until queue becomes full. But once queue becomes full, we can not insert the next element until all the elements are deleted from the queue. Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. Program: #include<iostream> #include<stdlib.h> using namespace std; int siz,front=0,rear=0; void insert(int q[],int n) {     if(front==1&&rear==siz||front==rear+1)     {cout<<"Overflow ";     exit(0);}     else if (rear==0)         front=rear=1;     else if(rear==siz)         rear=1;     else rear++;         q[rear]=n; } void del(int q[]) {int rak;     if(front==0) ...

C++ program for linked queue

Image
Linked queue The major problem with the queue implemented using array is, It will work for only fixed number of data values. That means, the amount of data must be specified in the beginning itself. Queue using array is not suitable when we don't know the size of data which we are going to use. A queue data structure can be implemented using linked list data structure. The queue which is implemented using linked list can work for unlimited number of values.  Program: #include<iostream> using namespace std; struct node{ int info ; node *next ; }*t; node *front=NULL; node *rear=NULL; void insert(int n) {node *tmp=new node; tmp->info=n; tmp->next=NULL; if(front== NULL)     front=rear=tmp; else{ rear->next=tmp; rear=tmp;} } void del() { node *tmp=new node; tmp=front; front=front->next;//front becomes the next node delete tmp; } void display() { node *f=front;     while(f!=NULL) ...

C++ program for linked stack

Image
Linked stack Using a linked list is one way to implement a stack so that it can handle  essentially  any number of elements. Program: #include<iostream> using namespace std; struct node{ int info ; node *next ; }*t; node *top=NULL; void push(int n) {node *tmp=new node; tmp->info=n; tmp->next=top;//Assigns next pointer of top to tmp top=tmp;//assigns address of tmp to top } void display() { node *f=top;     while(f!=NULL) {cout<<f->info<<"->"; f=f->next;} cout<<"!!!"; } void pop() { node *tmp=new node; tmp=top; top=top->next;//top becomes the next node delete tmp; } main() {int val,p,ch,val1; char c; node *t; do{cout<<"Enter the info to create new node :"; cin>>val; push(val); cout<<"Want to add more : "; cin>>c; }while(c=='y'||c=='Y'); cout<<"Enter your choice \n1.Push \n2.Pop\nChoice:" ; cin>...

C++ program for Queue

Image
Queue A queue is a container of objects (a linear collection) that are inserted and removed according to the first-in first-out (FIFO) principle. An excellent example of a queue is a line of students in the food court.  New additions to a line made to the back of the queue, while removal (or serving) happens in the front.  In the queue only two operations are allowed insertion  and  deletion .  Insertion means to insert an item into the back of the queue. Deletion means removing the front item.  The picture demonstrates the FIFO access. The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added. Program: #include<iostream> #include<stdlib.h> using namespace std; int siz,front=0,rear=0; void insert(int q[],int n) {     if(rear==siz)         {cout<<"Overflow ";exit...

C++ program for Stack

Image
Stack   A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. In the stacks only two operations are allowed: push  the item into the stack, and  pop  the item out of the stack.  A stack is a limited access data structure - elements can be added and removed from the stack only at the top. Push  adds an item to the top of the stack, Pop  removes the item from the top. A helpful analogy is to think of a stack of books; you can remove only the top book, also you can add a new book on the top. Program: #include<iostream> #include<stdlib.h> using namespace std; int siz,del; void push(int stack[],int n,int &top) {     if(top==siz)       {       cout<<"Overflow ";       exit(0);      }     else     {         top++; ...

C++ program for linked lists

Image
  Linked lists  A linked list is a linear data structure where each element is a separate object. Each node  of a list is comprising of two items - the data and a reference to the next node. The last node has a reference to  null .  The entry point into a linked list is called the head  of the list. It should be noted that head is not a separate node, but the reference to the first node. If the list is empty then the head is a null reference. A linked list is a dynamic data structure. The number of nodes in a list is not fixed and can grow and shrink on demand.  One disadvantage of a linked list against an array is that it does not allow direct access to the individual elements. If you want to access a particular item then you have to start at the head and follow the references until you get to that item. Program: #include<iostream> using namespace std; struct node{ int info ; node *next ; }*t; node *head=NU...