Saturday, 9 May 2015

STACK

#include<stdio.h>
#include<conio.h>

struct stack{
	int *ary;
   int k;
   int size;
   stack(int x){
   	k=-1;
      ary=new int(x);
      size=x;
   }
   void push(int x){
   	ary[++k]=x;
   }
   int pop(){
   	return ary[k--];
   }
   int sfull(){
   	if(k<size-1)
      	return 1;
      else
      	return 0;
   }
   int sempty(){
   	if(k<0)
      	return 0;
      else
      	return 1;
   }
   int stacktop(){
   	return ary[k];
   }
};
int main(){
   int m;
   printf("Enter the size of the stack: \a");
	scanf("%d",&m);
	stack s(m);

   for(int i=0;i<2;i++){
   	printf("Enter the value you want to push on stack: ");
      scanf("%d",&m);
      s.push(m);
   }
	printf("\n\n\t\t\tvalue on top of stack is: %d\n\n",s.stacktop());
   printf("Value popped from stack is: %d\n",s.pop());

   while(s.sfull()==1){
	   printf("Enter the value you want to push on stack: ");
      scanf("%d",&m);
      s.push(m);
   }
   printf("\n\n\t\t\tvalue on top of stack is: %d\n\n",s.stacktop());

   while(s.sempty()==1){
   	printf("Value popped from stack is: %d\n",s.pop());
   }
   getch();
}

DOUBLE LINKED LIST

#include<stdio.h>
#include<conio.h>
struct Node{
	int data;
   Node *next;
   Node * prev;
   Node(int a){
   	data=a;
   	next=NULL;
      prev=NULL;
   }
};

struct llist{
	Node *front;
   Node *rear;
   llist(){
   	front=NULL;
      rear=NULL;
   }
   void add(int x){
   	Node *temp=new Node(x);

      if(front==NULL){
      	front=temp;
         rear=temp;
      }
      else{
      	rear->next=temp;
         temp->prev=rear;
         rear=temp;
      }
   }
   void display(){
   	Node *f=front;
      if(f==NULL){
         printf("\nNothing is added to list...\n");
      	return;
      }
      while(f!=NULL){
      	printf("Value of the Node is: %d\n",f->data);
         f=f->next;
      }
   }
   void rev_display(){
   	Node *f=rear;
      if(f==NULL){
         printf("\nNothing is added to list...\n");
      	return;
      }
      while(f!=NULL){
      	printf("Value of the Node is: %d\n",f->data);
         f=f->prev;
      }
   }
   void average(){
   	Node *f=front;
      if(f==NULL){
         printf("\nNothing is added to list...\n");
      	return;
      }
      int total=0;
      int count=0;
      while(f!=NULL){
         total+=f->data;
         count++;
      	printf("Value of the Node is: %d\n",f->data);
         f=f->next;
      }
      printf("\nAverage salary is: %d\n",total/count);
   }
   void ndelete(int x){
   	Node *f=front,*g;
      if(f==NULL){
         printf("\nNothing is added to list...\n");
      	return;
      }
      if(front->data==x){
      	front=front->next;
         front->prev=NULL;
         delete f;
      }
      else{
      	while(f->data!=x && f!=NULL){
         	g=f;
            f=f->next;
         }
         if(f->data==x){
         	g->next=f->next;

            if(f->next !=NULL)
            	(f->next)->prev=g;
            if(rear->data==x)
            	rear=rear->prev;
            /*f->prev=NULL;
            f->next=NULL;*/
            delete f;
         }
      }
   }
   void nndelete(){
   	Node*f=front;
      if(f==NULL){
         printf("\nNothing is added to list...\n");
      	return;
      }

   	while(front!=NULL){
   		f=front;
   		front=front->next;
         //front->prev=NULL;
         delete f;
   }
   }
   void search(int x){
   	Node *f=front;
      if(f==NULL){
         printf("\nNothing is added to list...\n");
      	return;
      }
      while(f!=NULL){
      	if(f->data==x){
         	printf("\nNode found\n");
            break;
         }
         f=f->next;
      }
          if(f==NULL)
             	printf("\nNode not found...\n");
   }

};
int main(){
	llist ls;
   int m;
   for(int i=0;i<10;i++){
   	printf("Enter the value you want to add: ");
      scanf("%d",&m);
      ls.add(m);
   }
   ls.rev_display();
   printf("\nDeleting middle node . . . \n");
   ls.ndelete(50);
   ls.display();
   printf("\nDeleting first node . . . \n");
   ls.ndelete(10);
   ls.display();
   printf("\nDeleting last node . . . \n");
   ls.ndelete(100);
   ls.display();
   printf("Enter the value you want to search: ");
      scanf("%d",&m);
   ls.search(m);
   ls.average();
   ls.nndelete();
   ls.display();
   getch();



}