Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DS Practical 1

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

PROGRAM NO.

Q. Write a C program to calculate factorial of a given number using recursion.

#include<stdio.h>

long factorial(int n)

if (n == 0)

return 1;

else

return(n * factorial(n-1));

void main()

int number;

long fact;

printf("Enter a number: ");

scanf("%d", &number);

fact = factorial(number);

printf("Factorial of %d is %ld\n", number, fact);

return 0;

OUTPUT :
PROGRAM NO. 2

Q. Write a C program to implement the concept of Bubble sort.

#include <stdio.h>

void bubblesort(int arr[], int size)

int i, j;

for (i = 0; i < size; i++)

for (j = 0; j < size - i; j++)

if (arr[j] > arr[j+1])

swap(&arr[j], &arr[j+1]);

void swap(int *a, int *b)

int temp;

temp = *a;

*a = *b;

*b = temp;

int main()

int array[100], i, size;

printf("How many numbers you want to sort: ");

scanf("%d", &size);

printf("\nEnter %d numbers : ", size);

for (i = 0; i < size; i++)

scanf("%d", &array[i]);
bubblesort(array, size);

printf("\nSorted array is ");

for (i = 0; i < size; i++)

printf(" %d ", array[i]);

return 0;

OUTPUT:
PROGRAM NO. 3

Q. Write a C program to implement the concept of Binary search.

#include <stdio.h>

int main()

int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)

scanf("%d", &array[c]);

printf("Enter value to find\n");

scanf("%d", &search);

first = 0;

last = n - 1;

middle = (first+last)/2;

while (first <= last) {

if (array[middle] < search)

first = middle + 1;

else if (array[middle] == search) {

printf("%d found at location %d.\n", search, middle+1);

break;

else

last = middle - 1;
middle = (first + last)/2;

if (first > last)

printf("Not found! %d isn't present in the list.\n", search);

return 0;

OUTPUT:
PROGRAM NO. 4

Q. Write a C program to implement the concept of Linear search.

#include <stdio.h>

void main()

{ int num;

int i, keynum, found = 0;

printf("Enter the number of elements ");

scanf("%d", &num);

int array[num];

printf("Enter the elements one by one \n");

for (i = 0; i < num; i++)

scanf("%d", &array[i]);

printf("Enter the element to be searched ");

scanf("%d", &keynum);

for (i = 0; i < num ; i++)

if (keynum == array[i] )

found = 1;

break;

if (found == 1)

printf("Element is present in the array at position %d",i+1);


else

printf("Element is not present in the array\n");

OUTPUT:
PROGRAM NO. 5

Q. Write a C program to reverse a singly linked list.

#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node *next;

}*head;

void createList(int n);

void reverseList();

void displayList();

int main()

int n, choice;

printf("Enter total number of nodes: ");

scanf("%d", &n);

createList(n);

printf("\nData in the list \n");

displayList();
printf("\nPress 1 to reverse the order of singly linked list\n");

scanf("%d", &choice);

if(choice == 1)

reverseList();

printf("\nData in the list\n");

displayList();

return 0;

void createList(int n)

struct node *newNode, *temp;

int data, i;

if(n <= 0)

printf("List size must be greater than zero.\n");

return;

head = (struct node *)malloc(sizeof(struct node));

if(head == NULL)
{

printf("Unable to allocate memory.");

else

printf("Enter the data of node 1: ");

scanf("%d", &data);

head->data = data;

head->next = NULL;

temp = head;

for(i=2; i<=n; i++)

newNode = (struct node *)malloc(sizeof(struct node));

if(newNode == NULL)

printf("Unable to allocate memory.");

break;

else

printf("Enter the data of node %d: ", i);

scanf("%d", &data);

newNode->data = data;
newNode->next = NULL;

temp->next = newNode;

temp = temp->next;

printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");

/*

* Reverse the order of nodes of a singly linked list

*/

void reverseList()

struct node *prevNode, *curNode;

if(head != NULL)

prevNode = head;

curNode = head->next;

head = head->next;

prevNode->next = NULL; // Make first node as last node

while(head != NULL)

head = head->next;

curNode->next = prevNode;
prevNode = curNode;

curNode = head;

head = prevNode; // Make last node as head

printf("SUCCESSFULLY REVERSED LIST\n");

void displayList()

struct node *temp;

if(head == NULL)

printf("List is empty.");

else

temp = head;

while(temp != NULL)

printf("Data = %d\n", temp->data);

temp = temp->next;

}
}
PROGRAM NO. 6

Q. Write a C program to implement circular queue using array.

#include <stdio.h>

# define max 8

int queue[max]; // array declaration

int front=-1;

int rear=-1;

// function to insert an element in a circular queue

void enqueue(int element)

if(front==-1 && 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)

printf("\nThe dequeued element is %d", queue[front]);

front=-1;

rear=-1;

else

printf("\nThe dequeued element is %d", queue[front]);

front=(front+1)%max;

void display()

int i=front;

if(front==-1 && rear==-1)

printf("\n Queue is empty..");

else

printf("\nElements in a Queue are :");

while(i<=rear)

printf("%d,", queue[i]);

i=(i+1)%max;
}

int main()

int choice=1,x;

while(choice<4 && choice!=0)

printf("\n 1: Insert an element");

printf("\n 2: Delete an element");

printf("\n 3: Display the element");

printf("\nEnter your choice");

scanf("%d", &choice);

switch(choice)

case 1:

printf("Enter the element to be inserted");

scanf("%d", &x);

enqueue(x);

break;

case 2:

dequeue();

break;

case 3:

display();

}}
return 0;

OUTPUT:
PROGRAM NO. 7

Q. Write a C program to insert and delete elements in an array at a given position.

#include <stdio.h>

int search(int *a, int size, int elem)

int i;

for (i = 0; i < size; i++)

if (elem == *(a + i))

return i;

return -1;

void delete (int *a, int size, int elem)

int i, index = search(a, size, elem);

a += index;

for (i = index; i < size - 1; i++)

*a = *(a + 1);

a++;

size--;

a -= size;

for (i = 0; i < size; i++)


{

printf("%d ", *(a + i));

void insert(int *a, int size, int elem, int pos)

int i = 0;

a += size;

for (i = size; i >= pos; i--)

*a = *(a - 1);

a--;

*a = elem;

a -= pos - 1;

size++;

for (i = 0; i < size; i++)

printf("%d ", *(a + i));

int main()

int a[100], size, i, elem, pos;

char ch;

printf("Enter size of array");

scanf("%d", &size);

for (i = 0; i < size; i++)


{

printf("Enter %d element", i + 1);

scanf("%d", &a[i]);

printf("Enter i to insert / d to delete");

scanf(" %c", &ch);

if (ch == 'i')

printf("Enter an element to insert");

scanf("%d", &elem);

printf("Enter positon");

scanf("%d", &pos);

insert(a, size, elem, pos);

else if (ch == 'd')

printf("Enter an element to delete");

scanf("%d", &elem);

delete (a, size, elem);

else

printf("Wrong choice");

return 0;

OUTPUT:
PROGRAM NO. 8

Q. Write a C program to implement the concept of Insertion sort.

#include <stdio.h>

int main()

int m, i, j, temp;

int arr[87];

printf("Enter number of elements\n");

scanf("%d", &m);

printf("Enter %d integers\n", m);

for (i = 0; i < m; i++)

scanf("%d", &arr[i]);

for (i = 1 ; i <= m - 1; i++)

j = i;

while ( j > 0 && arr[j-1] > arr[j])

temp = arr[j];

arr[j] = arr[j-1];

arr[j-1] = temp;

j--;

printf("Sorted list in ascending order:\n");

for (i = 0; i <= m - 1; i++)

printf("%d\n", arr[i]);
}

return 0;

}
PROGRAM NO. 9

Q. C program to implement stack using array with the following operations:

push(), pop(), display(), peek().

#include <stdio.h>

int stack[100],i,j,choice=0,n,top=-1;

void push();

void pop();

void display();

void peek();

void main ()

printf("Enter the number of elements in the stack ");

scanf("%d",&n);

printf("*********Stack operations using array*********");

printf("\n----------------------------------------------\n");

while(choice != 4)

printf("Chose one from the below options...\n");

printf("\n1.Push\n2.Pop\n3.Display\n4.Peek\n5.Exit");

printf("\n Enter your choice \n");

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:

printf("Please Enter valid choice ");

};

void push ()

int val;

if (top == n )
printf("\n Overflow");

else

printf("Enter the value?");

scanf("%d",&val);

top = top +1;

stack[top] = val;

void pop ()

if(top == -1)

printf("Underflow");

else

top = top -1;

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

printf("\n the element at peek is %d",stack[top]);

OUTPUT:
PROGRAM NO. 10

Q. Write a C program to implement stack using link list with the following operations:

push(), pop(), display(), peek().

#include <stdio.h>

#include <stdlib.h>

struct node

int data;

struct node *next;

};

struct node *top;

int x;

void menu();

void push ()

int data;

struct node*ptr =(struct node*)malloc(sizeof(struct node));

printf("\nENTER DATA:");

scanf("%d",&x);

ptr->data= x;

ptr->next= top;

top=ptr;

printf("\nITEM PUSHED\n");

menu();

void pop()

int item;

struct node *ptr;

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;

struct node *ptr;

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("\t\t\tSTACK THROUGH LINKED LIST");

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");

printf("\nENTER YOUR CHOICE: ");

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:

printf("WRONG INPUT, TRY AGAIN");

break;

}while(choice >= 4);

int main ()

menu();

}
PROGRAM 11

Q. Write a C program to create, insert, delete, count and display elements in a singly

link list. (consider all cases - at beg, end, at specific position)

#include <stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

};

struct node *head=NULL, *newnode, *prevnode, *nextnode, *temp, *n, *m;

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)
{

printf("\n\n******* SINGLY LINKED LIST MENU ***********");

printf("\n 1:create\n 2:insert\n 3:display\n 4:delete\n 5:exit\n");

printf("\n enter your choice");

scanf("%d",&ch);

switch(ch)

case 1: printf("create list for the first time");

create();

break;

case 2: printf("\n--------insert menu------");

printf("\n 1)insert at beginning\n 2)inert at end\n 3)insert at position\n 4)exit");

printf("\n enter your choice(1-4)");

scanf("%d",&ch);

switch(ch)

case 1: insert_beg();

break;

case 2: insert_end();

break;

case 3: insert_pos();

break;

case 4: break;

default: printf("wrong choice");

break;

case 3: display();

break;

case 4: printf("\n---------------delete menu---------");


printf("\n 1)delete element at beginning\n 2)delete element at end\n 3)delete element at
position\n 4)exit");

printf("\n enter your choice(1-4)");

scanf("%d",&choice);

switch(choice)

case 1: delete_beg();

break;

case 2: delete_end();

break;

case 3: delete_position();

break;

case 4: break;

default: printf("\n no such choice exist");

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));

printf("\n enter data in link list");

scanf("%d", &num);

newnode->data=num;

newnode->next=NULL;

if(head==NULL)

head=newnode;

temp=newnode;

else

temp->next=newnode;

temp=newnode;

printf("\n do you want to continue(0,1)?");

scanf("%d",&ch1);

return 0;

void insert_beg()

int num;

newnode=(struct node*)malloc(sizeof(struct node));

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;

newnode=(struct node*)malloc(sizeof(struct node));

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;

newnode=(struct node*)malloc(sizeof(struct node));

printf("Enter data:");

scanf("%d",&num);

printf("Enter position to insert:");

scanf("%d",&pos);

newnode->data=num;

temp=head;

for(i=1;i<pos-1;i++)

if(temp->next==NULL)

printf("There are less elements!!");

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;

printf("The linked list is:\n");

while(temp!=NULL)

printf("%d->",temp->data);

temp=temp->next;

count++;

printf("\n total elements in list are: %d", count);

void delete_beg()

if(head==NULL)

printf("\n deletion not possible");

else

{
temp=head;

head=head->next;

printf("\n element deleted from the beginning of the list is %d:",temp->data);

free(temp);

void delete_end()

if(head==NULL)

printf("\n deletion not possible");

else

temp=head;

while(temp->next!=NULL)

prevnode=temp;

temp=temp->next;

if(temp==head)

head=NULL;

else

prevnode->next=NULL;

printf("\n element deleted from the end of the list is %d:",temp->data);

free(temp);

}
int delete_position()

int position,i;

if(head==NULL)

printf("\n deletion not possible(list is empty)");

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)

printf("There are less elements in the list");

return 0;

temp=temp->next;

nextnode=temp->next;

temp->next=nextnode->next;

printf("\n element deleted from the given position %d-> is %d:",position,nextnode->data);

free(nextnode);

return 0;
PROGRAM 12

Q. Write a C program to create, insert, delete, count and display elements in a doubly

link list. (consider all cases - at beg, end, at specific position)

#include<stdio.h>

#include<stdlib.h>

struct node

struct node *prev;

struct node *next;

int data;

};

struct node *head;

void insertion_beginning();

void insertion_last();

void insertion_specified();

void deletion_beginning();

void deletion_last();

void deletion_specified();

void display();

void search();

void main ()

int choice =0;

while(choice != 9)

printf("\n*********Main Menu*********\n");

printf("\nChoose one option from the following list ...\n");

printf("\n===============================================\n");

printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from


Beginning\n 5.Delete from last\n 6.Delete the node after the given data\n 7.Search\n 8.Show\n
9.Exit\n");
printf("\nEnter your choice?\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:

printf("Please enter valid choice..");

void insertion_beginning()

struct node *ptr;

int item;

ptr = (struct node *)malloc(sizeof(struct node));

if(ptr == NULL)

printf("\nOVERFLOW");

else

printf("\nEnter Item value");

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()

struct node *ptr,*temp;

int item;

ptr = (struct node *) malloc(sizeof(struct node));

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()

struct node *ptr,*temp;

int item,loc,i;

ptr = (struct node *)malloc(sizeof(struct node));

if(ptr == NULL)

printf("\n OVERFLOW");

else

temp=head;

printf("Enter the location");

scanf("%d",&loc);

for(i=0;i<loc;i++)

temp = temp->next;

if(temp == NULL)

printf("\n There are less than %d elements", loc);


return;

printf("Enter value");

scanf("%d",&item);

ptr->data = item;

ptr->next = temp->next;

ptr -> prev = temp;

temp->next = ptr;

temp->next->prev=ptr;

printf("\nnode inserted\n");

void deletion_beginning()

struct node *ptr;

if(head == NULL)

printf("\n UNDERFLOW");

else if(head->next == NULL)

head = NULL;

free(head);

printf("\nnode deleted\n");

else

ptr = head;

head = head -> next;

head -> prev = NULL;


free(ptr);

printf("\nnode deleted\n");

void deletion_last()

struct node *ptr;

if(head == NULL)

printf("\n UNDERFLOW");

else if(head->next == NULL)

head = NULL;

free(head);

printf("\nnode deleted\n");

else

ptr = head;

if(ptr->next != NULL)

ptr = ptr -> next;

ptr -> prev -> next = NULL;

free(ptr);

printf("\nnode deleted\n");

void deletion_specified()
{

struct node *ptr, *temp;

int val;

printf("\n Enter the data after which the node is to be deleted : ");

scanf("%d", &val);

ptr = head;

while(ptr -> data != val)

ptr = ptr -> next;

if(ptr -> next == NULL)

printf("\nCan't delete\n");

else if(ptr -> next -> next == NULL)

ptr ->next = NULL;

else

temp = ptr -> next;

ptr -> next = temp -> next;

temp -> next -> prev = ptr;

free(temp);

printf("\nnode deleted\n");

void display()

struct node *ptr;

printf("\n printing values...\n");

ptr = head;

while(ptr != NULL)
{

printf("%d\n",ptr->data);

ptr=ptr->next;

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("\nitem found at location %d ",i+1);

flag=0;

break;

else

flag=1;

i++;
ptr = ptr -> next;

if(flag==1)

printf("\nItem not found\n");

}
PROGRAM 13

Q. Write a C program to insert, delete, count and display elements in a circular link

list. (consider all cases - at beg, end, at a position)

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

};

struct node *head;

void beginsert ();

void lastinsert ();

void randominsert();

void begin_delete();

void last_delete();

void random_delete();

void display();

void search();

void main ()

int choice =0;

while(choice != 7)

printf("\n*********Main Menu*********\n");

printf("\nChoose one option from the following list ...\n");

printf("\n===============================================\n");

printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\n4.Delete from last\


n5.Search for an element\n6.Show\n7.Exit\n");
printf("\nEnter your choice?\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:

printf("Please enter valid choice..");

void beginsert()
{

struct node *ptr,*temp;

int item;

ptr = (struct node *)malloc(sizeof(struct node));

if(ptr == NULL)

printf("\nOVERFLOW");

else

printf("\nEnter the node data?");

scanf("%d",&item);

ptr -> data = item;

if(head == NULL)

head = ptr;

ptr -> next = head;

else

temp = head;

while(temp->next != head)

temp = temp->next;

ptr->next = head;

temp -> next = ptr;

head = ptr;

printf("\nnode inserted\n");

}
void lastinsert()

struct node *ptr,*temp;

int item;

ptr = (struct node *)malloc(sizeof(struct node));

if(ptr == NULL)

printf("\nOVERFLOW\n");

else

printf("\nEnter Data?");

scanf("%d",&item);

ptr->data = item;

if(head == NULL)

head = ptr;

ptr -> next = head;

else

temp = head;

while(temp -> next != head)

temp = temp -> next;

temp -> next = ptr;

ptr -> next = head;

printf("\nnode inserted\n");
}

void begin_delete()

struct node *ptr;

if(head == NULL)

printf("\nUNDERFLOW");

else if(head->next == head)

head = NULL;

free(head);

printf("\nnode deleted\n");

else

{ ptr = head;

while(ptr -> next != head)

ptr = ptr -> next;

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");

else if (head ->next == head)

head = NULL;

free(head);

printf("\nnode deleted\n");

else

ptr = head;

while(ptr ->next != head)

preptr=ptr;

ptr = ptr->next;

preptr->next = ptr -> next;

free(ptr);

printf("\nnode deleted\n");

void search()

struct node *ptr;

int item,i=0,flag=1;
ptr = head;

if(ptr == NULL)

printf("\nEmpty List\n");

else

printf("\nEnter item which you want to search?\n");

scanf("%d",&item);

if(head ->data == item)

printf("item found at location %d",i+1);

flag=0;

else

while (ptr->next != head)

if(ptr->data == item)

printf("item found at location %d ",i+1);

flag=0;

break;

else

flag=1;

i++;

ptr = ptr -> next;

}
}

if(flag != 0)

printf("Item not found\n");

void display()

struct node *ptr;

ptr=head;

if(head == NULL)

printf("\nnothing to print");

else

printf("\n printing values ... \n");

while(ptr -> next != head)

printf("%d\n", ptr -> data);

ptr = ptr -> next;

printf("%d\n", ptr -> data);

You might also like