Posts

Showing posts from 2019

Program to print the sequence 1-2+3-4+5-6+......n

Program to print the sequence 1-2+3-4+5-6+......n #include<iostream> using namespace std; main() {int n,sum=0; cout<<"Enter number "; cin>>n; cout<<"The sequence is "; for(int i=1;i<=n;i++) {if(i%2==0) {sum-=i; cout<<"-"<<i; } else { cout<<"+"<<i; sum+=i;} } cout<<"\nAnd the sum is "<<sum; }

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