Pages

Custom Search
Showing posts with label lifo. Show all posts
Showing posts with label lifo. Show all posts

Thursday, November 3, 2011

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));  
         }  
     }  
 }  

Wednesday, October 5, 2011

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]);
}
}
}


Custom Search