0% found this document useful (0 votes)
86 views33 pages

Queue Insertion and Deletion: "Stdio.h" "Conio.h" "Stdlib.h"

The document contains code snippets for various data structures and algorithms: 1. Queue insertion and deletion functions are defined to add and remove elements from a circular queue. 2. An infix to postfix conversion function uses a stack to convert infix expressions to postfix. 3. An array insertion function demonstrates how to insert a new element into an array at a given position, shifting the other elements. The document provides code examples for common data structures and algorithms like queues, stacks, arrays, and string functions.

Uploaded by

Kush Varma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views33 pages

Queue Insertion and Deletion: "Stdio.h" "Conio.h" "Stdlib.h"

The document contains code snippets for various data structures and algorithms: 1. Queue insertion and deletion functions are defined to add and remove elements from a circular queue. 2. An infix to postfix conversion function uses a stack to convert infix expressions to postfix. 3. An array insertion function demonstrates how to insert a new element into an array at a given position, shifting the other elements. The document provides code examples for common data structures and algorithms like queues, stacks, arrays, and string functions.

Uploaded by

Kush Varma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

- 1.

Queue Insertion and Deletion

#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
int queue[5],front=-1,rear=-1;
void insert()
{
int n;
printf("enter number to be entered");
scanf("%d",&n);
if(rear==-1)
{
rear=front=0;
queue[rear]=n;
}
else
if(rear==4)
{
printf("queue is full");
}
else
queue[++rear]=n;
}
void del()
{
if(front==-1)
printf("queue is empty");
else
if(front==rear)
{
printf("last element entered is %d",queue[front]);
front=rear=-1;
}
else
printf("%d",queue[front++]);
}
void main()
{
int k=1,num;
while(k==1)
{
printf("\n1 insert\n");
printf("2 delete\n");
printf("3 exit");
scanf("%d",&num);
if(num==1)
{
insert();
}
if(num==2)
{
del();
}
if(num==3)
{
k=0;
exit(0);
}
}
}
- 2. Infix to postfix

#include "stdio.h"
#include "conio.h"
#include "ctype.h"
char stack[100];
int tos=-1;
void push(char n)
{
if(tos==99)
printf("stack is overflow");
else
stack[++tos]=n;
}
char pop()
{
char ch;
if(tos==-1)
printf("stack is empty");
else
{
ch=stack[tos];
tos--;
}
return ch;
}

int isp(char ch)


{
switch(ch)
{
case '(':
return 0;
case ')':
return 9;
case '{':
return 1;
case '}':
return 8;
case '[':
return 2;
case ']':
return 7;
case '^':
return 5;
case '*':
return 4;
case '/':
return 4;
case '%':
return 4;
case '+':
return 3;
case '-':
return 3;
case '\0':
return 0;
}
}
int icp(char ch)
{
switch(ch)
{
case '(':
return 20;
case ')':
return 9;
case '{':
return 18;
case '}':
return 8;
case '[':
return 17;
case ']':
return 7;
case '^':
return 5;
case '*':
return 4;
case '/':
return 4;
case '%':
return 4;
case '+':
return 3;
case '-':
return 3;
case '\0':
return 0;
}
}
void in_postfix(char infix[])
{
char ch,token,postfix[100];
int p=0,i=0;
push('\0');

token=infix[i];
while(token!='\0')
{
if(isalnum(token))
postfix[p++]=token;
else
if(token=='(')
push('(');
else

if(token==')')
{
while(stack[tos]!='(')
{
ch=pop();
postfix[p++]=ch;
}
ch=pop();
}

else
if(token=='{')
push('{');
else

if(token=='}')
{
while(stack[tos]!='{')
{
ch=pop();
postfix[p++]=ch;
}
ch=pop();
}

else

if(token=='[')
push('[');

else

if(token==']')
{
while(stack[tos]!='[')
{
ch=pop();
postfix[p++]=ch;
}
ch=pop();
}

else
{
if(isp(stack[tos])<icp(token))
push(token);
else
{
while(isp(stack[tos])>=icp(token))
{
ch=pop();
postfix[p++]=ch;
}
push(token);
}
}
i++;
token=infix[i];
}

while(tos!=-1)
{
ch=pop();
postfix[p++]=ch;
}
printf("%s",postfix);
}

void main()
{
char infix[100];
printf("\n enter infix string");
gets(infix);
in_postfix(infix);
}
- 3. Add in Array

#include "stdio.h"
#include "conio.h"
void main()
{
int i,arr[10],num,n;
printf("enter 9 numbers in the array of 10\n");
for(i=0;i<9;i++)
{
printf("\n%d -----",i+1);
scanf ("%d",&arr[i]);
}
printf("\nEnter position where you want to add a number");
scanf("%d",&num);

for(i=9;i>num-1;i--)
{
arr[i]=arr[i-1];
}
printf("enter a number to be enter at %d postion",num);
scanf("%d",&n);
arr[num-1]=n;

printf("\nnow the new array is\n");


for(i=0;i<10;i++)
{
printf("%d ",arr[i]);
}
}
4. Deletion from an Array
#include "stdio.h"
#include "conio.h"

void main()
{
int i,arr[10],num;
printf("enter 10 numbers in the array\n");
for(i=0;i<10;i++)
{
printf("\n%d -----",i+1);
scanf ("%d",&arr[i]);
}
printf("\nEnter position where you want to delete");
scanf ("%d",&num);

for(i=num-1;i<10;i++)
{
arr[i]=arr[i+1];
}

printf("\nnow the new array is\n");


for(i=0;i<9;i++)
{
printf("%d ",arr[i]);
}
}
5. Selection Sort
#include "stdio.h"
#include "conio.h"
void main()
{
int a[10],i,j,t;
printf("enter 10 numbers\n");
for(i=0;i<10;i++)
{
printf("%d element = ",i+1);
scanf ("%d",&a[i]);
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("The array after selection Sort\n");
for(i=0;i<10;i++)
{
printf("%d element is %d\n",i+1,a[i]);
}
}
6. Bubble Sorting

#include "stdio.h"
#include "conio.h"
void main()
{
int a[10],i,j,t;
printf("enter 10 numbers\n");
for(i=0;i<10;i++)
{
printf("%d element = ",i+1);
scanf ("%d",&a[i]);
}
for(j=0;j<9;j++)
{
for(i=0;i<9;i++)
{
if(a[i]>a[i+1])
{
t=a[i+1];
a[i+1]=a[i];
a[i]=t;
}
}
}
printf("The array after Bubble Sort\n");
for(i=0;i<10;i++)
{
printf("%d\n",a[i]);
}
}

7. String Length, Reverse, Concat


#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
# define MAX 50
void strln(char s[MAX])
{
int i;
for(i=0;s[i]!=0;i++);

printf("length of string is %d ",i);


}
void strrev(char s[MAX])
{
int i,j=0,k;
char t[20];
for(i=0;s[i]!=0;i++);
for(k=i-1;k>=0;k--)
{
t[j++]=s[k];
}

t[j++]=0;
printf("The reversed string is %s ",t);
}
void strcon(char s[], char s1[])
{
int k=0,i;
char s2[MAX];
for(i=0;s[i]!=0;i++)
{
s2[k++]=s[i];
}
s2[k++]=' ';
for(i=0;s1[i]!=0;i++)
{
s2[k++]=s1[i];
}
s2[k++]=0;
printf("concatted string is %s",s2);
}
void main()
{
int num,ch=1;
char s[MAX],s1[MAX];
while(ch==1)
{

printf("What you Want to do\n");


printf("enter 1 to find lenght of string\n");
printf("enter 2 to find reverse of string\n");
printf("enter 3 to Concat two String\n");
printf("enter 4 to exit");
scanf("%d",&num);

if(num==1)
{
printf("enter a string");
_flushall();
gets(s);

strln(s);
}
else if(num==2)
{
printf("enter a string");
_flushall();
gets(s);
strrev(s);
}
else if(num==3)
{
_flushall();
printf("\n\n\nenter String one");
gets(s);
printf("\n enter String two");
gets(s1);
strcon(s,s1);
}
else if(num==4)
{
exit(0);
}
printf("\n\n\ndo you want to continue\n");
printf("1 to continue 2 to exit");
scanf("%d",&ch);
}
}
- 8. Stack Pop and Push
#include"stdio.h"
#include"conio.h"
#include"stdlib.h"

int stack[10],tos=-1;
void push()
{
int num;
if(tos==9)
printf("stack is overflow");
else
printf("enter number to be entered : ");
scanf("%d",&num);
stack[++tos]=num;
}

void pop()
{
if(tos==-1)
printf(" stack is empty ");
else
printf(" num poped is %d\n ",stack[tos--]);
}

void traverse()
{
int i;
if(tos==-1)
printf(" stack is empty ");
else
for(i=0;i<=tos;i++)
printf(" %d\n",stack[i]);
}
void main()
{
int ch=1,num;
while(ch)
{
printf("press 1 to push element\n");
printf("press 2 to pop element\n");
printf("press 3 to traverse elements\n");
printf("press 4 to exit\n");
printf("enter your choice ");

scanf("%d",&num);

if(num==1)
push();
else if(num==2)
pop();
else if(num==3)
traverse();
else
exit(1);

printf(" enter 1 to continue and 0 to exit ");


scanf("%d",&ch);
}
}
- 9. Insertion Sort
#include "stdio.h"
#include "conio.h"
void main()
{
int a[10],i,j,num;
for(i=0;i<10;i++)
{
printf("enter %d number: ",i+1);
scanf("%d",&num);
for(j=i-1;j>=0 && a[j]>num;j--)
a[j+1]=a[j];
a[j+1]=num;
}

printf("After insertion sort output is\n");


for(i=0;i<10;i++)
printf(" %d\n",a[i]);
}
- 10. Linear Searching
#include "stdio.h"
#include "conio.h"
void main()
{
int a[10],i,num,flag,ch=1;
for(i=0;i<10;i++)
{
printf("enter number %d :: ",i+1);
scanf("%d",&a[i]);
}
while(ch==1)
{
printf("\n\nenter a number to find in array");
scanf("%d",&num);

flag=0;
for(i=0;i<10;i++)
{
if(a[i]==num)
{
flag=1;
break;
}
}
if(flag==1)
printf("\n%d found in array\n",num);
if(flag==0)
printf("\n%d does not exit\n",num);
printf("\n\ndo you want to continue searching\n");
printf("\n\nenter 1 to continue 0 to exit");
scanf("%d",&ch);
}
}
- 11. Link List
#include "stdio.h"
#include "conio.h"
#include "malloc.h"
#include "stdlib.h"
struct node
{
int data;
struct node *next;
};

typedef struct node node;

node *head=NULL;
node *createnode()
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->next=NULL;

printf("\n enter a number ");


scanf("%d",&temp->data);
return (temp);
}
void createlist()
{
int ch=1;
node *temp, *prev;
while(ch==1)
{
temp=createnode();
if(head==NULL)
head=temp;
else

prev->next=temp;
prev=temp;

printf(" Do you want to continue \n ");


printf(" enter 1 to continue 0 to end ");
scanf("%d",&ch);
}
}
void display()
{
node *temp;
for(temp=head;temp!=NULL;temp=temp->next)
{
printf("%d->",temp->data);
}
}
void del()
{
node *temp;
int n;
printf("\nenter data you want to delete");
scanf("%d",&n);
if(head->data==n)
head=head->next;
else
{
for(temp=head;temp->next!=NULL;temp=temp->next)
if(temp->next->data==n)
{
temp->next=temp->next->next;
return;
}
printf(" \nData not found ");
}
}
void insertaft()
{
node *prev,*temp;
int n;
printf(" \nenter data after which you want to insert a node " );
scanf("%d",&n);
temp=createnode();
for(prev=head;prev!=NULL;prev=prev->next)
if(prev->data==n)
{
temp->next=prev->next;
prev->next=temp;
return;
}
printf("\ndata not found ");
}

void insertbef()
{
node *temp,*prev;
int n;
printf("\n enter the data before which you want to insert a node ");
scanf("%d",&n);
if(head->data==n)
{
temp=createnode();
temp->next=head;
head=temp;
}
else
{
for(prev=head;prev->next=NULL;prev=prev->next)
if(prev->next->data==n)
{
temp=createnode();
temp->next=prev->next;
prev->next=temp;
return;
}
}
printf("\n data not found");
}

void main()
{
int ch;
while(1)
{
printf(" enter 1 to create list\n");
printf("enter 2 to display list\n");
printf("enter 3 to delete list node\n");
printf("enter 4 to insert before\n");
printf("enter 5 to insert after\n");
printf("enter 0 to exit ");

scanf("%d",&ch);
switch(ch)
{
case 1:
createlist();
break;
case 2:
display();
break;
case 3:
del();
break;
case 4:
insertbef();
break;
case 5:
insertaft();
break;
case 0:
exit(0);
break;
default:
printf(" \nInvalid choice ");
}
}
}
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
int stack[100],tos=-1;
void push(char i)
{
if(tos==99)
printf("Stack is full");
else
stack[++tos]=i;
}
int pop()
{
int ch;
if(tos==-1)
printf("stack is empty");
else
ch=stack[tos--];
return ch;
}
void eval(char post[])
{
int i,t1,t2,t;
for(i=0;post[i]!=0;i++)
{
if(isalnum(post[i]))
{
push(post[i]-48);
}
else
{
t1=pop();
t2=pop();
if(post[i]=='+')
{
t=t1+t2;
}
else if(post[i]=='-')
{
t=t2-t1;
}
else if(post[i]=='*')
{
t=t1*t2;
}
else if(post[i]=='/')
{
t=t2/t1;
}
else if(post[i]=='%')
{
t=t2%t1;
}
push(t);
}
}
printf("the result is %d ",pop());
}
void main()
{
char exp[100];
printf("enter an expression");
gets(exp);
eval(exp);
}
- Implimentation of Stack using Linklist
# include <stdio.h>
# include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *push(struct node *p, int value)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));

if(temp==NULL)
{
printf("No Memory available\n");
exit(0);
}
temp->data = value;
temp->next = p;
p = temp;
return(p);
}

struct node *pop(struct node *p, int *value)


{
struct node *temp;
if(p==NULL)
{
printf(" The stack is empty\n");
exit(0);
}
*value = p->data;
temp = p;
p = p->next;
free(temp);
return(p);
}

void main()
{
struct node *top = NULL;
int n,value;
do
{
do
{
printf("Enter the element to be pushed\n");
scanf("%d",&value);
top = push(top,value);
printf("Enter 1 to continue 0 to break\n");
scanf("%d",&n);
} while(n == 1);

printf("Enter 1 to pop an element\n");


scanf("%d",&n);
while( n == 1)
{
top = pop(top,&value);
printf("The value poped is %d\n",value);
printf("Enter 1 to pop an element 0 to exit\n");
scanf("%d",&n);
}
printf("Enter 1 to continue 0 to exit\n");
scanf("%d",&n);
} while(n == 1);
}
# include <stdio.h>
# include <stdlib.h>
struct node
{
int data;
struct node *link;
};

void insert(struct node **front, struct node **rear, int value)


{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));

if(temp==NULL)
{
printf("No Memory available \n");
exit(0);
}
temp->data = value;
temp->link=NULL;
if(*rear == NULL)
{
*rear = temp;
*front = *rear;
}
else
{
(*rear)->link = temp;
*rear = temp;
}
}

void delete(struct node **front, struct node **rear, int *value)


{
struct node *temp;
if((*front == *rear) && (*rear == NULL))
{
printf(" The queue is empty \n");
exit(0);
}
*value = (*front)->data;
temp = *front;
*front = (*front)->link;
if(*rear == temp)
*rear = (*rear)->link;
free(temp);
}

void main()
{
struct node *front=NULL,*rear = NULL;
int n,value;
do
{
do
{
printf("Enter the element to be inserted\n");
scanf("%d",&value);
insert(&front,&rear,value);
printf("Enter 1 to continue\n");
scanf("%d",&n);
} while(n == 1);

printf("Enter 1 to delete an element\n");


scanf("%d",&n);
while( n == 1)
{
delete(&front,&rear,&value);
printf("The value deleted is %d\n",value);
printf("Enter 1 to delete an element\n");
scanf("%d",&n);
}
printf("Enter 1 to continue\n");
scanf("%d",&n);
} while(n == 1);
}
-Binary Search
#include <stdio.h>
#define MAX 10

void bsearch(int list[],int n,int element)


{
int l,u,m, flag = 0;
l = 0;
u = n-1;
while(l <= u)
{
m = (l+u)/2;
if( list[m] == element)
{
printf(" The element whose value is %d is present at
position %d in list\n",element,m);
flag =1;
break;
}
else
if(list[m] < element)
l = m+1;
else
u = m-1;
}
if( flag == 0)
printf("The element whose value is %d is not present in the
list\n",element);
}

void readlist(int list[],int n)


{
int i;
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
}

void printlist(int list[],int n)


{
int i;
printf("The elements of the list are: \n");
for(i=0;i<n;i++)
printf("%d\t",list[i]);
}

void main()
{
int list[MAX], n, element;
printf("Enter the number of elements in the list max = 10\n");
scanf("%d",&n);
readlist(list,n);
printf("\nThe list before sorting is:\n");
printlist(list,n);
printf("\nEnter the element to be searched\n");
scanf("%d",&element);
bsearch(list,n,element);
}
-Order of parenthesis
#include"stdafx.h"
#define MAX 20
#define true 1
#define false 0

int top = -1;


int stack[MAX];

void push(char);
char pop();

void main()
{
char exp[MAX],temp;
int i,valid=true;
printf("Enter an algebraic expression : ");
gets_s(exp);

for(i=0;exp[i]!=0;i++)
{
if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
push( exp[i] );
if(exp[i]==')' || exp[i]=='}' || exp[i]==']')

if(top == -1)
valid=false;
else
{
temp=pop();
if( exp[i]==')' && (temp=='{' || temp=='[') )
valid=false;
if( exp[i]=='}' && (temp=='(' || temp=='[') )
valid=false;
if( exp[i]==']' && (temp=='(' || temp=='{') )
valid=false;
}
}
if(top>=0)
valid=false;

if( valid==true )
printf("Valid expression\n");
else
printf("Invalid expression\n");
}
void push(char item)
{
if(top == (MAX-1))
printf("Stack Overflow\n");
else
{
top=top+1;
stack[top] = item;
}
}

char pop()
{
char ch;
if(top == -1)
printf("Stack Underflow\n");
else
ch=stack[top--];
return ch;
}
-Tower Of hanoi
#include <stdio.h>
#include <conio.h>
void main()
{
void hanoi(char,char,char,int);
char t1='A',t2='B',t3='C';
int n;

printf("\n Enter the no. of disks on Tower A:");


scanf("%d",&n);
if(n<1)
{
printf("\n Nothing to move");
}
else
hanoi(t1,t2,t3,n);
getch();
}
void hanoi(char t1,char t2,char t3,int n)
{
static int step=0;
step++;
printf("\n %c %c %c %d",t1,t2,t3,n);
if(n==1)
{
printf("\n Move top disk from Tower %c ----> %c",t1,t2);
return;
}
hanoi(t1,t3,t2,n-1);
printf("\n %c %c %c %d",t1,t2,t3,n);
printf("\n Move top disk from Tower %c ----> %c",t1,t2);
printf("\n %c %c %c %d",t1,t2,t3,n);
hanoi(t3,t2,t1,n-1);
printf("\n %c %c %c %d steps=%d",t1,t2,t3,n,step);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy