Beruflich Dokumente
Kultur Dokumente
SG-14330
Page 1
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("Stack is Full\n");
return;
}
else
{
printf ("Enter the element to be pushed\n");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}
int pop () // If you want to delete element
{
int num;
if (s.top == - 1)
{
printf ("Stack is Empty\n");
return (s.top);
}
else
{
num = s.stk[s.top];
printf ("poped element is = %dn", s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}
/* Function to display the status of the stack */
void display ()
{
int i;
if (s.top == -1)
{
printf ("Stack is empty\n");
return;
}
else
{
printf ("\n The status of the stack is \n");
for (i = s.top; i >= 0; i--)
{
printf ("%d\n", s.stk[i]);
}
}
printf ("\n");
}
Parth Sharma
SG-14330
Page 2
Output:
SG-14330
Page 3
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main()
{
int no, ch, e;
printf("\n Press 1 to Push");
printf("\n Press 2 to Pop");
printf("\n Press 3 to Top");
printf("\n Press 4 to Empty");
printf("\n Press 5 to Exit");
printf("\n Press 6 to Dipslay");
printf("\n Press 7 to Stack Count");
printf("\n Press 8 to Destroy stack");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
Parth Sharma
SG-14330
Page 4
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
void create()
{
top = NULL;
}
void stack_count()
{
printf("\n No. of elements in stack : %d", count);
}
void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
void display()
{
top1 = top;
if (top1 == NULL)
{
Parth Sharma
SG-14330
Page 5
printf("Stack is empty");
return;
}
while (top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
void pop()
{
top1 = top;
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
int topelement()
{
return(top->info);
}
void
{
if (top
empty()
== NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d
count);
elements",
}
void
{
top1 =
while
{
top1 =
destroy()
top;
(top1 != NULL)
top->ptr;
free(top);
top1;
top =
Parth Sharma
SG-14330
Page 6
top1 = top1->ptr;
}
free(top1);
top = NULL;
printf("\n All stack elements destroyed");
count = 0;
}
OUTPUT:
SG-14330
Page 7
for(i=position;i<num;i++)
{
a[i]=a[i+1];
}
a[position]=item;
num=num+1;
printf("\nThe final result is");
for(i=0;i<num;i++)
{
printf(" %d",a[i]);
}
}
OUTPUT:
SG-14330
Page 8
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /*End of switch*/
} /*End of while*/
} /*End of main()*/
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
//return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /*End of delete() */
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
Parth Sharma
SG-14330
Page 9
printf("Queue is : \n");
for (i =
}
}
OUTPUT:
SG-14330
Page 10
}
}
}
void insert()
{
int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element that you want to insert:");
scanf("%d",&x);
if(rear==max-1&&front>0)
{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
}
}
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
printf("Queue is in underflow\n");
exit(1);
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else
if(front==max-1)
{
a=q[front];
front=0;
}
else a=q[front++];
printf("Deleted element is:%d\n",a);
}
void display()
{
int i,j;
Parth Sharma
SG-14330
Page 11
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
exit(1);
}
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("\t%d",q[i]);
for(j=front;j<=max-1;j++)
printf("\t%d",q[j]);
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
else
{
for(i=front;i<=rear;i++)
{
printf("\t%d",q[i]);
}
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
printf("\n");
}
OUTPUT:
Parth Sharma
SG-14330
Page 12
SG-14330
Page 13
int count = 0;
void main()
{
int no, ch, e;
printf("\n Press 1 for Enque");
printf("\n Press 2 for Deque");
printf("\n Press 3 for Front element");
printf("\n Press 4 for Empty");
printf("\n Press 5 for Exit");
printf("\n Press 6 for Display");
printf("\n Press 7 for Queue size");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
Parth Sharma
SG-14330
Page 14
}
void create()
{
front = rear = NULL;
}
void queuesize()
{
printf("\n Queue size : %d", count);
}
void enq(int data)
{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
}
else
{
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
}
count++;
}
void deq()
{
front1 = front;
if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
front = front1;
Parth Sharma
SG-14330
Page 15
}
else
{
printf("\n Dequed value : %d", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
void display()
{
front1 = front;
if ((front1 == NULL) && (rear == NULL))
{
printf("Queue is empty");
return;
}
while (front1 != rear)
{
printf("%d ", front1->info);
front1 = front1->ptr;
}
if (front1 == rear)
printf("%d", front1->info);
}
int frontelement()
{
if ((front != NULL) && (rear != NULL))
return(front->info);
else
return 0;
}
/* Display if queue is empty or not */
void empty()
{
if ((front == NULL) && (rear == NULL))
printf("\n Queue empty");
else
printf("Queue not empty");
}
OUTPUT:
Parth Sharma
SG-14330
Page 16
SG-14330
Page 17
a[j+1]=temp;
}
}
OUTPUT:
Q8. WAP to sort an array of integers in ascending order using selection sort
method.
#include<stdio.h>
//#include<conio.h>
void selectionSortMethod(int a[], int n);
void main()
{
int i, n, a[50];
printf("\nEnter Size of Array: ");
scanf("%d",&n);
printf("\n Enter %d elements of array \n\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selectionSortMethod(a, n);
printf("\n\n Sorted list of elements\n\n");
for(i=0;i<n;i++)
printf("%d\t", a[i]);
printf("\n");
}/*End of main Function*/
void selectionSortMethod(int a[], int n)
{
int i, loc, j, small;
for (i=1;i<n;i++){
small=a[i-1];
Parth Sharma
SG-14330
Page 18
loc=i-1;
for(j=i;j<n;j++){
if(a[j]<small){
small=a[j];
loc=j;
}
}
if(loc!=(i-1))
{
int temp=a[i-1];
a[i-1]=a[loc];
a[loc]=temp;
}
}
}
OUTPUT:
Parth Sharma
SG-14330
Page 19
SG-14330
Page 20
SG-14330
Page 21
}
while (choice !=4);
}//End of main function
boolean isEmpty()
{
if (front==-1)
return true;
else
return false;
}
boolean isFull()
{
if (((front==0)&&(rear==MAX-1)) || (front==rear+1))
return true;
else
return false;
}
void enqueue (int value)
{
if (front==-1)
front =rear=0;
else if (rear==MAX-1)
rear=0;
else
rear++;
elements[rear]=value;
}
int dequeue()
{
int temp;
temp=elements[front];
if(front==rear)
front=rear=-1;
else if (front==MAX-1)
front=0;
else
front++;
Parth Sharma
SG-14330
Page 22
return temp;
}
int peek()
{
return elements[front];
}
Q10. WAP to sort an array of integers in ascending order using merge sort
method.
#include<stdio.h>
#include<conio.h>
void mergeSortMethod(int a[], int beg, int end);
void mergingSortedSubArrays(int a[50], int lb, int lr, int rb, int rr);
void main()
{
int i, n, a[50];
printf("\nEnter Size of Array: ");
scanf("%d",&n);
printf("\n Enter %d elements of array \n\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selectionSortMethod(a, n);
printf("\n\n Sorted list of elements\n\n");
for(i=0;i<n;i++)
printf("%d\t", a[i]);
printf("\n");
Parth Sharma
SG-14330
Page 23
SG-14330
Page 24
Parth Sharma
SG-14330
Page 25