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

C Program to Draw Line in Graphics Mode

C Program to Draw Line in Graphics Mode.


C Program to Draw Line in Graphics Mode Images-Pics-Pictures-Photos-Basic C Programs-C Language Programs-C Program to draw a Line in Graphics Mode-General C Programs-Program for Line-Line Program-Graphics Programs.

⦁ InitGraph initializes the graphics system by loading a graphics driver from disk then putting the system into graphics mode

⦁ The syntax for line function is as follows:
                 
line(x1,y1,x2,y2);
          x1 - X Co-ordinate of first point.
          y1 - Y Co-ordinate of first point.
          x2 - X Co-ordinate of second point.
          y2 - Y Co-ordinate of second point.

⦁ closegraph( ) function switches back the screen from graphics mode to text mode.


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

#include<graphics.h>
#include<stdio.h>
int main(void)
{
    int gdriver = DETECT, gmode;
    int x1 = 200, y1 = 200;
    int x2 = 300,y2 = 300;
    initgraph(&gdriver, &gmode,"c:\\tc\\bgi");
    line(x1,y1,x2,y2);
    closegraph( );
}

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

Explanation

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

int x1=200, y1=200;
int x2=300, y2=300;

Using these variables we can keep track of starting and ending point.
line(x1,y1,x2,y2);

We need to pass 4 parameters to the line( ) function.

Line function draws line from (x1,y1) to (x2,y2)

C Program to Print all Arguments passed using Command Line

C Program to Print all Arguments passed using Command Line.


C Program to Print all Arguments passed using Command Line Images-Pictures-Pics-Photos-C Language Programs-C Program to print all arguments-Command line arguments-General C Programs-Command line arguments parameters-Graphics Programs.

⦁ The command line arguments are handled using main( ) function arguments where argc refers to the number of arguments passed, and argv[ ] is a pointer array which points to each argument passed to the program.

⦁ The declaration for command line arguments is as follows:
                   
int main(int argc, char *argv[ ])

⦁ argc - is the count of total command line arguments passed to executable on the time of execution.

⦁ argv - is the array of character string of each command line argument passed to executable on execution.

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

#include<stdio.h>
int main(int args, char *argv[ ])
{
    int i=0;
    for(i=0;i<args;i++)
    printf("\n %s", argv[i]);
    return 0;
}

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

Explanation

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

Steps to Run the Program

⦁ Save, Compile and Run the Program.

⦁ Open Command Prompt.

⦁ Move to the directory where program is saved.

⦁ Type the following command.

C:&gt;TC&gt;BIN&gt;Example1 Monika Kushal

C:&gt;TC&gt;BIN&gt;Example1.exe

Monika

Kushal


C Program to Draw Inductive Coil using Graphical Functions

C Program to Draw Inductive Coil using Graphical Functions

C Program to Draw Inductive Coil using Graphical Function.

⦁ InitGraph initializes the graphics system by loading a graphics driver from disk then putting the system into graphics mode

⦁ closegraph( ) function switches back the screen from graphics mode to text mode.

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

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main( )
{
   int gd = DETECT, gm;
   initgraph(&gd, &gm, "c:\\tc\\bgi");
   line(100,200,150,200);
   arc(200,200,0,180,50);
   arc(260,200,0,180,50);
   arc(230,200,180,360,20);
   arc(320,200,0,180,50);
   arc(290,200,180,360,20);
   arc(380,200,0,180,50);
   arc(350,200,180,360,20);
   line(430,200,470,200);
   getch( );
   closegraph( );
}

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

Output

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

Displays the Inductive Coil.

C Program to Draw Resistance using Graphics Function

C Program to Draw Resistance using Graphics Function.


C Program to Draw Resistance using Graphics Function Images-Pictures-Pis-Photos-Basic C Programs-C Language Programs-C Program to draw a Resistor in Graphics Mode-General C Programs-Program for Resistor-Resistor Program-Graphics Programs.

⦁ InitGraph initializes the graphics system by loading a graphics driver from disk then putting the system into graphics mode

⦁ outtextxy(x,y,"Resistor");

⦁ It is similar to printf statement. Prints text on a screen in Text Mode while outtextxy( ) function prints Text onto screen in Graphics Mode. This function accepts 3 parameters.

⦁ X Co-ordinate - Specifies X Co-ordinate

⦁ Y Co-ordinate - Specifies Y Co-ordinate

⦁ Text - String/Text to be printed on the specified Co-ordinates.

⦁ closegraph( ) function switches back the screen from graphics mode to text mode.


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

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main( )
{
    int gd = DETECT,gm;
    initgraph(&gd, &gm,, "c:\\tc\\bgi");
    line(90,100,100,100);
    line(100,100,110,120);
    line(110,120,120,100);
    line(120,100,130,120);
    line(130,120,140,100);
    line(140,100,150,120);
    line(150,120,160,100);
    line(160,100,170,100);
    outtextxy(100,130,"Resistor");
    getch( );
    closegraph( );
}

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

Output

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

Displays the Resistor

C Program to Draw PNP Transistor using Graphics Function

C Program to Draw PNP Transistor using Graphics Function

C Program to Draw PNP Transistor using Graphics Function.

⦁ InitGraph initializes the graphics system by loading a graphics driver from disk then putting the system into graphics mode.

⦁ closegraph( ) function switches back the screen from graphics mode to text mode.

⦁ outtextxy(x,y,"PNP Transistor");

⦁ It is similar to printf statement. Prints text on a screen in Text Mode while outtextxy( ) function prints Text onto screen in Graphics Mode. This function accepts 3 parameters.

⦁ X Co-ordinate - Specifies X Co-ordinate.

⦁ Y Co-ordinate - Specifies Y Co-ordinate.

⦁ Text - String/Text to be printed on the specified Co-ordinates.

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

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

void main( )
{
   int gd = DETECT, gm;
   initgraph(&gd, &gm, "c:\\tc\\bgi");
   line(100,100,100,200);
   line(70,150,100,150);
   line(100,125,150,90);
   line(100,175,150,210);
   line(100,175,110,200);
   line(100,175,125,175);
   outtextxy(200,150,"PNP Transistor");
   getch( );
   closegraph( );
}

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

Output

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

Displays PNP Transistor


C Program to Draw Eclipse using Graphics Function

C Program to Draw Eclipse using Graphics Function.


C Program to Draw Eclipse using Graphics Function Images-Pictures-Pics-Snaps-Snapshots-Basic C Programs-C Language Programs-C Program to draw a eclipse in Graphics Mode-General C Programs-Program for eclipse-eclipse Program-Graphics Programs.

⦁ InitGraph initializes the graphics system by loading a graphics driver from disk then putting the system into graphics mode

⦁ The declaration for ellipse function is as follows:
           
void ellipse(int xcenter, int ycenter, int startAngle, int endAngle, int xRadius, int yRadius);

      xcenter - X coordinate of center of eclipse.
      ycenter - Y coordinate of center of eclipse.
      startAngle - Start angle of the eclipse arc.
      endAngle - End angle of the eclipse arc.
      xRadius - Horizontal radius of the eclipse.
      yRadius - Vertical radius of the eclipse.

⦁ closegraph( ) function switches back the screen from graphics mode to text mode.

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

#include<graphics.h>
#include<conio.h>
main( )
{
   int gd = DETECT,gm;
   initgraph(&gd, &gm, "c:\\tc\\bgi");
   eclipse(100,100,0,360,50,25);
   getch( );
   closegraph( );
   return 0;
}

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

Output

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

Displays Eclipse

C Program to Draw NPN Transistor using Graphics Function

C Program to Draw NPN Transistor using Graphics Function.


C Program to Draw NPN Transistor using Graphics Function Images-Pictures-Pics-Photos-Basic C Programs-C Language Programs-C Program to draw a NPN Transistor in Graphics Mode-General C Programs-Program for NPN Transistor-NPN Transistor Program-PNP Transistor Program-Graphics Programs.

⦁ InitGraph initializes the graphics system by loading a graphics driver from disk then putting the system into graphics mode

⦁ closegraph( ) function switches back the screen from graphics mode to text mode.


⦁ outtextxy(x,y,"NPN Transistor");

⦁ It is similar to printf statement. Prints text on a screen in Text Mode while outtextxy( ) function prints Text onto screen in Graphics Mode. This function accepts 3 parameters.

⦁ X Co-ordinate - Specifies X Co-ordinate

⦁ Y Co-ordinate - Specifies Y Co-ordinate

⦁ Text - String/Text to be printed on the specified Co-ordinates.

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

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main( )
{
   int gd = DETECT,gm;
   initgraph(&gd, &gm, "c:\\tc\\bgi");
   line(100,100,100,200);
   line(70,150,100,150);
   line(100,125,150,90);
   line(100,175,150,210);
   line(140,190,150,210);
   line(130,210,150,210);
   outtextxy(100,250,"NPN Transistor");
   getch( );
   closegraph( );
}

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

Output

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

Displays NPN Transistor



Kategori

Kategori