Pages

Custom Search

Friday, November 4, 2011

Program to implement dynamically allocated Queue using linked list.



 #include <stdio.h>  
 #include <stdlib.h>  
 struct tag  
 {  
     int data;  
     struct tag *link;  
 };  
 typedef struct tag * NODE;  
 void ins_rnode();  
 void del_fnode();  
 void display();  
 int main()  
 {  
     int ch, element, dnum;  
     NODE front = NULL;  
     NODE rear = NULL;  
     do  
     {  
         printf("\nOPERATIONS ON QUEUE: ");  
         printf("\n1.) INSERT");  
         printf("\n2.) DELETE");  
         printf("\n3.) DISPLAY");  
         printf("\n4.) EXIT");  
         printf("\nEnter your choice: ");  
         scanf("%d", &ch);  
         switch(ch)  
         {  
             case 1: printf("\nEnter element to insert into the queue: ");  
                   scanf("%d", &element);  
                   ins_rnode(&front, &rear, element);  
                   break;  
             case 2: del_fnode(&front);  
                   break;  
             case 3: display(&front);  
                   break;  
             case 4: break;  
             default: printf("\nWrong choice!! Enter choice again.");  
         }  
     }while(ch!=4);  
 }  
 void ins_rnode(NODE *front, NODE *rear, int ele)  
 {  
     NODE newnode, temp;  
     temp = *front;  
     newnode = (NODE)malloc(sizeof(struct tag));  
     if(newnode == NULL)  
     {  
         printf("\nInsufficient memory.");  
         return;  
     }   
     newnode->data = ele;  
     newnode->link = NULL;  
     if(*front == NULL && *rear == NULL)  
     {  
         *front = newnode;  
         *rear = newnode;  
     }  
     else  
     {  
         while(temp->link != NULL)  
         {  
             temp = temp->link;  
         }  
         temp->link = newnode;  
         *rear = newnode;  
     }  
 }  
 void del_fnode(NODE *front)  
 {  
     NODE c;  
     if(*front == NULL)  
     {  
         printf("\nQueue is empty");  
         return;  
     }  
     c = *front;  
     printf("\nDeleted node: %d\n", c->data);  
     *front = c->link;  
     free(c);  
 }  
 void display(NODE *front)  
 {  
     NODE temp;  
     temp = *front;  
     if(*front == NULL)  
     {  
         printf("\nQueue is empty");  
         return;  
     }  
     printf("\nQueue is: ");  
     while(temp != NULL)  
     {  
      printf("%d ", temp->data);  
         temp = temp->link;  
     }  
 }  

Program to implement dynamically allocated Stack using linked list.


 #include <stdio.h>  
 #include <stdlib.h>  
 struct tag  
 {  
     int data;  
     struct tag *link;  
 };  
 typedef struct tag * NODE;  
 void push_node();  
 void pop_node();  
 void display();  
 int main()  
 {  
     int ch, element, dnum;  
     NODE top = NULL;  
     do  
     {  
         printf("\n1.) PUSH");  
         printf("\n2.) POP");  
         printf("\n3.) DISPLAY");  
         printf("\n4.) EXIT");  
         printf("\nEnter your choice: ");  
         scanf("%d", &ch);  
         switch(ch)  
         {  
             case 1: printf("\nEnter element to push: ");  
                   scanf("%d", &element);  
                   push_node(&top, element);  
                   break;  
             case 2: pop_node(&top);  
                   break;  
             case 3: display(top);  
                   break;  
             case 4: break;  
             default: printf("\nWrong choice!! Enter choice again.");  
         }  
     }while(ch!=4);  
 }  
 void push_node(NODE *top, int ele)  
 {  
     NODE newnode, temp;  
     temp = *top;  
     newnode = (NODE)malloc(sizeof(struct tag));  
     if(newnode == NULL)  
     {  
         printf("\nInsufficient memory");  
         return;  
     }  
     newnode->data = ele;  
     if(*top == NULL)  
     {  
         newnode->link = NULL;  
         *top = newnode;  
     }  
     else  
     {  
         newnode->link = *top;  
         *top = newnode;  
     }  
 }  
 void pop_node(NODE *top)  
 {  
     NODE c;  
     if(*top == NULL)  
     {  
         printf("\nStack is empty");  
         return;  
     }  
     c=*top;  
     if(c->link == NULL)  
     {  
         printf("\nPopped node: %d", c->data);  
         free(c);  
         *top = NULL;  
         return;  
     }  
     *top = c->link;  
     printf("\nPopped node: %d", c->data);  
     free(c);  
 }  
 void display(NODE top)  
 {  
     NODE temp;  
     temp = top;  
     if(top == NULL)  
     {  
         printf("\nStack is empty");  
         return;  
     }  
     printf("\nStack is: \n");  
     while(temp != NULL)  
     {  
         printf("\n%d", temp->data);  
         temp = temp->link;  
     }  
 }  

Thursday, November 3, 2011

Program to implement dynamically allocated Queue.


Dynamic allocation of memory is different from the static memory allocation. Static memory allocation is where we allocate more memory than required initially. It may be so that we may use the entire allocated memory or fall short of memory or use less memory and hence in any of the above cases memory is not managed properly.
So the solution for such a problem is the method of allocating memory during run time. There are four functions which help us to allocate, reallocate and deallocate memory as per our needs and requirements during run time. They are:
a.) Malloc    [Allocates a single block of memory]
b.) Calloc    [Allocates a contiguous block of memory initially filled with zero's]
c.) Realloc  [Reallocates memory whenever required, may be more or less]
d.) Free      [Deallocates previously allocated memory]

Source Code: 

 #include <stdio.h>  
 #include <stdlib.h>  
 #define realloc(p,n)             // Macro definition for realloc function  
 void insert();  
 void deleteq();  
 void display();  
 int capacity = 1;  
 int rear = -1;  
 int front = 0;  
 int main()  
 {  
     int *p;  
     int choice, ele;  
     p = (int*)malloc(sizeof(int)); //allocating memory to pointer  
     do  
     {  
         printf("\n\nOperations on stack: \n");  
         printf("\n1.) PUSH");  
         printf("\n2.) POP");  
         printf("\n3.) DISPLAY");  
         printf("\n4.) EXIT");  
         printf("\nEnter your choice: ");  
         scanf("%d", &choice);  
         switch(choice)  
         {  
             case 1: printf("\nEnter an element to push: ");  
                   scanf("%d", &ele);  
                   insert(&p, ele);  
                   break;  
             case 2: deleteq(&p);  
                   break;  
             case 3: display(p);  
                   break;  
             case 4: break;  
             default: printf("\nInvalid choice. Enter again...");  
         }  
     }while(choice!=4);  
     free(p);  
 }  
 void insert(int **p, int ele)  
 {  
     if(rear == capacity-1)  //checking if allocated memory is full  
     {    //if full reallocate memory by doubling capacity size  
         capacity*=2;  
         realloc(p, capacity);  
         printf("\nCapacity increased");  
     }  
     rear++;  
     *(*p+rear) = ele;  //storing value in pointer  
 //   printf("\nCapacity: %d", capacity);   remove comment to understand changes in the capacity values  
 }  
 void deleteq(int **p)  
 {  
     if(front > rear)  
     {  
         printf("\nQueue empty");  
     }  
     else  
     {  
         printf("\nDeleted element: %d", *(*p+front));  
         front++;  
         capacity--;  
         realloc(p, capacity);  
 //       printf("\nCapacity: %d", capacity);   remove comment to understand changes in the capacity values  
     }  
 }  
 void display(int *p)  
 {  
     int i;  
     if(front > rear)  
     {  
         printf("\nQueue is empty");  
     }  
     else  
     {  
         printf("\nContents of Queue are: ");  
         for( i=front ;i<=rear ;i++)  
         {  
             printf("%d  ", *(p+i));  
         }  
     }  
 }  

Program to implement dynamically allocated Stack.

Dynamic allocation of memory is different from the static memory allocation. Static memory allocation is where we allocate more memory than required initially. It may be so that we may use the entire allocated memory or fall short of memory or use less memory and hence in any of the above cases memory is not managed properly.
So the solution for such a problem is the method of allocating memory during run time. There are four functions which help us to allocate, reallocate and deallocate memory as per our needs and requirements during run time. They are:
a.) Malloc    [Allocates a single block of memory]
b.) Calloc    [Allocates a contiguous block of memory initially filled with zero's]
c.) Realloc  [Reallocates memory whenever required, may be more or less]
d.) Free      [Deallocates previously allocated memory]

Source Code: 

 #include <stdio.h>  
 #include <stdlib.h>  
 #define realloc(p,n)           // Macro definition for realloc function  
 void push();  
 void pop();  
 void display();  
 int capacity = 1;  
 int top = -1;  
 int main()  
 {  
     int *p;  
     int choice, ele;  
     p = (int*)malloc(sizeof(int));           //allocating memory to pointer  
     do  
     {  
         printf("\n\nOperations on stack: \n");  
         printf("\n1.) PUSH");  
         printf("\n2.) POP");  
         printf("\n3.) DISPLAY");  
         printf("\n4.) EXIT");  
         printf("\nEnter your choice: ");  
         scanf("%d", &choice);  
         switch(choice)  
         {  
             case 1: printf("\nEnter an element to push: ");  
                   scanf("%d", &ele);  
                   push(&p, ele);  
                   break;  
             case 2: pop(&p);  
                   break;  
             case 3: display(p);  
                   break;  
             case 4: break;  
             default: printf("\nInvalid choice. Enter again...");  
         }  
     }while(choice!=4);  
     free(p);        // Allocated Memory freed  
 }  
 void push(int **p, int ele)  
 {  
     if(top == capacity-1)          //checking if allocated memory is full  
     {                     //if full reallocate memory doubling capacity size  
         capacity*=2;  
         realloc(p, capacity);  
         printf("\nCapacity increased");  
     }  
     top++;  
     *(*p+top) = ele;      //storing value in pointer  
 //    printf("\nCapacity: %d", capacity);  remove comment to understand changes in the capacity values  
 }  
 void pop(int **p)  
 {  
     if(top == -1)  
     {  
         printf("\nStack underflow");  
     }  
     else  
     {  
         printf("\nPopped element: %d", *(*p+top));  
         (top)--;  
         capacity--;  
         realloc(p, capacity);  
 //       printf("\nCapacity: %d", capacity);  remove comment to understand changes in the capacity values  
     }  
 }  
 void display(int *p)  
 {  
     int i;  
     if(top == -1)  
     {  
         printf("\nStack is empty");  
     }  
     else  
     {  
         printf("\nContents of Stack are: \n");  
         for( i=top ;i>=0 ;i--)  
         {  
             printf("\n%d", *(p+i));  
         }  
     }  
 }  

Monday, October 31, 2011

Program to find Octal of a number [ C++ ].

Problem Statement:
 Design, develop, and execute a program in C++ to create a class
called OCTAL, which has the characteristics of an octal number.
Implement the following operations by writing an appropriate
constructor and an overloaded operator +.
i.) OCTAL h = x ; where x is an integer
ii.) int y = h + k ; where h is an OCTAL object and k is an
integer.
Display the OCTAL result by overloading the operator <<. 
Also display the values of h and y.





Source Code: 


 #include <iostream.h>  
 #include <math.h>  
 class OCTAL  
 {  
     int data;  
     public:  
     OCTAL(int t = 0)               //Constructor  
     {  
         int num, rem, n=0, oct;  
         data = t;  
         num = data;  
         oct = 0;  
         while(num)  
         {  
             rem = num % 8;  
             oct = oct + rem * pow(10, n);  
             num = num / 8;  
             n++;  
         }  
         data = oct;  
     }  
     int operator +(int t)           // Overloaded + operator  
     {  
         int num, rem, n=0, oct;  
         int y = data + t;  
         num = y;  
         oct=0;  
         while(num)  
         {  
             rem = num % 8;  
             oct = oct + rem * pow(10, n);  
             num = num / 8;  
             n++;  
         }  
         y=oct;  
         return (y);  
     }  
     friend ostream & operator <<(ostream & dout, OCTAL h)  // Overloaded <<(insertion) operator  
     {  
         dout<<h.data;  
         return (dout);  
     }  
 };  
 int main()  
 {  
     int x, k, y;  
     cout<<"\nEnter a number to get its octal: ";  
     cin>>x;  
     cout<<"\nEnter value of k: ";  
     cin>>k;  
     OCTAL h = x;  
     cout<<"\nValue of x: "<<x;  
     y = h + k;  
     cout<<"\nOctal of "<<x<<": ";  
     cout<<h;  
     cout<<"\nOctal of y(h+k): "<<y<<endl;   
 }  

SAMPLE OUTPUTS:



Sunday, October 16, 2011

Program to insert and delete node at the end of Doubly Linked List




 #include <stdio.h>  
 #include <conio.h>  
 struct tag  
 {  
     struct tag* llink;  
     int data;  
     struct tag* rlink;  
 };  
 typedef struct tag * NODE;  
 void ins_rear();  
 void del_rear();  
 void display();  
 int main()  
 {  
     NODE first=NULL;  
     int choice, ele;  
     do  
     {  
         printf("\nDoubly linked list operations: ");  
         printf("\n1.) Insert front");  
         printf("\n2.) Delete front");  
         printf("\n3.) Display");  
         printf("\n4.) Exit");  
         printf("\n\nEnter your choice: ");  
         scanf("%d", &choice);  
         switch(choice)  
         {  
             case 1: printf("\nEnter element to insert at rear: ");  
                 scanf("%d", &ele);  
                 ins_rear(&first, ele);  
                 break;  
             case 2: del_rear(&first);  
                 break;  
             case 3: display(&first);  
                 break;  
             case 4: break;  
             default: printf("\nWrong choice!! Enter choice again.");  
         }  
     }while(choice != 4);  
     getch();  
 }  
 //Function to insert node at rear   
 void ins_rear(NODE *first, int ele)  
 {  
     NODE newnode, temp;  
     newnode = (NODE)malloc(sizeof(struct tag));  
     if(newnode == NULL)  
     {  
         printf("\nInsufficient memory");  
         return;  
     }              //checking if memory is allocated successfully  
     newnode->data = ele;  
     newnode->rlink = NULL;  
     if(*first == NULL)        //If list is empty  
     {  
         newnode->llink = NULL;  
         *first = newnode;  
     }  
     else        //If list contains one or more nodes  
     {  
         temp = *first;  
         while(temp->rlink != NULL)     //Traversing Doubly L.L  
         {  
             temp = temp->rlink;  
         }  
         temp->rlink = newnode;  
         newnode->llink = temp;  
     }  
 }  
 //Function to delete rear node  
 void del_rear(NODE *first)  
 {  
     NODE temp;  
     if(*first == NULL)  
     {  
         printf("\nDoubly linked list is empty");  
     }  
     else if((*first)->rlink == NULL)     //If only one node is present  
     {  
         temp= *first;  
         printf("\nDeleted node: %d", temp->data);  
         *first = NULL;  
         free(temp);  
     }  
     else        //If more than one nodes are present  
     {  
         temp = *first;  
         while(temp->rlink != NULL)  
         {  
             temp = temp->rlink;  
         }  
         printf("\nDeleted node: %d", temp->data);  
         temp->llink->rlink = NULL;  
         free(temp);  
     }  
 }  
 //Function to display contents of doubly linked list  
 void display(NODE *first)  
 {  
     NODE temp;  
     if(*first == NULL)  
     {  
         printf("\nDoubly linked list is empty");  
     }  
     else  
     {  
         temp = *first;  
         while(temp!=NULL)  
         {  
             printf("%d ", temp->data);  
             temp=temp->rlink;  
         }  
     }  
 }  

Saturday, October 15, 2011

Singly linked list (in C++) to insert and delete node at front.


//Singly Linked list in C++...
//Inserting and deleting a node from front.


 #include <iostream>  
 #include <cstdlib>  
 using namespace std;  
 class NODE  
 {  
     public:  
     int data;  
     NODE* link;  
 };  
 class LIST  
 {  
     NODE* first;  
     public:  
     LIST();  
     ~LIST();  
     void ins_front(int);  
     void del_front();  
     void display();  
 };  
 LIST::LIST()         //Constructor  
 {  
     first = NULL;  
 }  
 LIST::~LIST()           //Destructor  
 {  
     cout<<"\nDestroyed\n";  
 }  
 //Function to insert node at front  
 void LIST::ins_front(int inum)  
 {  
     NODE* newnode;  
     newnode = (NODE*)malloc(sizeof(NODE));     //Allocating memory to newnode  
     newnode->data= inum;  
     (newnode->link) = first;  
     first = newnode;  
 }  
 //Function to delete front node  
 void LIST::del_front()  
 {  
     NODE* temp;  
     if(first == NULL)  
     {  
         cout<<"\nSingly Linked list is empty";  
         return;  
     }  
     if(first->link == NULL)      //to check if only one node is present  
     {  
         temp = first;  
         first = NULL;  
         cout<<"\nDeleted node: "<<temp->data;  
         free(temp);  
      }  
     else             //If more than one nodes are present  
     {  
         temp = first;  
         first = first->link;  
         cout<<"\nDeleted node: "<<temp->data;  
         free(temp);  
      }  
 }  
 void LIST::display()  
 {  
     NODE* temp;  
     if(first == NULL)  
     {  
         cout<<"\nSingly Linked list is empty";  
         return;  
     }  
     temp = first;  
     cout<<"\nSingly linked list is:\n";  
     while(temp!=NULL)  
     {  
         cout<<temp->data<<" ";  
         temp = temp->link;  
     }  
 }  
 int main()  
 {  
     LIST obj;         //Creating object of the class LIST  
     int choice, inum;  
  do  
  {  
         cout<<"\n\nSingly linked list operations: ";      //Menu for operations on linked list  
         cout<<"\n1.) Insert node at front";  
         cout<<"\n2.) Delete front node";  
         cout<<"\n3.) Display";  
         cout<<"\n4.) Exit";  
         cout<<"\n\nEnter your choice: ";  
         cin>>choice;  
         switch(choice)  
         {  
             case 1: cout<<"\nEnter element to insert at the front: ";  
                   cin>>inum;  
                   obj.ins_front(inum);  
                   break;  
             case 2: obj.del_front();  
                   break;  
             case 3: obj.display();  
                   break;  
             case 4: break;  
             default: cout<<"\nInvalid choice!! Enter choice again.";  
         }  
     }while(choice != 4);  
 }  

Tuesday, October 11, 2011

Program to generate superprime numbers in a given range.


//Program to generate superprimes in a given range and display the number of superprimes.
//leave space between lower limit and upper limit

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

void superprime();                   //Function declaration

int main()
{
int  sn, en;
printf("\nEnter range of numbers: ");
scanf("%d %d", &sn, &en);

superprime(sn, en);                 //Function call

getch();
}
void superprime(int sn, int en)
{
int a, digit, d=2, flag=1, f=1, t, i, c=0;;

for(i=sn;i<=en;i++)
{
t=i;
flag=1;
d=2;
while(d<t)
{
if((t%d)==0)
{
flag=0;
break;
}
d++;
}
if(flag==1)
{
while(t>0)
{
a=2;
digit=t%10;
f=1;
while(a<digit)
{
if((digit%a)==0)
{
f=0;
break;
}
a++;
}
t=t/10;
if(f==0)
break;
}
if(f==1)
{
printf("%d\t", i);
c++;
}
}
}
printf("\n\nNumber of superprimes: %d\n", c);
}

SAMPLE OUTPUT:



Program to generate armstrong numbers in a given range.


//Generate armstrong number in a given range.
Leave space between the upper and the lower range.

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

void range_arm();                    //Function declaration

int main()
{
        int sn, en;
        printf("\nEnter range of numbers: ");
        scanf("%d %d", &sn, &en);
        range_arm(sn, en);                   //Function call
        getch();
}
void range_arm(int sn, int en)
{
        int t, cube, scube=0, d, i, c=0;

        for(i=sn;i<=en;i++)
        {
                scube=0;
                t=i;
                while(t>0)
                {
                        d=t%10;
                        cube=d*d*d;
                        scube=scube+cube;
                        t=t/10;
                }
                 if(scube==i)
                {
                        printf("%d\t", i);
                        c++;
                }
        }
        if(c==0)
        {
                printf("\nNo armstrong numbers in the given range");
        }
        printf("\n");
}

SAMPLE OUTPUT:


Program to check whether entered number is armstrong or not.


//An armstrong number is an number in which the sum of cubes of the digits of the number equals the number.
For eg: 153 = 1(1's cube) + 125(5's cube) + 27(3's cube)

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

void armstrong();                             //Function declaration

void main()
{
        int num, a;
        printf("\nEnter an integer number: ");
        scanf("%d", &num);
        armstrong(num);                  //Function call
        getch();
}
void armstrong(int p)
{
        int t, cube, scube=0, d;
        t=p;
        while(p>0)
{
                d=p%10;                     //Separating digit's
                cube=d*d*d;               //Cube of separated digit
                scube=scube+cube;     //Adding cube to sum of cube(scube)
                p=p/10;
        }
        if(scube==t)
                printf("\nGiven number is armstrong\n");
        else
                printf("\nGiven number is not armstrong\n");
        return;
}

SAMPLE OUTPUT:


Program to check whether the entered number is superprime or not.


//For a number to be a superprime number the first condition is that it should be a prime number. If the number is a prime number, then for it to become superprime the digits in the prime number should also be prime.
For ex: 19 is a prime number; 1 is a prime but 9 is not a prime. Hence 19 is not a superprime number.
            13 is a prime number; 1 and 3 both are prime numbers. Hence 13 is a superprime number.

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

void superprime(int *p);

int main()
{
        int num;
        printf("\nEnter an integer number: ");
        scanf("%d", &num);
        superprime(&num);
        getch();
}
void superprime(int *p)
{
        int a, digit, d=2, flag=1, f=1, t;
        t=*p;

        while(d<*p)
        {
                if((*p%d)==0)
                {
                        flag=0;
                        break;
                }
                d++;
         }
                if(flag==1)
                {
                        f=1;
                        while(t>0)
                        {
                                a=2;

                                digit=t%10;
                                while(a<digit)
                                {
                                         if((digit%a)==0)
                                         {
                                                f=0;
                                                break;
                                         }
                                         a++;
                                }
                                t=t/10;
                        }
                 }

        if(f==1&&flag==1)
                printf("\nGiven number is superprime\n");
        else if(flag==0)
                printf("\nGiven number is not prime\n");
        else if(f==0&&flag==1)
                printf("\nGiven number is prime but not superprime");
        else
                printf("\nGiven number is not prime\n");
        return;
}

SAMPLE OUTPUT:



Monday, October 10, 2011

Program to check if entered string is palindrome or not using stack.


/*A string is said to be palindrome when the reverse of the string is same as that of the original string
   For ex: Entered string: madam... is a palindrome*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define SIZE 10

typedef struct
{
        int items[SIZE];
        int top;

}STACK;


void push();
int pop();
void display();
int isoverflow();
int isempty();

int main()
{
        STACK s;
        char str[100];
        int i, flag;

        s.top = -1;

        printf("\nEnter a string: ");
        gets(str);

        for(i=0;str[i]!='\0'; i++)
                  push(&s, str[i]);

        flag = 1;
        for(i=0;str[i]!='\0';i++)
        {
                if(str[i] != pop(&s))
                {
                        flag = 0;
                        break;
                }
        }

if(flag == 1)
                printf("\nEntered string is palindrome\n");
        else
                printf("\nEntered string is not a palindrome\n");

}

void push(STACK *p, int element)
{
           if(isoverflow(p->top))
           {
                    printf("\nStack is overflow");
           }
           else
          {
                  (p->top)++;
                   p->items[p->top] = element;
          }
}

int pop(STACK *p)
{
         if(isempty(p->top))
         {
                  printf("\nStack is underflow");
         }
         else
         {
                  return (p->items[(p->top)--]);
         }
}

void display(STACK s)
{
        int i;
        if(isempty(s.top))
        {
printf("\nStack is empty");
        }
        else
        {
                for(i=s.top; i>=0; i--)
                {
                        printf("\n%d", s.items[i]);
                }
        }
}

//Function to check stack overflow condition
int isoverflow(int top)
{
        if(top == SIZE - 1)
                return (1);
        else
                return (0);
}

//Function to check stack empty condition
int isempty(int top)
{
         if(top == -1)
                return (1);
        else
                return (0);
}

SAMPLE OUTPUT:




Program to print entered numbers in words


//For ex: if enetered number is '1234' the output will be "One Two Three Four"

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

void main()
{
        int digit, num, rev=0, n, m, i, count=0;
        printf("\nEnter an integer: ");
        scanf("%d", &num);
        m=num;
        while(num>0)
        {
                digit=num%10;
                count++;
                num=num/10;
                rev=rev*10+digit;
        }
        n=rev;
        rev=0;
        if(m%10!=0)
        {


                while(n>0)
                {
                        digit=n%10;
                        switch(digit)
                        {
                                case 1 : printf(" One");
                                            break;
                                case 2 : printf(" Two");
                                            break;
                                case 3 : printf(" Three");
                                            break;
                                case 4 : printf(" Four");
                                            break;
                                case 5 : printf(" Five");
                                            break;
                                case 6 : printf(" Six");
                                             break;
                                case 7 : printf(" Seven");
                                             break;
                                case 8 : printf(" Eight");
                                             break;
                                case 9 : printf(" Nine");
                                             break;
                                case 0 : printf(" Zero");
                                             break;
 
                        }
                        n=n/10;
                        rev=rev*10+digit;
                }
        }
        else
        {
                for(i=0;i<count;i++)
                {
                        digit=n%10;
                        switch(digit)
                        {
                                case 1 : printf(" One");
                                             break;
                                case 2 : printf(" Two");
                                             break;
                                case 3 : printf(" Three");
                                            break;
                                case 4 : printf(" Four");
                                             break;
                                case 5 : printf(" Five");
                                             break;
                                case 6 : printf(" Six");
                                             break;
                                case 7 : printf(" Seven");
                                             break;
                                case 8 : printf(" Eight");
                                             break;
                                case 9 : printf(" Nine");
                                             break;
                                case 0 : printf(" Zero");
                                             break;
                        }
                         n=n/10;
                }
        }
         printf("\n");
        printf("\nPress ENTER to EXIT\n");
        getch();
}

SAMPLE OUTPUT:

Reversing a Stack using Queue.


#include <stdio.h>
#include <stdlib.h>
#define SIZE 5

struct tag
{
        int a[SIZE];
        int top;
};                                                                 //Structure definition for stack
typedef struct tag stack;


struct tagg
{
        int items[SIZE];
        int rear;
        int front;
};                                                                  //Structure definition for queue
typedef struct tagg QUEUE;


void push();
int pop();
void display();
void rev();                       //Function declarations(parameters not required at declaration)


int main()
{
        int ch, element, dnum;
        stack st;
        st.top= -1;

        do
        {                                                                 //Displaying menu for operations on stack
                printf("\n1.) PUSH");
                printf("\n2.) POP");
                printf("\n3.) DISPLAY");
                printf("\n4.) REVERSE");
                printf("\n5.) EXIT");
                printf("\nEnter your choice: ");
                scanf("%d", &ch);

                switch(ch)
                {
                        case 1: printf("\nEnter element to push: ");
                                    scanf("%d", &element);
                                    push(&st, element);
                                    break;

                        case 2: dnum = pop(&st);
                                    if(dnum != -1)
                                    printf("\nDeleted element: %d", dnum);
                                    break;

                        case 3: display(st);
                                    break;

                        case 4: rev(&st);
                                    break;

                        case 5: break;

                        default: printf("\nWrong choice!! Enter choice again.");
                  }
         }while(ch!=5);

}

void push(stack *s, int ele)
{
        if(s->top == SIZE-1)
        {
                printf("\nStack is overflow");
        }
        else
        {
                (s->top)++;
                s->a[s->top] = ele;;
        }
}

int pop(stack *s)
{
        if(s->top == -1)
        {
                printf("\nStack is underflow");
                return(-1);
        }
        else
        {
                return(s->a[(s->top)--]);
        }
}

void display(stack s)
{
        int i;
        if(s.top == -1)
        {
               printf("\nStack is empty");
        }
        else
        {
                for(i=s.top; i>=0; i--)
                {
                           printf("\n%d", s.a[i]);
                }
         printf("\n");
         }
}

//Function to reverse a stack using queue
void rev(stack *s)
{
        QUEUE q;
        q.rear = -1;
        q.front = 0;

        if(s->top==-1)
        {
                printf("\nStack empty");
                return;
        }

        while((s->top) > -1)
        {
                 q.items[++(q.rear)] = s->a[(s->top)--];
        }

        while(q.front <= q.rear)
        {
                  s->a[++(s->top)] = q.items[(q.front)++];
        }

}

Wednesday, October 5, 2011

Multiple Stack

Implementation of multiple stacks in a single linear array

//Include the necessary header files

#define SIZE 12 //Size of array(Vary as per need)
#define MAX 4 //Maximum number of stacks to store(vary as per need)

typedef struct
{
     int stk[SIZE];
     int top[MAX];
     int boundary[MAX];
}STACK;

void push();
void pop();
void display();

int main()
{
     STACK s;
     int choice, i, j, n;
     s.top[0] = s.boundary[0]=-1;
     printf("\nEnter the number of stacks you want to implement: ");
     scanf("%d", &n);
     for(i=1 ; i < n ; i++)
     {
            s.top[i] = s.boundary[i] = ((SIZE/n)*i)-1;
     }

     s.boundary[n] = SIZE-1;
     do
     {
            printf("\n\nOperation on three stacks:");
            printf("\n\n1.) Push");
            printf("\n2.) Pop");
            printf("\n3.) Display");
            printf("\n4.) Exit");
            printf("\n\nEnter your choice: ");
            scanf("%d", &choice);
            switch( choice )
            {
case 1: printf("\nEnter the stack number in which element is to be pushed(1- %d): ", n);
scanf("%d", &j);
if(j>n || j<1)
break;
push(&s, j);
break;
case 2: printf("\nEnter the stack number to pop element from(1-%d): ", n); scanf("%d", &j);
if(j>n || j<1);
break;
pop(&s, j);
break;

case 3: printf("\nEnter the stack number to display(1-%d): ", n);
scanf("%d", &j);
if(j>n || j<1)
break;
display(s, j);
break;
case 4: break;
default: printf("\nInvalid choice! Enter again.");
}
     }while(choice!=4);

     getch();
}

void push(STACK *p, int j)
{
  int i;
i=j-1;
if(p->top[i] == p->boundary[j])
{
printf("\nStack%d is overflow", j);
}
else
{
(p->top[i])++;
printf("\nEnter the element to push: ");
scanf("%d", &p->stk[p->top[i]]);
}
}

void pop(STACK *p, int j)
{
int i;
i=j-1;
if(p->top[i] == p->boundary[i])
{
printf("\nStack%d is underflow", j);
}
else
{
printf("\nPopped element: %d", p->stk[(p->top[i])--]);
}
}

void display(STACK s, int j)
{
int i, k;
i=j-1;
if(s.top[i] == s.boundary[i])
{
printf("\nStack%d is empty", j);
}
else
{
printf("\nStack%d is: \n", j);
for(k=s.top[i]; k>s.boundary[i]; k--)
{
printf("\n%d", s.stk[k]);
}
}
}


Two stacks in a single linear array.

Stack is a data structure in which last inserted element is the last deleted element. The term 'push' is used to insert an element, 'pop is used to delete an element. Stack is also known as LIFO(Last In First Out).
In this program we are implementing two stacks in a single linear array and the two stacks grow towards each other i.e., first stack starts from the front of the array and the second stack starts from the end of the array. And the total number of elements in both the stacks can vary.

/*Include the necessary header files*/

#define MAX 10

typedef struct
{
int stack[MAX]; //Array of integers
int top1;
int top2;
}STACK;



void push1();
void pop1();
void display1();
void push2();
void pop2();
void display2(); //Function prototypes


int main()
{
STACK s;
int choice;
s.top1 = -1;
s.top2 = MAX;
do
{
printf("\n\nOperation on two stacks:");
printf("\n\nStack 1\t\t\tStack 2");
printf("\n__________\t\t__________");
printf("\n\n1.) Push1\t\t4.) Push2");
printf("\n2.) Pop1\t\t5.) Pop2");
printf("\n3.) Display1\t\t6.) Display2");
printf("\n7.) Exit");
printf("\n\nEnter your choice: ");
scanf("%d", &choice);
switch( choice )
{
case 1: push1(&s);
break;
case 2: pop1(&s);
break;
case 3: display1(s);
break;
case 4: push2(&s);
break;
case 5: pop2(&s);
break;
case 6: display2(s);
break;
case 7: break;
default: printf("\nInvalid choice! Enter again.");
}
}while(choice!=7);
getch();
}

void push1(STACK *p)
{
if(p->top1 == (p->top2) - 1)
{
printf("\nStack1 is overflow");
}
else
{
(p->top1)++;
printf("\nEnter the element to push: ");
scanf("%d", &p->stack[p->top1]);
}
}

void pop1(STACK *p)
{
if(p->top1 == -1)
{
printf("\nStack1 is underflow");
}
else
{
printf("\nPopped element: %d", p->stack[(p->top1)--]);
}
}

void display1(STACK s)
{
int i;
if(s.top1 == -1)
{
printf("\nStack1 is Empty");
}
else
{
printf("\nStack1 is: \n");
for(i=s.top1; i>=0; i--)
{
printf("\n%d", s.stack[i]);
}
}
}

void push2(STACK *p)
{
if(p->top2 == p->top1 + 1)
{
printf("\nStack2 is overflow");
}
else
{
(p->top2)--;
printf("\nEnter the element to push: ");
scanf("%d", &p->stack[p->top2]);
}
}

void pop2(STACK *p)
{
if(p->top2 == MAX)
{
printf("\nStack2 is underflow");
}
else
{
printf("\nPopped element: %d", p->stack[(p->top2)++]);
}
}

void display2(STACK s)
{
int i;
if(s.top2 == MAX)
{
printf("\nStack2 is Empty");
}
else
{
printf("\nStack2 is: \n");
for(i=s.top2 ; i < MAX ; i++)
{
printf("\n%d", s.stack[i]);
}
}
}


ORDINARY QUEUE:

Queue is data structure in which the first inserted element is the first deleted element. It is also known as a FIFO(First In First Out). The program implementation using structure is as follows.


/*Include the necessary header files(stdio.h, stdlib.h, conio.h)*/

#define QSIZE 5 //Maximum elements in Queue

struct tag //structure definition
{
int items[QSIZE];
int rear;
int front;
};
typedef struct tag QUEUE;

void insert();
int Delete();
void display(); //Function Prototypes

int main()
{
QUEUE q;
int element, dnum, choice;
q.rear = -1;
q.front = 0;

do
{
printf("\n\nOperations on Ordinary Queue: ");
printf("\n\n1.) INSERT");
printf("\n\n2.) DELETE");
printf("\n\n3.) DISPLAY");
printf("\n\n4.) QUIT");
printf("\n\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("\nEnter the element into queue: ");
scanf("%d", &element);
insert(&q, element); //Function call by reference
break;
case 2: dnum = Delete(&q); // Function call by reference
if(dnum != -1)
{
printf("\nDeleted element: %d", dnum);
}
break;

case 3: display(q); //Function call by value
break;
case 4: break;
default: printf("\nInvalid choice! Enter again.");
}
}while(choice!=4);
getch();
}

//Function to insert elements into the queue

void insert(QUEUE *q, int element)
{
if(q->rear == QSIZE - 1)
{
printf("\nQueue is full.");
return;
}
else
{
(q->rear)++;
q->items[q->rear] = element;
}
}

//Function to delete an element from the front of queue.

int Delete(QUEUE *q)
{
if(q->front > q->rear)
{
printf("\nQueue is empty.");
return(-1);
}
else
{
return(q->items[(q->front)++]);
}
}

//Function to display the contents of queue from front to rear.

void display(QUEUE q)
{
int i;
if(q.front > q.rear)
printf("\n\nQueue empty.");
else
{
printf("\nQueue is: \n\n");
for(i=q.front; i<=q.rear; i++ )
{
printf("%d ", q.items[i]);
}
}
}

Custom Search