DS Practical 1
DS Practical 1
DS Practical 1
#include<stdio.h>
long factorial(int n)
if (n == 0)
return 1;
else
return(n * factorial(n-1));
void main()
int number;
long fact;
scanf("%d", &number);
fact = factorial(number);
return 0;
OUTPUT :
PROGRAM NO. 2
#include <stdio.h>
int i, j;
swap(&arr[j], &arr[j+1]);
int temp;
temp = *a;
*a = *b;
*b = temp;
int main()
scanf("%d", &size);
scanf("%d", &array[i]);
bubblesort(array, size);
return 0;
OUTPUT:
PROGRAM NO. 3
#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;
else
last = middle - 1;
middle = (first + last)/2;
return 0;
OUTPUT:
PROGRAM NO. 4
#include <stdio.h>
void main()
{ int num;
scanf("%d", &num);
int array[num];
scanf("%d", &array[i]);
scanf("%d", &keynum);
if (keynum == array[i] )
found = 1;
break;
if (found == 1)
OUTPUT:
PROGRAM NO. 5
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
}*head;
void reverseList();
void displayList();
int main()
int n, choice;
scanf("%d", &n);
createList(n);
displayList();
printf("\nPress 1 to reverse the order of singly linked list\n");
scanf("%d", &choice);
if(choice == 1)
reverseList();
displayList();
return 0;
void createList(int n)
int data, i;
if(n <= 0)
return;
if(head == NULL)
{
else
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
if(newNode == NULL)
break;
else
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
/*
*/
void reverseList()
if(head != NULL)
prevNode = head;
curNode = head->next;
head = head->next;
while(head != NULL)
head = head->next;
curNode->next = prevNode;
prevNode = curNode;
curNode = head;
void displayList()
if(head == NULL)
printf("List is empty.");
else
temp = head;
while(temp != NULL)
temp = temp->next;
}
}
PROGRAM NO. 6
#include <stdio.h>
# define max 8
int front=-1;
int rear=-1;
front=0;
rear=0;
queue[rear]=element;
else if((rear+1)%max==front)
printf("Queue is overflow..");
else
rear=(rear+1)%max;
queue[rear]=element;
int dequeue()
{
if((front==-1) && (rear==-1))
printf("\nQueue is underflow..");
else if(front==rear)
front=-1;
rear=-1;
else
front=(front+1)%max;
void display()
int i=front;
else
while(i<=rear)
printf("%d,", queue[i]);
i=(i+1)%max;
}
int main()
int choice=1,x;
scanf("%d", &choice);
switch(choice)
case 1:
scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
}}
return 0;
OUTPUT:
PROGRAM NO. 7
#include <stdio.h>
int i;
return i;
return -1;
a += index;
*a = *(a + 1);
a++;
size--;
a -= size;
int i = 0;
a += size;
*a = *(a - 1);
a--;
*a = elem;
a -= pos - 1;
size++;
int main()
char ch;
scanf("%d", &size);
scanf("%d", &a[i]);
if (ch == 'i')
scanf("%d", &elem);
printf("Enter positon");
scanf("%d", &pos);
scanf("%d", &elem);
else
printf("Wrong choice");
return 0;
OUTPUT:
PROGRAM NO. 8
#include <stdio.h>
int main()
int m, i, j, temp;
int arr[87];
scanf("%d", &m);
scanf("%d", &arr[i]);
j = i;
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
printf("%d\n", arr[i]);
}
return 0;
}
PROGRAM NO. 9
#include <stdio.h>
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void display();
void peek();
void main ()
scanf("%d",&n);
printf("\n----------------------------------------------\n");
while(choice != 4)
printf("\n1.Push\n2.Pop\n3.Display\n4.Peek\n5.Exit");
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
{
pop();
break;
case 3:
display();
break;
case 4:
peek();
break;
case 5:
printf("Exiting....");
break;
default:
};
void push ()
int val;
if (top == n )
printf("\n Overflow");
else
scanf("%d",&val);
stack[top] = val;
void pop ()
if(top == -1)
printf("Underflow");
else
void display()
for (i=top;i>=0;i--)
printf("%d\n",stack[i]);
if(top == -1)
printf("Stack is empty");
void peek()
if(top==-1)
printf("nothing at top, it's empty");
else
OUTPUT:
PROGRAM NO. 10
Q. Write a C program to implement stack using link list with the following operations:
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
};
int x;
void menu();
void push ()
int data;
printf("\nENTER DATA:");
scanf("%d",&x);
ptr->data= x;
ptr->next= top;
top=ptr;
printf("\nITEM PUSHED\n");
menu();
void pop()
int item;
if (top == NULL)
printf("\nUNDERFLOW");
else
item = top->data;
ptr = top;
top = top->next;
free(ptr);
printf("\nITEM POPPED");
menu();
int peek(){
if(top!= NULL)
printf("%d", top->data);
else
printf("\nSTACK IS EMPTY\n");
menu();
void display()
int i;
ptr=top;
if(ptr == NULL)
printf("\nSTACK IS EMPTY\n");
else
while(ptr!=NULL)
printf("%d,",ptr->data);
ptr= ptr->next;
}
menu();
void menu(){
int choice;
do
printf("\n 1. TO PUSH");
printf("\n 2. TO DISPLAY");
printf("\n 3. TO POP");
printf("\n 4. TO PEEK");
printf("\nPRESS 0 TO EXIT");
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
display();
break;
case 3:
pop();
break;
case 4:
peek();
break;
case 0:
exit(0);
default:
break;
int main ()
menu();
}
PROGRAM 11
Q. Write a C program to create, insert, delete, count and display elements in a singly
#include <stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
int data;
};
int main()
int ch,choice;
int create();
void insert_beg();
void insert_end();
int insert_pos();
void display();
void delete_beg();
void delete_end();
int delete_position();
while(1)
{
scanf("%d",&ch);
switch(ch)
create();
break;
scanf("%d",&ch);
switch(ch)
case 1: insert_beg();
break;
case 2: insert_end();
break;
case 3: insert_pos();
break;
case 4: break;
break;
case 3: display();
break;
scanf("%d",&choice);
switch(choice)
case 1: delete_beg();
break;
case 2: delete_end();
break;
case 3: delete_position();
break;
case 4: break;
break;
case 5: exit(0);
default:printf("invalid choice");
return 0;
int create()
int num,ch1;
while(ch1)
{
newnode=(struct node *)malloc(sizeof(struct node));
scanf("%d", &num);
newnode->data=num;
newnode->next=NULL;
if(head==NULL)
head=newnode;
temp=newnode;
else
temp->next=newnode;
temp=newnode;
scanf("%d",&ch1);
return 0;
void insert_beg()
int num;
printf("Enter data:");
scanf("%d",&num);
newnode->data=num;
if(head==NULL)
newnode->next=NULL;
head=newnode;
else
newnode->next=head;
head=newnode;
void insert_end()
int num;
printf("Enter data:");
scanf("%d",&num);
newnode->data=num;
newnode->next=NULL;
if(head==NULL)
head=newnode;
else
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
}
int insert_pos()
int pos,i,num;
if(head==NULL)
printf("List is empty!!");
return 0;
printf("Enter data:");
scanf("%d",&num);
scanf("%d",&pos);
newnode->data=num;
temp=head;
for(i=1;i<pos-1;i++)
if(temp->next==NULL)
return 0;
temp=temp->next;
newnode->next=temp->next;
temp->next=newnode;
return 0;
void display()
int count=0;
if(head==NULL)
printf("List is empty!!");
else
temp=head;
while(temp!=NULL)
printf("%d->",temp->data);
temp=temp->next;
count++;
void delete_beg()
if(head==NULL)
else
{
temp=head;
head=head->next;
free(temp);
void delete_end()
if(head==NULL)
else
temp=head;
while(temp->next!=NULL)
prevnode=temp;
temp=temp->next;
if(temp==head)
head=NULL;
else
prevnode->next=NULL;
free(temp);
}
int delete_position()
int position,i;
if(head==NULL)
return 0;
printf("\n enter the position from where you want to delete the data");
scanf("%d",&position);
temp=head;
for(i=1;i<position-1;i++)
if(temp->next==NULL)
return 0;
temp=temp->next;
nextnode=temp->next;
temp->next=nextnode->next;
free(nextnode);
return 0;
PROGRAM 12
Q. Write a C program to create, insert, delete, count and display elements in a doubly
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
void insertion_beginning();
void insertion_last();
void insertion_specified();
void deletion_beginning();
void deletion_last();
void deletion_specified();
void display();
void search();
void main ()
while(choice != 9)
printf("\n*********Main Menu*********\n");
printf("\n===============================================\n");
scanf("\n%d",&choice);
switch(choice)
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beginning();
break;
case 5:
deletion_last();
break;
case 6:
deletion_specified();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
void insertion_beginning()
int item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
scanf("%d",&item);
if(head==NULL)
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
else
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
printf("\nNode inserted\n");
void insertion_last()
int item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
else
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
printf("\nnode inserted\n");
void insertion_specified()
int item,loc,i;
if(ptr == NULL)
printf("\n OVERFLOW");
else
temp=head;
scanf("%d",&loc);
for(i=0;i<loc;i++)
temp = temp->next;
if(temp == NULL)
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
void deletion_beginning()
if(head == NULL)
printf("\n UNDERFLOW");
head = NULL;
free(head);
printf("\nnode deleted\n");
else
ptr = head;
printf("\nnode deleted\n");
void deletion_last()
if(head == NULL)
printf("\n UNDERFLOW");
head = NULL;
free(head);
printf("\nnode deleted\n");
else
ptr = head;
if(ptr->next != NULL)
free(ptr);
printf("\nnode deleted\n");
void deletion_specified()
{
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
ptr = head;
printf("\nCan't delete\n");
else
free(temp);
printf("\nnode deleted\n");
void display()
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
void search()
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
printf("\nEmpty List\n");
else
scanf("%d",&item);
while (ptr!=NULL)
if(ptr->data == item)
flag=0;
break;
else
flag=1;
i++;
ptr = ptr -> next;
if(flag==1)
}
PROGRAM 13
Q. Write a C program to insert, delete, count and display elements in a circular link
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
while(choice != 7)
printf("\n*********Main Menu*********\n");
printf("\n===============================================\n");
scanf("\n%d",&choice);
switch(choice)
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
begin_delete();
break;
case 4:
last_delete();
break;
case 5:
search();
break;
case 6:
display();
break;
case 7:
exit(0);
break;
default:
void beginsert()
{
int item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
scanf("%d",&item);
if(head == NULL)
head = ptr;
else
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
head = ptr;
printf("\nnode inserted\n");
}
void lastinsert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW\n");
else
printf("\nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
head = ptr;
else
temp = head;
printf("\nnode inserted\n");
}
void begin_delete()
if(head == NULL)
printf("\nUNDERFLOW");
head = NULL;
free(head);
printf("\nnode deleted\n");
else
{ ptr = head;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");
void last_delete()
{
struct node *ptr, *preptr;
if(head==NULL)
printf("\nUNDERFLOW");
head = NULL;
free(head);
printf("\nnode deleted\n");
else
ptr = head;
preptr=ptr;
ptr = ptr->next;
free(ptr);
printf("\nnode deleted\n");
void search()
int item,i=0,flag=1;
ptr = head;
if(ptr == NULL)
printf("\nEmpty List\n");
else
scanf("%d",&item);
flag=0;
else
if(ptr->data == item)
flag=0;
break;
else
flag=1;
i++;
}
}
if(flag != 0)
void display()
ptr=head;
if(head == NULL)
printf("\nnothing to print");
else