Posts

Showing posts from December, 2018

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...