DS
DS
Department of BCA
Program:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
top=-1;
scanf("%d",&n);
printf("\n\t------------------------");
do
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
{
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
pop();
break;
case 3:
display();
break;
case 4:
printf("\n\tExist point");
break;
default:
while(choice=4);
return 0;
void push()
if(top>=n-1)
else
scanf("%d",&x);
top++;
stack[top]=x;
void pop()
if(top<=-1)
else
top--;
void display()
if(top>=0)
for(i=top;i>=0;i--)
printf("\n%d",stack[i]);
else
Output:
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Program:
#include<stdio.h>
#include<string.h>
#define size 20
char stack[size];
if(top==(size-1))
printf("Stack is Overflow\n");
else
stack[++top]=ch;
char pop()
if(top==-1)
printf("Stack is Underflow\n");
else
return stack[top--];
int main()
char str[20];
int i;
gets(str);
for(i=0;i<strlen(str);i++)
push(str[i]);
for(i=0;i<strlen(str);i++)
str[i]=pop();
puts(str);
Output:
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Program:
#include<stdio.h>
char stack[100];
int top= -1;
void push(char x)
{
stack[++top]=x;
}
char pop()
{
if(top== -1)
return-1;
else
return stack[top--];
}
int priority(char x)
{
if(x=='(')
return 0;
if(x=='+'|| x=='-')
return 1;
if(x=='*'|| x=='/')
return 2;
return 0;
}
int main()
{
char exp[100];
char*e,x;
printf("Enter the expression:");
scanf("%s",exp);
printf("\n");
e=exp;
while(*e !='\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e =='(')
push(*e);
else if(*e==')')
{
while((x=pop()!='('))
printf("%c",x);
}
else
{
while(priority(stack[top])>=priority(*e))
printf("%c",pop());
push(*e);
}
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
e++;
}
while(top!=-1)
{
printf("%c",pop());
}
return 0;
}
Output:
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
#include<stdio.h>
int main()
{
int queue[100],ch=1,front=0,rear=0,i,j=1,n;
printf("Enter size of the Queue:");
scanf("%d",&n);
printf("Queue using Array");
printf("\n 1.Insertion\n 2.Deletion\n 3.Display\n 4.Exit");
while(ch)
{
printf("\n Enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==n)
printf("\nQueue is full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
n++;
}
break;
case 3:
printf("\n Queue Elements are:\n");
if(front==rear)
printf("\n Queue is empty");
else
{
for(i=front;i<rear;i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Output:
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
}
}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
}
}
}
void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
}
void begin_delete()
{
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
if(ptr == NULL)
{
printf("\nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not found\n");
}
}
void display()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
else
{
printf("\ n printing values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}
Output:
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
};
void printList() {
printf("\n[head] =>");
while(ptr != NULL) {
printf(" %d =>",ptr->data);
ptr = ptr->next;
printf(" [null]\n");
//create a link
//link->key = key;
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
link->data = data;
link->next = head;
head = link;
int main() {
insert(10);
insert(20);
insert(30);
insert(1);
insert(40);
insert(56);
printList();
return 0;
Output:
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
link->data = data;
link->next = NULL;
current = head;
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
printf("\n[head] =>");
printf(" %d =>",ptr->data);
printf(" [head]\n");
}
int main() {
insert(10);
insert(20);
insert(30);
insert(1);
insert(40);
insert(56);
printList();
return 0;
}
Output:
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
#include <stdio.h>
#include <stdlib.h>
// defining a node
int data;
} Node;
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
if (*head == NULL) {
*head = newNode;
return;
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
if (*head == NULL) {
*head = newNode;
return;
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
if (position < 1) {
return;
if (position == 1) {
insertAtBeginning(head, data);
return;
temp = temp->next;
if (temp == NULL) {
printf(
return;
newNode->next = temp->next;
newNode->prev = temp;
if (temp->next != NULL) {
temp->next->prev = newNode;
}
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
temp->next = newNode;
if (*head == NULL) {
return;
*head = (*head)->next;
if (*head != NULL) {
(*head)->prev = NULL;
free(temp);
if (*head == NULL) {
return;
if (temp->next == NULL) {
*head = NULL;
free(temp);
return;
temp = temp->next;
temp->prev->next = NULL;
free(temp);
if (*head == NULL) {
return;
if (position == 1) {
deleteAtBeginning(head);
return;
temp = temp->next;
if (temp == NULL) {
"nodes.\n");
return;
if (temp->next != NULL) {
temp->next->prev = temp->prev;
if (temp->prev != NULL) {
temp->prev->next = temp->next;
free(temp);
temp = temp->next;
printf("\n");
if (temp == NULL) {
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
return;
temp = temp->next;
// Traverse backwards
temp = temp->prev;
printf("\n");
int main()
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtBeginning(&head, 5);
printf("After Insertions:\n");
printListForward(head);
printListReverse(head);
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
deleteAtBeginning(&head); // List: 15 10 20
deleteAtEnd(&head); // List: 15 10
printf("After Deletions:\n");
printListForward(head);
return 0;
Output:
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Program:
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
void main ()
{
int choice=0;
printf("\n*********Stack operations using linked list*********\n");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("\n\nChose one from the below options...\n");
printf("\n 1.Push\n 2.Pop\n 3.Show\n 4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice ");
}
};
}
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
printf("Enter the value:");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;
}
printf("Item pushed");
}
}
void pop()
{
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Output:
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\n");
printf("\
n=================================================================\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice :");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\n printing values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
}
}
Output:
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
#include<stdio.h>
#include<conio.h>
void main()
int A[]={34,15,29,8,1};
int i;
bubble_sort(A,5);
for(i=0;i<=4;i++)
printf("\n%d",A[i]);
getch();
bubble_sort(int A[],int N)
int round,i,temp;
for(round=1;round<=N-1;round++)
for(i=0;i<=N-1-round;i++)
if(A[i]>A[i+1])
temp=A[i];
A[i]=A[i+1];
A[i+1]=temp;
return 0;
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Output:
#include<math.h>
#include<stdio.h>
int i,key,j;
for(i=1;i<n;i++){
key= arr[i];
j=i-1;
while(j>=0 &&arr[j]>key){
arr[j+1]= arr[j];
j=j-1;
arr[j+1]=key;
int i;
for(i=0;i<n;i++)
printf(" %d",arr[i]);
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
printf("\n");
int main()
int arr[]={12,11,13,5,6};
int n= sizeof(arr)/sizeof(arr[0]);
insertion_sort(arr,n);
printarray(arr,n);
return 0;
Output:
#include<stdio.h>
int i,j,small;
for(i=0;i<n-1;i++)
small=i;
for(j=i+1;j<n;j++)
if(arr[j]<arr[small])
small=j;
int temp=arr[small];
arr[small]=arr[i];
arr[i]=temp;
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
int i;
for(i=0;i<n;i++)
printf(" %d",a[i]);
int main()
int a[]={12,31,25,8,32,17};
int n= sizeof(a)/sizeof(a[0]);
printarr(a,n);
selection(a,n);
printarr(a,n);
return 0;
Output:
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
#include <stdio.h>
int main()
scanf("%d",&number);
scanf("%d",&array[c]);
scanf("%d",&search);
break;
if ( c == number )
return 0;
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
Output:
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
#include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &array[c]);
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
first = middle + 1;
break;
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA
else
last = middle - 1;
return 0;
Output:
Page No :