Stack Operations Using Pointer


C Program to Perform Stack Operations Using Pointer.



C Program to Perform Stack Operations Using Pointer Images-Pics-Photos-Wall Papers-Basic C Programs-C Language Programs-C Program to Perform Stack Operations-Stack Operations using Pointer-Pointers-General C Programs-Stack Program-Linked List Programs-Push Pop Operations.


·        Stack is a linear data structure in which the operations are performed based on LIFO principle.

·        In a Stack, adding and removing of elements are performed at single position which is known as "top".

·       The insertion operation is performed using a function called push and deletion operation is performed using a function called pop.

·        A Pointer is a variable whose value is the address of another variable, that is direct address of the memory location.
---------------------------------------------------------------------------------------------------------

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 50
int size;
struct stack
{
    int arr[MAX];
    int top;
}
void init_stk(struct stack *st)
{
   st->top = -1;
}
void push(struct stack *st, int num)
{
   if(st->top == size-1)
   {
       printf("\n Stack Overflow");
       return;
   }
   st->top++;
   st->arr[st->top] = num;
}
int pop(struct stack *st)
{
    int num;
    if(st->top == -1)
    {
          printf("\n Stack Underflow");
          return NULL;
    }
    num = st->arr[st->top];
    syt->top--;
    return num;
}
void display(struct stack *st)
{
    int i;
    for(i=st->top;i>=0;i--)
    printf("\n %d",st->arr[i]);
}
int main( )
{
     int element, opt, val;
     struct stack ptr;
     init_stk(&ptr);
     printf("\n Enter Stack Size: ");
     scanf("%d", &size);
     while(1)
     {
           printf("\n\n Stack Operations");
           printf("\n1. Push");
           printf("\n2.Pop");
           printf("\n3.Display");
           printf("\n4.Quit");
           printf("\n");
           printf("\n Enter Option : ");
           scanf("%d",&opt);
           switch(opt)
          {
                case 1:
                printf("\n Enter the element into stack");
                scanf("%d", &val);
                push(&ptr, val);
                break;
                case 2:
                element = pop(&ptr);
                printf("\n The element popped from stack is                                                                                                   %d",element);
                break;
                case 3:
                printf("\n The current stack elemengts are : ");
                display(&ptr);
                break;
                case 4:
                exit(0);
                default:
                printf("\n Enter correct option! Try again.");
}
}
return(0);
}

Output
---------------------------------------------------------------------------------------------------------
Displays Stack Operations
---------------------------------------------------------------------------------------------------------
Explanation
·        Declaring the stack with MAX size
            struct stack
            {
                  int arr[MAX];
                  int top;
            };

·        Initializing the top location of stack to -1.

·        We are passing the structure to the function using pointer so we can see struct stack as data type in the function call.
               void init_stk(struct stack *st)
               {
                    st-&gt;top = -1;
               }

·        Access the top element of stack using
         st-&gt;top

·        Access the 0th element of the stack array using
         st-&gt;arr[0]

·        Access the top element using
         st-&gt;arr[st-&gt;top]

·        Perform Push and Pop Operations.


Stack Operations Using Array

C Program to Implement Stack Operations Using Array.



C Program to Implement Stack Operations Using Array Images-Pics-Pictures-Photos-Basic C Programs-C Language Programs-C Program to Perform Stack Operations-Stack Operations using Array-General C Programs-Stack Program-Linked List Programs-Push Pop Operations.

Stack is a liner data structure in which the operations are performed based on LIFO principle.

In a Stack, adding and removing of elements are performed at single position which is known as "top".

The insertion operation is performed using a function called push and deletion operation is performed using a function called pop.

An array is a variable which can store multiple values of same data type at a time.


C Program to Implement Stack Operations Using Array Images-Pics-Pictures-Photos-Basic C Programs-C Language Programs-C Program to Perform Stack Operations-Stack Operations using Array-General C Programs-Stack Program-Linked List Programs-Push Pop Operations.




--------------------------------------------------------------------------------------------------------------------------


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
struct stack
{
    int s[size];
    int top;
} st;
int stfull( )
{
   if(st.top>=size-1)
   return 1;
   else
   return 0;
}
void push(int item)
{
    st.top++;
    st.s[st.top] = item;
 }
int stempty( )
{

   if(st.top == -1)
   return 1;
   else
   return 0;
}
int pop( )
{
    int item;
    item = st.s[st.top];
    st.top--;
    return(item);
}
void display( )
{
    int i;
    if(stempty( ))
    printf("\n Stack is Empty!");
    else
    {
         for(i=st.top;i>=0;i--)
         printf("\n %d", st.s[i]);
    }

}

int main( )
{
    int item,choice;
    char ans;
    st.top = -1;
    printf("\n\t Implementation of Stack");
    do
    {
         printf("\n Main Menu");
         printf("\n1.Push \n2.Pop\n3. Display\n4.exit");
         printf("\n Enter your choice");
         scanf("%d", &choice);
         switch(choice)
         {
                case 1:
                printf("\n Enter the item to be pushed");
                scanf("%d", &item);
                if(stfull( ))
                printf("\n Stack is Full!");
                else
                push(item);
                break;
                case 2:
                if(stempty( ))
                printf("\n Empty stack! Underflow!!");
                else
                {
                     item = pop( );
                     printf("\n The popped element is %d",item);
                }
               break;
               case 3:
               display( );
               break;
               case 4:
               exit(0);
    }
    printf("\n Do You want to Continue?");
    ans = getche( );
}
while(ans =='Y' || ans == 'Y');
return 0;
}

--------------------------------------------------------------------------------------------------------------------------

Explanation

--------------------------------------------------------------------------------------------------------------------------


We have created stack structure and we have array of elements having size size.

We have declared top as structure member to keep track of Topmost element.

We are incrementing the top and then adding element.
                   
  #define size 5
                      struct stack
                     {
                         int s[size];
                         int top;
                     }st;

While removing or poping element, we are firstly removing the element and then decrementing the top.
     
void push(int item)
      {
          st.top++;
          st.s[st.top] = item;
      }



Stack Operations using Singly Linked List

C Program to Implement Stack Operations using Singly Linked List.


C Program to Implement Stack Operations using Singly Linked List Images-Pictures-Photos-Wall Papers-Pics--Basic C Programs-C Language Programs-C Program to Perform Stack Operations-Stack Operations using Singly Linked List-General C Programs-Stack Program-Push Pop Operations-Linked List Programs

Stack is a linear data structure in which the operations are performed based on LIFO principle.

Single linked list is a sequence of elements in which every element has link to its next element in the sequence.

The individual element is called as Node. Every node contains 2 fields data and next.

The data field is used to store actual value of that node and next field is used to store the address of the next node in the sequence.


C Program to Implement Stack Operations using Singly Linked List Images-Pictures-Photos-Wall Papers-Pics--Basic C Programs-C Language Programs-C Program to Perform Stack Operations-Stack Operations using Singly Linked List-General C Programs-Stack Program-Push Pop Operations-Linked List Programs

--------------------------------------------------------------------------------------------------------------------------


#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#include<alloc.h>
void Push(int,node**);
void Display(node **);
int Pop(node**);
int Sempty(node *);
typedef struct stack
{
    int data;
    struct stack *next;
}node;
void main( )
{
    node *top;
    int data,item,choice;
    char ans,ch;
    clrscr( );
    top = NULL;
    printf("\n Stack using Linked List : nn");
    do{
    printf("\n The main menu");
    printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
    printf("\n Enter Your Choice");
    scanf("%d",&choice);
    switch(choice)
    {
         case 1:
         printf("\n Enter Data");
         scanf("%d",&data);
         push(data,&top);
         break;
         case 2:
         if(Sempty(top))
         printf("\n Stack underflow!");
         else
         {
              item = Pop(&top);
              printf("\n The popped node is %d",ite,m);
         }
         break;
         case 3:
         Display(&top);
         break;
         case 4:
         printf("\n Do You want to Quit?(y/n)");
         ch = getche( );
         if(ch =='y');
         exit(0);
         else
         break;
       }
       printf("\n Do you want to continue?");
       ans = getche( );
       getch( );
       clrscr( );
 }
while(ans =='y' || ans == 'y');
getch( );
}
void Push(int item, node **top)
{
    node *New;
    node *get_node(int);
    New = get_node(Item);
    New->next = *top;
    *top = New;
}
node *get_node(int item)
{
    node *temp;
    temp = (node *)malloc(sizeof(node));
    if(temp == NULL)
    printf("\n Memory cannot be allowed");
    temp->data = item;
    temp->next = NULL;
    return temp;
}
int Sempty(node *temp)
{
   if(temp == NULL)
   return 1;
   else
   return 0;
}
int Pop(node **top)
{
    int item;
    node *temp;
    item = (*top)->data;
    temp = *top;
    *top = (*top)->next;
    free(temp);
    return(item);
}
void Display(node **head)
{
   node *temp;
   temp = *head;
   if(Sempty(temp))
   printf("\n The stack is empty!");
   else
   {
        while(temp! = NULL)
        {
            printf("%d\n",temp->data_);
            temp = temp->next;
        }
}
getch( );
}

--------------------------------------------------------------------------------------------------------------------------

Output

--------------------------------------------------------------------------------------------------------------------------    

Displays Stack Operations.

C Program for Temperature Conversion from Celsius to Fahrenheit

C Program for Temperature Conversion from Celsius to Fahrenheit.


C Program for Temperature Conversion from Celsius to Fahrenheit Images-Pictures-Photos-Pics-Wall Papers-Snaps-Basic C Programs-C Language Programs-C Program to Convert Fahrenheit to Celsius-General C Programs-Convert Temperature-C Program to Convert Celsius to Fahrenheit-K&RC Program.


--------------------------------------------------------------------------------------------------------

#include<stdio.h>

int main( )

{
   float fahrenheit, celsius;
   int lower,upper,step;
   lower = 0;
   upper = 200;
   step = 20;
   printf("C\tF");
   printf("\n---------");
   celsius = lower;
   while(celsius <= upper)
   {
       fahrenheit = (9.0/5.0)*celsius+32.0;
       printf("\n%3.0f%6.1f",celsius,fahrenheit);
       celsius = celsius + step;
   }
return 0;
}

--------------------------------------------------------------------------------------------------------------------------

Output

--------------------------------------------------------------------------------------------------------------------------


C             F
0             32.0
20           68.0
40           104.0
60           140.0
80           176.0
100         212.0




C Program for Temperature Conversion from Fahrenheit to Celsius

C Program for Temperature Conversion from Fahrenheit to Celsius.


C Program for Temperature Conversion from Fahrenheit to Celsius Images-Pics-Pictures-Photos-Snaps-Snapshots-Basic C Programs-C Language Programs-C Program to Convert Fahrenheit to Celsius-General C Programs-Convert Temperature-C Program to Convert Celsius to Fahrenheit-K&RC Program.


--------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>
int main(void)
{
    float fahrenheit, celsius;
    int lower, upper, step;
    lower = 0;
    upper = 200;
    step = 20;
    printf("F\tC")
    printf("\n-------");
    fahrenheit = lower;
    while(fahrenheit <=upper)
    {
        celsius = (5.0/9.0)*(fahrenheit-32.0);
        printf("\n%3.0f%6.1f",fahrenheit,celsius);
        fahrenheit = fahrenheit+step;
     }
return 0;
}

--------------------------------------------------------------------------------------------------------------------------

Output

--------------------------------------------------------------------------------------------------------------------------

F    C
0     17.8
20   -6.7
40    4.4
60    15.6
80    26.7
100  37.8




C Program for Changing Backslash Character Arguments

C Program for Changing Backslash Character Arguments

C Program for Changing Backslash Character Arguments.

#include<stdio.h>
int main(int argc, char *argv[ ])
{
     printf("\nC, Programming\a");
     printf("\nC, Programming\b");
     printf("\nC, Programming\c");
     printf("\nC, Programming\d");
     printf("\nC, Programming\e");
     return 0;
}

--------------------------------------------------------------------------------------------------------------------------

Output

--------------------------------------------------------------------------------------------------------------------------

C, Programming
C, Programming
C, Programmingc
C, Programmingd
C, Programming

C Program to Hide Mouse Pointer - Graphics Programs

C Program to Hide Mouse Pointer.


C Program to Hide Mouse Pointer Images-Pictures-Photos-Pics-Wall Papers-Snaps-Snap shots-Basic C Programs-C Language Programs-C Program to Hide Mouse Pointer in text mode-Mouse support-General C Programs-Mouse Pointer in text mode-Mouse Support-Graphics Programs.

Int86( ) is a C function that allows to call interrupts in the program.

Prototype in dos.h usage is int86 (int intr num, union REGS *inregs, union REGS *outregs)

in and out register must be type of REGS.

REGS is built in UNION declaration in C.

It is defined in the header file <dos.h>

--------------------------------------------------------------------------------------------------------------------------

#include<graphics.h>
#include<conio.h>
#include<dos.h>
void showmouseptr( );
void hidemouseptr( );
union REGS i,0;
int main( )
{
    int count = 1, gDriver = DETECT, gMode;
    initgraph(&gDriver, &gMode, "C:\\tc\\bgi");
    i.x.ax = 0;
    int86(0X33, &i,&o);
    if(0.x.ax ==0)
    {
       printf("Mouse Support is Unavailable!!");
    }
    else
    {
       showmouseptr( );
       while(count<=10)
       {
          getch( );
          count++;
          if(count%2 ==0)
          hidemouseptr( );
          else
          showmouseptr( );
      }
}
getch( );
return 0;
}
void showmouseptr( )
{
   i.x.ax = 1;
   int 86(0X33, &i, &o);
}
void hidemouseptr( )
{
    i.x.ax = 2;
    int 86(0X33, &i, &o);
}

--------------------------------------------------------------------------------------------------------------------------

Output

--------------------------------------------------------------------------------------------------------------------------

Mouse Support is Unavailable.

--------------------------------------------------------------------------------------------------------

Explanation

--------------------------------------------------------------------------------------------------------------------------


i.x.ax = 0;
int86(0X33,&i,&0);
Will check whether the mouse driver exists or not.
Int 33,0x01 - Show Mouse Cursor
Int 33,0x02 - Hide Mouse Cursor

Kategori

Kategori