SlideShare a Scribd company logo
/* IMPLEMENTATION OF THE STACK USING ARRAYS */
/* STACK_A.C */
# include<stdio.h>
# include<string.h>
# include<ctype.h>
#include<conio.h>
# define size 3

int top = -1;
int flag = 0;
int stack[size];
void push(int *, int);
int pop(int *);
void display(int *);

/* Definition of the push function */
void push(int s[], int d)
{
       if(top ==(size-1))
               flag = 0;
       else
       {       flag = 1;
               ++top;
               s[top] = d;
       }
}

/* Definition of the pop function */
int pop(int s[])
{       int popped_element;
        if(top == -1)
        { popped_element = 0;
                 flag = 0;
        }
        else
        {        flag = 1;
                 popped_element = s[top];
                 --top;
        }
        return (popped_element);
}

/* Definition of the display function */
void display(int s[])
{      int i;
       if(top == -1)
               printf("n Stack is empty");
       else
       { for(i = top; i >= 0; --i)
                        printf("n %d", s[i] );
       }
}
/* Function main */
void main()
{      int data;
       char choice;
       int q = 0;
       int top = -1;
       clrscr();
       do
       {       printf(" n enter i to insert");
               printf(" n enter p to delete");
               printf(" n enter q to quit");
               printf("n enter your choice : ");
               do
               {       choice = getchar();
                       choice =tolower(choice);
               }while(strchr("ipq",choice)==NULL);

               switch(choice)
               {
               case 'i' : printf("n Input the element to push:");
                         scanf("%d", &data);
                         push(stack, data);
                         if(flag)
                         {        printf("n After inserting ");
                                  display(stack);
                                  if(top == (size-1))
                                          printf("n Stack is full");
                         }
                         else printf("n Stack overflow after pushing");
                         break;

              case 'p' : data = pop(stack);
                       if(flag)
                       {        printf("n Data is popped: %d", data);
                                printf("n Rest data in stack is as follows:n");
                                display(stack);
                       }
                       else printf("n Stack underflow" );
                       break;
              case 'q': q = 1;
              }
       } while(!q);
}
/* THIS PROGRAM DEMONSTRATES THE STACK OPERATIONS USING OOPS-
CLASS STACK*/
/* cstack.cpp */

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
 class stack
  { int stack[30];
    int top,n;
    public:
     stack()
     { top=-1; //indicates stack is empty
          n=5;
     }
     void push(int);
     int pop();
     void display();
   };

 void stack::push(int x)
 { if(top<n)
       { top++;
         stack[top]=x;
       }
   else cout<<"n the stack is full";
 }

 int stack::pop()
  { int v=-99;
     if(top>=0)
          { v=stack[top];
            top--;
          }
    return v;
   }
  void stack::display()
   { int i;
         if(top>=0)
           { for(i=top;i>=0;i--)
              cout<<stack[i]<<" ";
           }
         else cout<<"n stack is empty";
    }
  void main()
   { stack s;
     char ch; int x;
     clrscr();
         do
          { cout<<"n enter i to insert an element";
            cout<<"n enter d to view the contents";
            cout<<"n enter r to remove an element";
cout<<"n enter q to quit";
         cout<<"n enter your choice : ";
         cin>>ch;
          switch(ch)
           { case 'i': cout<<"n enter the value to insert";
                          cin>>x;
                          s.push(x);
                          break;
             case 'd': cout<<"n";
                          s.display();
                          break;
             case 'r': x=s.pop();
                          if(x==-99) cout<<"n stack is empty";
                          else cout<<" n the element removed is "<<x;
                          break;
            }
        }while(ch!='q');
  }

OUTPUT:

enter i to insert an element
enter d to view the contents
enter r to remove an element
enter q to quit
enter your choice : r

stack is empty
enter i to insert an element
enter d to view the contents
enter r to remove an element
enter q to quit
enter your choice : i

enter the value to insert : 1

enter i to insert an element
enter d to view the contents
enter r to remove an element
enter q to quit
enter your choice : i

enter the value to insert : 2

enter i to insert an element
enter d to view the contents
enter r to remove an element
enter q to quit
enter your choice : i

enter the value to insert : -1
enter i to insert an element
enter d to view the contents
enter r to remove an element
enter q to quit
enter your choice : d

-1 2 1
 enter the value to insert : 2

enter i to insert an element
enter d to view the contents
enter r to remove an element
enter q to quit
enter your choice : i

enter the value to insert : -1

enter i to insert an element
enter d to view the contents
enter r to remove an element
enter q to quit
enter your choice : d

-1 2 1
 enter i to insert an element
 enter d to view the contents
 enter r to remove an element
 enter q to quit
 enter your choice : r

the element removed is -1
enter i to insert an element
enter d to view the contents
enter r to remove an element
enter q to quit
enter your choice : r

the element removed is 2
enter i to insert an element
enter d to view the contents
enter r to remove an element
enter q to quit
enter your choice : r

the element removed is 1
enter i to insert an element
enter d to view the contents
enter r to remove an element
enter q to quit
enter your choice : r

stack is empty
/*THE FOLLOWING PROGRAM CONVERTS ANY infix EXPRESSION TO POSTFIX
postfix.c */

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

int precedence(char op)
{
  switch(op)
  {
    case '-':return 1;
    case '+':return 1;
    case '*':return 2;
    case '/':return 2;
    case '$':return 3;
    case '(':return 0;
  }
return -1;
}

 void push(char a[],int *top,char ch)
 { a[*top]=ch;
   (*top)++;
 }

 char pop(char a[],int *top)
  { *top=*top-1;
    return a[*top];
  }

 void main()
 {
   int top=0,i=0,j=0,k,l,length=0;
   char stack[50],infix[50],postfix[50];
   char ch;
   clrscr();
   cout<<"Enter the infix expression : n";
   scanf("%s",infix);
   length=strlen(infix);
     for(i=0;i<length;i++)
      {
        if(infix[i]=='(')
        push(stack,&top,infix[i]);

        if(isalpha(infix[i]))
             { postfix[j]=infix[i];
               j++;
             }
if(infix[i]==')')
          { while(stack[top-1]!='(')
               { postfix[j]=pop(stack,&top);
                    j++;
               }
         pop(stack,&top);
      }
    if(infix[i]=='+'||infix[i]=='-'||infix[i]=='/'||infix[i]=='*'||infix[i]=='$')
        { if(precedence(stack[top-1])<precedence(infix[i]))
           push(stack,&top,infix[i]);

         else
           { while(precedence(stack[top-1])>=precedence(infix[i]))
                { postfix[j]=pop(stack,&top);
                   j++;
                 }
           push(stack,&top,infix[i]);
           }
         }
}
   while(top!=0)
      { postfix[j]=pop(stack,&top);
        j++;
      }
   cout<<"nThe postfix expression is :n";
  for(k=0;k<j;k++)
   cout<<postfix[k];
getch();
}


OUTPUT:



Enter the infix expression :
(((X*Y-A*B)-D/F)+A)*B

The postfix expression is :
XY*AB*-DF/-A+B*
do you want to continue (y/n) : y
Enter the infix expression :
A*B-P/(Q*(A-C+P*D)/B)

The postfix expression is :
AB*PQAC-PD*+*B//-
do you want to continue (y/n) : n
/* INSERTION AND DELETION IN A QUEUE ARRAY IMPLEMENTATION */
/* queue_a.c */

# include<stdio.h>
# include<string.h>
# include<ctype.h>
# include<stdlib.h>
# define size 5

int ch;
int q[size];
int rear = 0;
int front = 0;

void Insert_queue();
void Delete_queue();
void Display_queue();

/* Function to create queue */
void Insert_queue()
{      printf("n Input the element :");
       scanf("%d", &ch);
       if(rear < size)
          { rear ++;
               q[rear] = ch ;
               if(front == 0)
                       front = 1;
          }
       else printf("n Queue is overflow");
}

/* Function to perform delete operation */
void Delete_queue()
{      if (front == 0)
          {    printf("n Underflow");
               return ;
          }
       else
       {       ch = q[front];
               printf("n Element deleted : %d", ch);
       }
       if(front == rear)
       {       front = 0;
               rear = 0;
       }
       else front = front + 1;
}
/* Output function */
void Display_queue() //char q[])
{      int i;
       if (front == 0)
               return;
       for(i = front ; i <= rear; i++)
               printf(" %d ", q[i]);
}

/* Function main */
void main()
{
       int k = 0;
       char choice;
       clrscr();
       do
       {       printf("nInsert->i Delete->d Quit->q:");
               printf("nInput the choice : ");
               do
               { choice = getchar();
                  choice = tolower(choice);
               }while(strchr("idq",choice)==NULL);
        switch(choice)
               {
               case 'i' : Insert_queue();
                           printf("n Queue after inserting ");
                           Display_queue();
                           break;

               case 'd' : Delete_queue();
                          printf("n Queue after deleting:n");
                          Display_queue();
                          break;
               case 'q': k = 1;
               }
        } while(!k);
}
/* THIS PROGRAM DEMONSTRATES OPERATIONS ON A CIRCULAR QUEUE
USING OOPS-CLASS QUEUE –cqueue.cpp*/

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
 class queue
  { int q[30]; //queue declaration
    int f,r ;
    int n;
    public:
     queue() //constructor
     { f=r=0; //indicates queue is empty
          n=5;
     }
     void insert(int);
     int delet();
     void display();
   };

 void queue::insert(int x)
  { if((r+1)%n==f)
          cout<<"n the queue is full";
    else
         { r=(r+1)%n;
           q[r]=x;
         }
  }
 int queue::delet()
  { int v=-99;
     if(f!=r)
          { f=(f+1)%n;
            v=q[f];
          }
     return v;
   }
  void queue::display()
   { int i;
         if(f!=r)
           { i=f;
             while(i!=r)
             { i=(i+1)%n;
               cout<<q[i]<<" ";
             }
           }
         else cout<<"n queue is empty";
    }
  void main()
   { queue s;
     char ch;int x;
     clrscr();
         do
{ cout<<"n enter i to insert an elemnt";
         cout<<"n enter d to view the contents";
         cout<<"n enter r to remove an elemnt";
         cout<<"n enter q to quit";
         cout<<"n enter your choice : ";
         cin>>ch;
          switch(ch)
           { case 'i': cout<<"n enter the value to insert";
                          cin>>x;
                          s.insert(x);
                          break;
             case 'd': cout<<"n";
                          s.display();
                          break;
             case 'r': x=s.delet();
                          if(x==-99) cout<<"n queue is empty";
                          else cout<<" n the element removed is "<<x;
                          break;
            }
        }while(ch!='q');
  }

OUTPUT :

assume size of queue is 3 integers
Insert->i Delete->d Quit->q:
Input the choice : i

Input the element :1

Queue after inserting 1
Insert->i Delete->d Quit->q:
Input the choice : i

Input the element :2

Queue after inserting 1 2
Insert->i Delete->d Quit->q:
Input the choice : d

Element deleted : 1
Queue after deleting: 2
Insert->i Delete->d Quit->q:
Input the choice : d

Element deleted : 2
 Queue after deleting:
queue is empty

Insert->i Delete->d Quit->q:
Input the choice : q
/* THIS PROGRAM DEMONSTRATES THE CREATION OF A SIMPLE LINKED LIST
INCORPORATING ALL FUNCTIONS OF INSERTION AND DELETION AT THE
BEGINNING , END AND AFTER ANY PARTICULAR NODE WITH DISPLAY FEATURES */
/* LINKLIST.C */

# include <stdio.h>
# include <malloc.h>

struct link
 { int info;
   struct link *next;
 };

int i,number;
struct link start, *previous, *new1;

void insertiona(struct link *);
void insertionb(struct link *);
void create_list(struct link *);
void insertione(struct link *);
void display(struct link *);
void delete_nodef(struct link *);
void delete_node(struct link *);
void delete_nodel(struct link *);

/* Function to create a linked list */
void create_list(struct link *node)
{      char ch;
       start.next = NULL; /* Empty list */

        node = &start; /* Point to the start of the list */
        i = 0;
        printf("n Input choice n for break: ");
        ch = getchar();
        while(ch != 'n')
        {
                node->next = (struct link* ) malloc(sizeof(struct link));
                node = node->next;
                printf("n Input the node: %d: ", i+1);
                scanf("%d", &node->info);
                node->next = NULL;
                fflush(stdin);
                printf("n Input choice n for break: ");
                ch = getchar();
                i++;
        }
        printf("n Total nodes = %d", i);
}

/* Inserting a node after a particular node*/
void insertiona(struct link *node)
{     int ins_node,x;
node = start.next;
       previous = &start;
       printf("n Input value of the nodeafter which you want to insert:");
       scanf("%d", &ins_node);

       while((node->info!=ins_node)&&(node!=NULL))
       node=node->next;
        if(node!=NULL)
              {        new1 = (struct link* ) malloc(sizeof(struct link));
                       printf("n enter the new value to be inserted : ");
                       scanf(" %d",&new1->info);
                        new1->next = node->next;
                       node->next = new1;
              }
        else
                printf("n insertion is not possible");


}

/* Display the list */
void display(struct link *node)
{     node = start.next;
        while (node)
         { printf(" %d ", node->info);
           node = node->next;
           printf(" ");
         }
}

void main()
{    int c;
       struct link *node = (struct link *) malloc(sizeof(struct link));
       clrscr();
       do
        { printf("n enter 1 to create the list");
         printf("n enter 2 to insert a node at the beginning");
         printf("n enter 3 to insert a node anywhere");
         printf("n enter 4 to append a node ");
         printf("n enter 5 to display the list");
         printf("n enter 6 to remove a node from beginning");
         printf("n enter 7 to remove a node anywhere");
         printf("n enter 8 to remove a node from the end");
         printf("n enter 9 to quit");
         printf("n enter your choice : ");
         scanf("%d",&c);
           switch(c)
            { case 1 : create_list(node);
                         printf("n Created list is as follows:n");
                         display(node);
                         break;
                case 2 : insertionb(node);
break;
                case 3 : insertiona(node);
                         break;
                case 4 : display(node);
                         insertione(node);
                         break;
                case 5 : display(node);
                         break;
                case 6 : delete_nodef(node);
                         break;
                case 7 : delete_node(node);
                         break;
                case 8 : delete_nodel(node);
                         break;
                case 9 : exit(0);
             }
         }while((c>=1)&&(c<=9));
    getch();
}

/* Inserting a node at the beginning*/

void insertionb(struct link *node)
{     node = start.next;
        previous = &start;
        new1 = (struct link* ) malloc(sizeof(struct link));
        new1->next = node ;
        previous->next = new1;
        printf("n Input the fisrt node value: ");
        scanf("%d", &new1->info);
}

/* Inserting a node at the end */
void insertione(struct link *node)
{       node = start.next;
        previous = &start;
        if(node==NULL)
           { printf("n the list is empty to create return ");
             return;
           }
        else
         { while(node->next!=NULL)
                 node = node->next;
          new1 = (struct link* ) malloc(sizeof(struct link));
          printf("n Input the value to be inserted at the end : ");
          scanf(" %d",&new1->info);
          node->next=new1;
          new1->next=NULL;
        }
}

/* Removing the first node */
void delete_nodef(struct link*node)
{    node = start.next;
       previous = &start;
         if (node == NULL)
                printf("n Under flow");
         else
           {    previous->next = node->next;
                free(node);
           }
}

/* Removing a node when information is known*/
void delete_node(struct link *node)
{      int node_number = 1;
       int del_node;

       node = start.next;
       previous = &start;
       printf("n Input information of a node you want to delete: ");
       scanf("%d", &del_node);
       while(node)
       { if(node->info == del_node)
               {     printf("n Position of the information in the list is : %d", node_number);
                       previous->next = node->next;
                       free(node);
                       break ;
               }
               else
               {     node = node->next;
                       previous = previous->next;
               }
               node_number++;
       }
}

/* Removing the last node */
void delete_nodel(struct link *node)
{      int node_number = 0;
       node = start.next;
       previous = &start;

       if (node == NULL)
          printf("n Underflow");
       else
         while(node)
                { node = node->next;
                  previous = previous->next;
                  node_number ++;
                }
       node = start.next;
       previous = &start;
while(node_number != 1)
    {      node = node->next;
           previous = previous->next;
           node_number --;
    }
    if(node_number == 1)
    {      previous->next = node->next;
           free(node);
    }
}

More Related Content

What's hot

Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
vrgokila
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
Bilal Mirza
 
C programms
C programmsC programms
C programms
Mukund Gandrakota
 
Double linked list
Double linked listDouble linked list
Double linked list
Sayantan Sur
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
Nitesh Dubey
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
argusacademy
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
Rahul Chugh
 
Double linked list
Double linked listDouble linked list
Double linked list
raviahuja11
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
Harjinder Singh
 
Blocks+gcd入門
Blocks+gcd入門Blocks+gcd入門
Blocks+gcd入門
領一 和泉田
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
Arkadeep Dey
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
Sayantan Sur
 
Single linked list
Single linked listSingle linked list
Single linked list
Sayantan Sur
 
Graphics point clipping c program
Graphics point clipping c programGraphics point clipping c program
Graphics point clipping c program
Dr.M.Karthika parthasarathy
 
Sorting programs
Sorting programsSorting programs
Sorting programsVarun Garg
 

What's hot (20)

Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
Struct examples
Struct examplesStruct examples
Struct examples
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
C programms
C programmsC programms
C programms
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Double linked list
Double linked listDouble linked list
Double linked list
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Double linked list
Double linked listDouble linked list
Double linked list
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
Blocks+gcd入門
Blocks+gcd入門Blocks+gcd入門
Blocks+gcd入門
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Single linked list
Single linked listSingle linked list
Single linked list
 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
Graphics point clipping c program
Graphics point clipping c programGraphics point clipping c program
Graphics point clipping c program
 
Binary tree
Binary treeBinary tree
Binary tree
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 

Viewers also liked

Photography Portfolio
Photography PortfolioPhotography Portfolio
Photography Portfolio
jillianlovesfilm
 
Going mobile - A Technical Job Prep for Vassar Students
Going mobile - A Technical Job Prep for Vassar StudentsGoing mobile - A Technical Job Prep for Vassar Students
Going mobile - A Technical Job Prep for Vassar Studentsbryan costanich
 
Where we live in atlanta georgia usa
Where we live in atlanta georgia usaWhere we live in atlanta georgia usa
Where we live in atlanta georgia usarochesterdavisacademy
 
Running SagePFW in a Private Cloud
Running SagePFW in a Private CloudRunning SagePFW in a Private Cloud
Running SagePFW in a Private CloudVertical Solutions
 
FRx is Dead: Using SagePFW Intelligence
FRx is Dead: Using SagePFW IntelligenceFRx is Dead: Using SagePFW Intelligence
FRx is Dead: Using SagePFW IntelligenceVertical Solutions
 
Cross Platform Mobile Development with Xamarin
Cross Platform Mobile Development with XamarinCross Platform Mobile Development with Xamarin
Cross Platform Mobile Development with Xamarinbryan costanich
 
Cross Platform Development with Xamarin
Cross Platform Development with XamarinCross Platform Development with Xamarin
Cross Platform Development with Xamarin
bryan costanich
 
Advanced android app lifecycle + Patterns
Advanced android app lifecycle + PatternsAdvanced android app lifecycle + Patterns
Advanced android app lifecycle + Patternsbryan costanich
 
Poetry Dedication Project
Poetry Dedication ProjectPoetry Dedication Project
Poetry Dedication Project
Jon_C
 
Cross-Platform Mobile Development in Visual Studio
Cross-Platform Mobile Development in Visual StudioCross-Platform Mobile Development in Visual Studio
Cross-Platform Mobile Development in Visual Studiobryan costanich
 
Vlnová analýza časových řad zesílení a expanze plantážního zemědělství v braz...
Vlnová analýza časových řad zesílení a expanze plantážního zemědělství v braz...Vlnová analýza časových řad zesílení a expanze plantážního zemědělství v braz...
Vlnová analýza časových řad zesílení a expanze plantážního zemědělství v braz...Miroslav Vereš
 
Lifestyle workbook
Lifestyle workbookLifestyle workbook
Lifestyle workbook
KjnO8484
 
Miroslav veres -_monitorovanie_poziarov_a_dpz_-_prezentacia
Miroslav veres -_monitorovanie_poziarov_a_dpz_-_prezentaciaMiroslav veres -_monitorovanie_poziarov_a_dpz_-_prezentacia
Miroslav veres -_monitorovanie_poziarov_a_dpz_-_prezentaciaMiroslav Vereš
 

Viewers also liked (16)

C# rocks
C# rocksC# rocks
C# rocks
 
Photography Portfolio
Photography PortfolioPhotography Portfolio
Photography Portfolio
 
Going mobile - A Technical Job Prep for Vassar Students
Going mobile - A Technical Job Prep for Vassar StudentsGoing mobile - A Technical Job Prep for Vassar Students
Going mobile - A Technical Job Prep for Vassar Students
 
Where we live in atlanta georgia usa
Where we live in atlanta georgia usaWhere we live in atlanta georgia usa
Where we live in atlanta georgia usa
 
All about me
All about meAll about me
All about me
 
Running SagePFW in a Private Cloud
Running SagePFW in a Private CloudRunning SagePFW in a Private Cloud
Running SagePFW in a Private Cloud
 
Metallic bonding
Metallic bondingMetallic bonding
Metallic bonding
 
FRx is Dead: Using SagePFW Intelligence
FRx is Dead: Using SagePFW IntelligenceFRx is Dead: Using SagePFW Intelligence
FRx is Dead: Using SagePFW Intelligence
 
Cross Platform Mobile Development with Xamarin
Cross Platform Mobile Development with XamarinCross Platform Mobile Development with Xamarin
Cross Platform Mobile Development with Xamarin
 
Cross Platform Development with Xamarin
Cross Platform Development with XamarinCross Platform Development with Xamarin
Cross Platform Development with Xamarin
 
Advanced android app lifecycle + Patterns
Advanced android app lifecycle + PatternsAdvanced android app lifecycle + Patterns
Advanced android app lifecycle + Patterns
 
Poetry Dedication Project
Poetry Dedication ProjectPoetry Dedication Project
Poetry Dedication Project
 
Cross-Platform Mobile Development in Visual Studio
Cross-Platform Mobile Development in Visual StudioCross-Platform Mobile Development in Visual Studio
Cross-Platform Mobile Development in Visual Studio
 
Vlnová analýza časových řad zesílení a expanze plantážního zemědělství v braz...
Vlnová analýza časových řad zesílení a expanze plantážního zemědělství v braz...Vlnová analýza časových řad zesílení a expanze plantážního zemědělství v braz...
Vlnová analýza časových řad zesílení a expanze plantážního zemědělství v braz...
 
Lifestyle workbook
Lifestyle workbookLifestyle workbook
Lifestyle workbook
 
Miroslav veres -_monitorovanie_poziarov_a_dpz_-_prezentacia
Miroslav veres -_monitorovanie_poziarov_a_dpz_-_prezentaciaMiroslav veres -_monitorovanie_poziarov_a_dpz_-_prezentacia
Miroslav veres -_monitorovanie_poziarov_a_dpz_-_prezentacia
 

Similar to Datastructures asignment

VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
Nithin Kumar,VVCE, Mysuru
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
Lakshmi Sarvani Videla
 
stack.pptx
stack.pptxstack.pptx
stack.pptx
MeghaKulkarni27
 
Artificial intelligence
Artificial intelligenceArtificial intelligence
Artificial intelligenceAditya Sharma
 
Qprgs
QprgsQprgs
Stack using Array
Stack using ArrayStack using Array
Stack using Array
Sayantan Sur
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Er Ritu Aggarwal
 
#includeiostream#includecctypeusing namespace std;cl.docx
#includeiostream#includecctypeusing namespace std;cl.docx#includeiostream#includecctypeusing namespace std;cl.docx
#includeiostream#includecctypeusing namespace std;cl.docx
katherncarlyle
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
mustkeem khan
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
mustkeem khan
 
data structure and algorithm.pdf
data structure and algorithm.pdfdata structure and algorithm.pdf
data structure and algorithm.pdf
Asrinath1
 
C basics
C basicsC basics
C basicsMSc CST
 
Stack array
Stack arrayStack array
Stack array
rimshailyas1
 
Programs
ProgramsPrograms
CS8391-Data Structures Unit 2
CS8391-Data Structures Unit 2CS8391-Data Structures Unit 2
CS8391-Data Structures Unit 2
SIMONTHOMAS S
 
DS- Stack ADT
DS- Stack ADTDS- Stack ADT
DS- Stack ADT
MythiliMurugan3
 

Similar to Datastructures asignment (20)

week-18x
week-18xweek-18x
week-18x
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
week-16x
week-16xweek-16x
week-16x
 
stack.pptx
stack.pptxstack.pptx
stack.pptx
 
week-15x
week-15xweek-15x
week-15x
 
Artificial intelligence
Artificial intelligenceArtificial intelligence
Artificial intelligence
 
Qprgs
QprgsQprgs
Qprgs
 
Stack using Array
Stack using ArrayStack using Array
Stack using Array
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
#includeiostream#includecctypeusing namespace std;cl.docx
#includeiostream#includecctypeusing namespace std;cl.docx#includeiostream#includecctypeusing namespace std;cl.docx
#includeiostream#includecctypeusing namespace std;cl.docx
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
 
data structure and algorithm.pdf
data structure and algorithm.pdfdata structure and algorithm.pdf
data structure and algorithm.pdf
 
C basics
C basicsC basics
C basics
 
Stack array
Stack arrayStack array
Stack array
 
Programs
ProgramsPrograms
Programs
 
CS8391-Data Structures Unit 2
CS8391-Data Structures Unit 2CS8391-Data Structures Unit 2
CS8391-Data Structures Unit 2
 
DS- Stack ADT
DS- Stack ADTDS- Stack ADT
DS- Stack ADT
 
week-14x
week-14xweek-14x
week-14x
 

Recently uploaded

Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
ShivajiThube2
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 

Recently uploaded (20)

Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 

Datastructures asignment

  • 1. /* IMPLEMENTATION OF THE STACK USING ARRAYS */ /* STACK_A.C */ # include<stdio.h> # include<string.h> # include<ctype.h> #include<conio.h> # define size 3 int top = -1; int flag = 0; int stack[size]; void push(int *, int); int pop(int *); void display(int *); /* Definition of the push function */ void push(int s[], int d) { if(top ==(size-1)) flag = 0; else { flag = 1; ++top; s[top] = d; } } /* Definition of the pop function */ int pop(int s[]) { int popped_element; if(top == -1) { popped_element = 0; flag = 0; } else { flag = 1; popped_element = s[top]; --top; } return (popped_element); } /* Definition of the display function */ void display(int s[]) { int i; if(top == -1) printf("n Stack is empty"); else { for(i = top; i >= 0; --i) printf("n %d", s[i] ); } }
  • 2. /* Function main */ void main() { int data; char choice; int q = 0; int top = -1; clrscr(); do { printf(" n enter i to insert"); printf(" n enter p to delete"); printf(" n enter q to quit"); printf("n enter your choice : "); do { choice = getchar(); choice =tolower(choice); }while(strchr("ipq",choice)==NULL); switch(choice) { case 'i' : printf("n Input the element to push:"); scanf("%d", &data); push(stack, data); if(flag) { printf("n After inserting "); display(stack); if(top == (size-1)) printf("n Stack is full"); } else printf("n Stack overflow after pushing"); break; case 'p' : data = pop(stack); if(flag) { printf("n Data is popped: %d", data); printf("n Rest data in stack is as follows:n"); display(stack); } else printf("n Stack underflow" ); break; case 'q': q = 1; } } while(!q); }
  • 3. /* THIS PROGRAM DEMONSTRATES THE STACK OPERATIONS USING OOPS- CLASS STACK*/ /* cstack.cpp */ #include<iostream.h> #include<conio.h> #include<stdio.h> class stack { int stack[30]; int top,n; public: stack() { top=-1; //indicates stack is empty n=5; } void push(int); int pop(); void display(); }; void stack::push(int x) { if(top<n) { top++; stack[top]=x; } else cout<<"n the stack is full"; } int stack::pop() { int v=-99; if(top>=0) { v=stack[top]; top--; } return v; } void stack::display() { int i; if(top>=0) { for(i=top;i>=0;i--) cout<<stack[i]<<" "; } else cout<<"n stack is empty"; } void main() { stack s; char ch; int x; clrscr(); do { cout<<"n enter i to insert an element"; cout<<"n enter d to view the contents"; cout<<"n enter r to remove an element";
  • 4. cout<<"n enter q to quit"; cout<<"n enter your choice : "; cin>>ch; switch(ch) { case 'i': cout<<"n enter the value to insert"; cin>>x; s.push(x); break; case 'd': cout<<"n"; s.display(); break; case 'r': x=s.pop(); if(x==-99) cout<<"n stack is empty"; else cout<<" n the element removed is "<<x; break; } }while(ch!='q'); } OUTPUT: enter i to insert an element enter d to view the contents enter r to remove an element enter q to quit enter your choice : r stack is empty enter i to insert an element enter d to view the contents enter r to remove an element enter q to quit enter your choice : i enter the value to insert : 1 enter i to insert an element enter d to view the contents enter r to remove an element enter q to quit enter your choice : i enter the value to insert : 2 enter i to insert an element enter d to view the contents enter r to remove an element enter q to quit enter your choice : i enter the value to insert : -1
  • 5. enter i to insert an element enter d to view the contents enter r to remove an element enter q to quit enter your choice : d -1 2 1 enter the value to insert : 2 enter i to insert an element enter d to view the contents enter r to remove an element enter q to quit enter your choice : i enter the value to insert : -1 enter i to insert an element enter d to view the contents enter r to remove an element enter q to quit enter your choice : d -1 2 1 enter i to insert an element enter d to view the contents enter r to remove an element enter q to quit enter your choice : r the element removed is -1 enter i to insert an element enter d to view the contents enter r to remove an element enter q to quit enter your choice : r the element removed is 2 enter i to insert an element enter d to view the contents enter r to remove an element enter q to quit enter your choice : r the element removed is 1 enter i to insert an element enter d to view the contents enter r to remove an element enter q to quit enter your choice : r stack is empty
  • 6. /*THE FOLLOWING PROGRAM CONVERTS ANY infix EXPRESSION TO POSTFIX postfix.c */ #include<iostream.h> #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<ctype.h> #include<string.h> int precedence(char op) { switch(op) { case '-':return 1; case '+':return 1; case '*':return 2; case '/':return 2; case '$':return 3; case '(':return 0; } return -1; } void push(char a[],int *top,char ch) { a[*top]=ch; (*top)++; } char pop(char a[],int *top) { *top=*top-1; return a[*top]; } void main() { int top=0,i=0,j=0,k,l,length=0; char stack[50],infix[50],postfix[50]; char ch; clrscr(); cout<<"Enter the infix expression : n"; scanf("%s",infix); length=strlen(infix); for(i=0;i<length;i++) { if(infix[i]=='(') push(stack,&top,infix[i]); if(isalpha(infix[i])) { postfix[j]=infix[i]; j++; }
  • 7. if(infix[i]==')') { while(stack[top-1]!='(') { postfix[j]=pop(stack,&top); j++; } pop(stack,&top); } if(infix[i]=='+'||infix[i]=='-'||infix[i]=='/'||infix[i]=='*'||infix[i]=='$') { if(precedence(stack[top-1])<precedence(infix[i])) push(stack,&top,infix[i]); else { while(precedence(stack[top-1])>=precedence(infix[i])) { postfix[j]=pop(stack,&top); j++; } push(stack,&top,infix[i]); } } } while(top!=0) { postfix[j]=pop(stack,&top); j++; } cout<<"nThe postfix expression is :n"; for(k=0;k<j;k++) cout<<postfix[k]; getch(); } OUTPUT: Enter the infix expression : (((X*Y-A*B)-D/F)+A)*B The postfix expression is : XY*AB*-DF/-A+B* do you want to continue (y/n) : y Enter the infix expression : A*B-P/(Q*(A-C+P*D)/B) The postfix expression is : AB*PQAC-PD*+*B//- do you want to continue (y/n) : n
  • 8. /* INSERTION AND DELETION IN A QUEUE ARRAY IMPLEMENTATION */ /* queue_a.c */ # include<stdio.h> # include<string.h> # include<ctype.h> # include<stdlib.h> # define size 5 int ch; int q[size]; int rear = 0; int front = 0; void Insert_queue(); void Delete_queue(); void Display_queue(); /* Function to create queue */ void Insert_queue() { printf("n Input the element :"); scanf("%d", &ch); if(rear < size) { rear ++; q[rear] = ch ; if(front == 0) front = 1; } else printf("n Queue is overflow"); } /* Function to perform delete operation */ void Delete_queue() { if (front == 0) { printf("n Underflow"); return ; } else { ch = q[front]; printf("n Element deleted : %d", ch); } if(front == rear) { front = 0; rear = 0; } else front = front + 1; }
  • 9. /* Output function */ void Display_queue() //char q[]) { int i; if (front == 0) return; for(i = front ; i <= rear; i++) printf(" %d ", q[i]); } /* Function main */ void main() { int k = 0; char choice; clrscr(); do { printf("nInsert->i Delete->d Quit->q:"); printf("nInput the choice : "); do { choice = getchar(); choice = tolower(choice); }while(strchr("idq",choice)==NULL); switch(choice) { case 'i' : Insert_queue(); printf("n Queue after inserting "); Display_queue(); break; case 'd' : Delete_queue(); printf("n Queue after deleting:n"); Display_queue(); break; case 'q': k = 1; } } while(!k); }
  • 10. /* THIS PROGRAM DEMONSTRATES OPERATIONS ON A CIRCULAR QUEUE USING OOPS-CLASS QUEUE –cqueue.cpp*/ #include<iostream.h> #include<conio.h> #include<stdio.h> class queue { int q[30]; //queue declaration int f,r ; int n; public: queue() //constructor { f=r=0; //indicates queue is empty n=5; } void insert(int); int delet(); void display(); }; void queue::insert(int x) { if((r+1)%n==f) cout<<"n the queue is full"; else { r=(r+1)%n; q[r]=x; } } int queue::delet() { int v=-99; if(f!=r) { f=(f+1)%n; v=q[f]; } return v; } void queue::display() { int i; if(f!=r) { i=f; while(i!=r) { i=(i+1)%n; cout<<q[i]<<" "; } } else cout<<"n queue is empty"; } void main() { queue s; char ch;int x; clrscr(); do
  • 11. { cout<<"n enter i to insert an elemnt"; cout<<"n enter d to view the contents"; cout<<"n enter r to remove an elemnt"; cout<<"n enter q to quit"; cout<<"n enter your choice : "; cin>>ch; switch(ch) { case 'i': cout<<"n enter the value to insert"; cin>>x; s.insert(x); break; case 'd': cout<<"n"; s.display(); break; case 'r': x=s.delet(); if(x==-99) cout<<"n queue is empty"; else cout<<" n the element removed is "<<x; break; } }while(ch!='q'); } OUTPUT : assume size of queue is 3 integers Insert->i Delete->d Quit->q: Input the choice : i Input the element :1 Queue after inserting 1 Insert->i Delete->d Quit->q: Input the choice : i Input the element :2 Queue after inserting 1 2 Insert->i Delete->d Quit->q: Input the choice : d Element deleted : 1 Queue after deleting: 2 Insert->i Delete->d Quit->q: Input the choice : d Element deleted : 2 Queue after deleting: queue is empty Insert->i Delete->d Quit->q: Input the choice : q
  • 12. /* THIS PROGRAM DEMONSTRATES THE CREATION OF A SIMPLE LINKED LIST INCORPORATING ALL FUNCTIONS OF INSERTION AND DELETION AT THE BEGINNING , END AND AFTER ANY PARTICULAR NODE WITH DISPLAY FEATURES */ /* LINKLIST.C */ # include <stdio.h> # include <malloc.h> struct link { int info; struct link *next; }; int i,number; struct link start, *previous, *new1; void insertiona(struct link *); void insertionb(struct link *); void create_list(struct link *); void insertione(struct link *); void display(struct link *); void delete_nodef(struct link *); void delete_node(struct link *); void delete_nodel(struct link *); /* Function to create a linked list */ void create_list(struct link *node) { char ch; start.next = NULL; /* Empty list */ node = &start; /* Point to the start of the list */ i = 0; printf("n Input choice n for break: "); ch = getchar(); while(ch != 'n') { node->next = (struct link* ) malloc(sizeof(struct link)); node = node->next; printf("n Input the node: %d: ", i+1); scanf("%d", &node->info); node->next = NULL; fflush(stdin); printf("n Input choice n for break: "); ch = getchar(); i++; } printf("n Total nodes = %d", i); } /* Inserting a node after a particular node*/ void insertiona(struct link *node) { int ins_node,x;
  • 13. node = start.next; previous = &start; printf("n Input value of the nodeafter which you want to insert:"); scanf("%d", &ins_node); while((node->info!=ins_node)&&(node!=NULL)) node=node->next; if(node!=NULL) { new1 = (struct link* ) malloc(sizeof(struct link)); printf("n enter the new value to be inserted : "); scanf(" %d",&new1->info); new1->next = node->next; node->next = new1; } else printf("n insertion is not possible"); } /* Display the list */ void display(struct link *node) { node = start.next; while (node) { printf(" %d ", node->info); node = node->next; printf(" "); } } void main() { int c; struct link *node = (struct link *) malloc(sizeof(struct link)); clrscr(); do { printf("n enter 1 to create the list"); printf("n enter 2 to insert a node at the beginning"); printf("n enter 3 to insert a node anywhere"); printf("n enter 4 to append a node "); printf("n enter 5 to display the list"); printf("n enter 6 to remove a node from beginning"); printf("n enter 7 to remove a node anywhere"); printf("n enter 8 to remove a node from the end"); printf("n enter 9 to quit"); printf("n enter your choice : "); scanf("%d",&c); switch(c) { case 1 : create_list(node); printf("n Created list is as follows:n"); display(node); break; case 2 : insertionb(node);
  • 14. break; case 3 : insertiona(node); break; case 4 : display(node); insertione(node); break; case 5 : display(node); break; case 6 : delete_nodef(node); break; case 7 : delete_node(node); break; case 8 : delete_nodel(node); break; case 9 : exit(0); } }while((c>=1)&&(c<=9)); getch(); } /* Inserting a node at the beginning*/ void insertionb(struct link *node) { node = start.next; previous = &start; new1 = (struct link* ) malloc(sizeof(struct link)); new1->next = node ; previous->next = new1; printf("n Input the fisrt node value: "); scanf("%d", &new1->info); } /* Inserting a node at the end */ void insertione(struct link *node) { node = start.next; previous = &start; if(node==NULL) { printf("n the list is empty to create return "); return; } else { while(node->next!=NULL) node = node->next; new1 = (struct link* ) malloc(sizeof(struct link)); printf("n Input the value to be inserted at the end : "); scanf(" %d",&new1->info); node->next=new1; new1->next=NULL; } } /* Removing the first node */
  • 15. void delete_nodef(struct link*node) { node = start.next; previous = &start; if (node == NULL) printf("n Under flow"); else { previous->next = node->next; free(node); } } /* Removing a node when information is known*/ void delete_node(struct link *node) { int node_number = 1; int del_node; node = start.next; previous = &start; printf("n Input information of a node you want to delete: "); scanf("%d", &del_node); while(node) { if(node->info == del_node) { printf("n Position of the information in the list is : %d", node_number); previous->next = node->next; free(node); break ; } else { node = node->next; previous = previous->next; } node_number++; } } /* Removing the last node */ void delete_nodel(struct link *node) { int node_number = 0; node = start.next; previous = &start; if (node == NULL) printf("n Underflow"); else while(node) { node = node->next; previous = previous->next; node_number ++; } node = start.next; previous = &start;
  • 16. while(node_number != 1) { node = node->next; previous = previous->next; node_number --; } if(node_number == 1) { previous->next = node->next; free(node); } }