krishG.DS

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Practical 7

 Steps for infix to prefix and infix to postfix expression


(A) Infix to postfix expression conversion:
Step 1: start scanning of expression from left to right.
Step 2: if operand comes directly put into the output.
Step 3: if operator comes push onto the stack.
(i) the precedence of incoming operator is higher than top of
the stack push directly on to the stack.
(ii) if the precedence of incoming operator is lower than top of
the stack than first you need to pop the operator from the stack and
check the precedence of operator with new top of the stack.
Step 4: if both the operator have same operator than check
associativity ,
(i) If it is left to right then first pop the operator from stack
and then push the new operator.
(ii) If it is right to left then directly push onto the stack.
Step 5: no ned to check precedence of operator before parenthesis.
Step 6: if you find closing parenthesis pop out all the operator until
you get opening parenthesis.
Step 7: once you reach end of the expression pop out all the
operators from the stack until it is empty.
Practical-6
 Write a program to implement to stack operation:

#include<stdio.h>
#include<stdlib.h>
#define size 10
int top=-1,array[size];
void push();
void pop();
void peek();

int main(){
int choice;
while(1){
printf("\n\n perfom operation on the stack: ");
printf("\n1.push the element\n2.pop the element\
n3.show(peek operation)\n4.End");
printf("\n\n enter the choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek;
break;
case 4:
exit(0);
defualt:
printf("\n invalid choice||");
}
}
}

void push(){
int x;
if(top==size-1){
printf("\n overflow||");
}else{
printf("\n enter the element to be added onto the stack: ");
scanf("%d",&x);
top=top+1;
array[top]=x;
}
}

void pop(){
if(top==-1){
printf("\nUnderflow||");
}else{
printf("\n enter element to be remove onto the stack:
%d",array[top]);
top=top-1;
}
}

void peek(){
if(top==-1){
printf("\n underflow");
}else{
printf("\n element present in the stack: \n");
for(int i=0;i<=top;i++)
printf("%d\n",array[i]);
}
}

Practical-13
 Quick sort:

#include<stdio.h>
void swap(int*a,int*b)
{
int t=*a;
*a=*b;
*b=t;
}
int partition(int arr[],int low,int high){
int pivot=arr[low];
int start=low;
int end=high;
while(start<end){
while(arr[start]<=pivot){
start ++;
}
while(arr[end]>=pivot){
end --;
}
if(start<end){
swap(&arr[start],&arr[end]);
}
}
swap(&arr[low],&arr[end]);
return end;
}
void quicksort(int arr[],int low,int high){
if(low<high){
int pi=partition(arr,low,high);
quicksort(arr,low,pi-1);
quicksort(arr,pi+1,high);
}
}
int main(){
int arr[10],n,i;
printf("enter the array size: ");
scanf("%d",&n);
printf("\n enter array elements>>\n");
for(i=0;i<n;i++){
printf("array[%d]:>>",i);
scanf("%d",&arr[i]);
}
quicksort(arr,0,n-1);
printf("\n sorted array: \n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
return 0;
}

 Output:

enter the array size: 3

enter array elements>>


array[0]:>>23
array[1]:>>55
array[2]:>>15
sorted array:
15 23 55

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node*next;
};
struct node* head=NULL;
struct node* tail=NULL;
int size=0;
void create(){
int element;
struct node* newnode=(struct node*)malloc(sizeof(struct
node));
printf("enter element: ");
scanf("%d",&element);
newnode->data=element;
newnode->next=NULL;
if(head==NULL){
head=tail=newnode;
size++;
}else{
tail->next=newnode;
tail=newnode;
size++;
}
}
void insertatpos(){
int pos,data,i=1;
printf("enter position: ");
scanf("%d",&pos);
struct node* newnode=(struct node*)malloc(sizeof(struct
node));
printf("enter the element: ");
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
if(pos<1 || pos>size+1)
printf("invalid\n");
else if(pos==1){
newnode->next=head;
head=newnode;
size++;
}else{
struct node* temp=head;
while(i<pos+1){
temp=temp->next;
i++;
}
newnode->next=temp->next;
temp->next=newnode;
size++;
}
}

void display(){
struct node* temp;
temp=head;
if(temp==NULL){
printf("list is empty\n");
}else{
while(temp!=NULL){
printf("%d-->",temp->data);
temp=temp->next;
}
printf("\n");
}
}

void deletefrompos(){
struct node* temp;
int pos;
printf("enter position to delete: ");
scanf("%d",&pos);
if(pos>size){
printf("invalid position\n");
}else if(pos==1){
temp=head;
head=temp->next;
printf("deleted node is:%d\n",temp->data);
free(temp);
size--;
}else{
struct node* p=head,*temp;
int i=1;
while(i<pos-1){
p=p->next;
i++;
}
temp=p->next;
p->next=temp->next;
printf("deleted node is: %d\n",temp->data);
free(temp);
size--;
}
}

int main(){
int ch;
while(1){
printf("single linked list operation: \n");
printf("1.create a list\n");
printf("2.insert at specific posititon\n");
printf("3.display\n");
printf("4.delete from specific position\n");
printf("5.exit\n");
printf("enter your choice: ");
printf("%d",&ch);

switch(ch){
case 1: create();
break;
case 2: insertatpos();
break;
case 3: display();
break;
case 4: deletefrompos();
break;
case 5: exit(0);
default: printf("invalid choice");
}
}
return 0;
}

You might also like