Stack(using linked list)

#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
};
node* push(node* top);
node* pop(node* top);
void display(node* top);
void main()
{
clrscr();
int choice;
node* top;
top=NULL;
do
{
cout<<” 1 for push \n 2 for pop \n 3 for display \n 4 for quit”;
cin>>choice;
switch(choice)
{
case 1:
top= push(top);
break;

case 2: top=pop(top);
break;
case 3: display(top);
break;
}
}
while(choice!=4);
getch();
}
node* push(node* top)
{
node* x;
x=new node;
cout<<“enter value”;
cin>>x->data;
x->next=top;
top=x;
return top;
}
node* pop(node* top)
{ int v;
if(top==NULL)

{
cout<<“stack empty”;
return top;
}
else
{
node *x;
x=top;
top=top->next;
v=x->data;
cout<<“popped value”<<v;
delete x;
return top;
}
}
void display(node* top)
{
while(top!=NULL)
{
cout<<top->data;
cout<<“\n”;
top=top->next;
}
}

 

Queue( using linked list)

#include<iostream.h>
#include<conio.h>
struct node
{
int info;
node *next;
};
node* insert(node* rear);
node* delet(node* front);
void display(node* front);
int main()
{
node *front,*rear;
front=rear=NULL;
int choice;
do
{
cout<<” enter 1 for insert \n 2 for delete \n 3 for display \n 4 for exit”;
cin>>choice;
switch(choice)
{
case 1:rear=insert(rear);
if(front==NULL)
{
front=rear;
}
break;
case 2:front=delet(front);
if(front==NULL)
{
rear=front;
}
break;
case 3:display(front);
break;
}
}while(choice!=4);
getch();
return 0;
}
node* insert( node* rear)
{
node *temp;
temp=new(node);
cout<<“enter info”;
cin>>temp->info;
temp->next=NULL;
if(rear!=NULL)
{
rear->next=temp;
}
rear=temp;
cout<<“info is “<<rear->info;
return rear;
}
node* delet(node* front)
{
if(front==NULL)
{
cout<<” underflow”;
return front;
}
else
{
node *ptr;
ptr=front;
ptr->info=front->info;
cout<<“deleted value is “<<ptr->info;
front=front->next;
delete ptr;

}
return front;
}
void display(node* front)
{
node *ptr;
ptr=front;
while(ptr!=NULL)
{
cout<<“info is”<<ptr->info;
ptr=ptr->next;
}
}

QUEUE(linear)

#include<iostream.h>
#include<conio.h>
void insert(int queue[],int &rear,int val,int n);
int delet(int queue[],int &front,int &rear );
void display(int queue[],int &front,int &rear);
void main()
{
clrscr();
int queue[50],front,rear,choice,val,p;
front=rear=0;
do
{
cout<<” 1 for insert \n 2 for delet \n 3 for display \n 4 for quit”;
cin>>choice;
switch(choice)
{
case 1: cout<<“enter val”;
cin>>val;
insert(queue,rear,val,30);
break;

case 2: p=delet(queue,front,rear);
if(p!= -9999)
{
cout<<“deleted value is”<<p;
}
break;
case 3: display(queue,front,rear);
break;
}
}
while(choice!=4);
getch();
}
void insert(int queue[],int &rear,int val,int n)
{
if(rear<n)
{

queue[rear++]=val;
}
else{
cout<<“queue full”;
}
}
int delet(int queue[],int &front,int &rear)
{ int v;
if(front==rear)
{
cout<<“queue empty”;
return -9999;
}
else
{ v=queue[++front];
return v;
}
}
void display(int queue[],int &front,int &rear)
{ int i;
if(front<rear)
{
for(i=front+1;i<=rear;i++)
{
cout<<queue[i];
cout<<“\n”;
}
}
}

Stack(linear)

#include<conio.h>
#include<iostream.h>
#include<process.h>
void insert(int[],int,int);
void display(int[],int);
const int size=50;
void main()
{
char ch;
int stack[size],item,top=-1,res;
while(ch==’y’||ch==’Y’)
{
char ch;
cout<<“enter element”;
cin>>item;
insert(stack,top,item);
cout<<“do u want to enter more elements”;
cin>>ch;
}
display(stack,top);
getch();
}

void insert(int stack[],int top,int item)
{
if(top==size-1)
{
cout<<“overflow”;
exit(1);
}
else
{
top++;
stack[top]=item;
}
}
void display(int stack[],int top)
{
int i=0;
for(i=0;i<top;i++)
cin>>stack[i];
}

Circular Doubly Linked List

#include< stdio.h >
#include< stdlib.h >
struct list
{
int num;
struct list *next;
struct list *prev;
};

struct list *node;

void create(struct list *n)
{
char ch;
node=n;
printf(“\nWant to create location(y/n):-“);
scanf(“%c”,&ch);
fflush(stdin);

while(ch!=’n’)
{
fflush(stdin);
node->next=(struct list *)malloc(sizeof(struct list));
node->next->prev=node;
node=node->next;
printf(“\Enter value:-“);
scanf(“%d”,&node->num);
fflush(stdin);
printf(“\Any more (y/n)”);
scanf(“%c”,&ch);
}

node->next=n;
n->prev=node;
}

void display(struct list *n)
{
node=n->next;

while(node!=n)
{
printf(“%d\n”,node->num);
node=node->next;
}

}

void insert(struct list *n)
{
struct list *node,*new1;
int c=1,count;
node=n;
new1=(struct list*)malloc(sizeof(struct list));
printf(“\nEnter the location where the new location will be inserted:”);
scanf(“%d”,&count);
printf(“\Enter value:-“);
scanf(“%d”,&new1->num);

do
{
if(c==count)
break;
node=node->next;
c++;
}while(node!=n);

new1->next=node->next;
node->next->prev=new1;
node->next=new1;
new1->prev=node;
}

void main()
{
struct list *start;
clrscr();
start=(struct list*)malloc(sizeof(struct list));
/* header node without any value */
create(start);
printf(“Now display the value of the circular lists:-\n”);
display(start);
insert(start);
printf(“\nAfter insertion, the list is\n”);
display(start);
getch();
}

Header Linked List

#include< stdio.h >
#include< stdlib.h >
#include< string.h>

struct list
{
char name[100];
struct list *next;
};
struct list *start;

void insert()
{
int c=1,count;
struct list *p,*node,*new1;
printf(“\n which location you want to insert:”);
scanf(“%d”,&count);

node=start;
p=node->next;
new1=(struct list *)stdlib(sizeof(struct list));
printf(“\Enter Name:-“);
scanf(“%s”,&new1->name);

while(p!=NULL)
{
if(c==count)
break;
node=node->next;
p=p->next;
c++;
}

node->next=new1;
new1->next=p;
}

void del()
{
int c=1,count;
struct list *p,*node;
printf(“\nwhich location you want to delete:”);
scanf(“%d”,&count);

node=start;
p=node->next;

while(p->next!=NULL)
{
if(c==count)
break;
node=node->next;
p=p->next;
c++;
}

node->next=p->next;
free(p);
}

void create(struct list *node)
{
char ch;
start=node;
printf(“\nEnter value for the header node (****):-“);
scanf(“%s”,&node->name);
fflush(stdin);
node->next=NULL;
printf(“\nWant to create another node(y/n):-“);
scanf(“%c”,&ch);
fflush(stdin);

while(ch!=’n’)
{
fflush(stdin);
node->next=(struct list *)stdlib(sizeof(struct list));
node=node->next;
printf(“\Enter Name:-“);
scanf(“%s”,&node->name);
fflush(stdin);
node->next=NULL;
printf(“\Any more (y/n)”);
scanf(“%c”,&ch);
}

node->next=start;
/*last node is connected with the header node */
}

void display(struct list *node)
{
int x=0;
node=node->next;

while(x==0)
{
printf(“%s\n”,node->name);
node=node->next;
if(strcmp(node->name,”****”)==0)
x=1;
}

}

void main()
{
struct list *node=(struct list *)stdlib(sizeof(struct list));
clrscr();
create(node);
printf(“Now display the Names of the circular list:-\n”);
display(node);
del();
printf(“\nAfter deletion\n”);
display(node);
insert();
printf(“\nAfter insertion\n”);
display(node);
getch();
}

Standard Deviation and Variance

#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
float array[30],arraysd[30],sum;
int n;

sum=0;
float mean=0.0;
cout<<“enter n”;
cin>>n;
for(int a=0;a<n;a++)
{
cout<<“enter”;
cin>>array[a];
}
for(int b=0;b<n;b++)
{
sum=sum+array[b];
}
cout<<“sum is”<<sum;
mean=sum/n;
cout<<“mean is”<<mean;

for(int c=0;c<n;c++)
{
arraysd[c]=array[c]-mean;
arraysd[c]=arraysd[c]*arraysd[c];
}
float sumsd=0.0;
for(int d=0;d<n;d++)
{
sumsd=sumsd+arraysd[d];
}
float sd=0.0;
sd=sumsd/n;
float variance;
variance=sqrt(sd);
cout<<” sd and variance are”<<sd<<“\t”<<variance;
getch();
return 0;
}

Calendar

July 2012
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Categories

Archives