C Ds Record Programs
C Ds Record Programs
ALGORITHM :
A is an array
N is number of elements (size)
Element is a data element Pos is the location of the element to be inserted. Insertion (A,
N, Element, Pos)
Step 1: for i = N-1 downto Pos repeat step 2
Step 2: A[i+1] = A[i] End for
Step 3: A [Pos] = Element
Step 4: N = N + 1
#include<stdio.h>
void main()
{
int i,p,n,s,a[10];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=n-1;i>=p-1;i--)
a[i+1]=a[i];
a[p]=s;
for(i=0;i<=n;i++)
printf("%3d",a[i]);
}
Output : enter range : 5
10
20
30
40
50
enter inserting value & position: 88
1
10 88 20 30 40 50
2. Delete an element from an Array
ALGORITHM :
A is an array
N is number of elements (size)
Element is a deleted data element
Pos is the location of the element to be deleted. Deletion (A, N, Pos)
Step 1: Element = A [Pos]
Step 2: for i = Pos to N-1 repeat
step 3 Step 3: A[ i ] = A[i+1] End for
Step 4: N = N -1
#include<stdio.h>
main()
{
int i,p,n,s,a[10];
for(i=p+1;i<=n;i++)
a[i-1]=a[i];
n=n-1;
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
output : enter range : 5
11
22
33
44
55
enter deleting value position: 1
11
33
44
55
3. STACK USING ARRAY :
ALGORITHM:
Step 1: Start
Step 3: Check if the stack is full or not by comparing top with (MAX-1)
If the stack is full, Then print "Stack Overflow" i.e, stack is full and cannot be
pushed with another element
Step 5: Stop
Step 1: Start
Step 4: Check if the stack is empty or not by comparing top with base of array i.e 0
If top is less than 0, then stack is empty, print "Stack Underflow"
Step 5: Else, If top is greater than zero the stack is not empty, then store the value pointed by
top in a variable x=a[top] and decrement top by 1. The popped element is x.
PROGRAM
include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
Before we implement actual operations, first follow the below steps to create an empty queue.
Step 1 - Include all the header files which are used in the program and define a
constant 'SIZE' with specific value.
Step 2 - Declare all the user defined functions which are used in queue implementation.
Step 3 - Create a one dimensional array with above defined SIZE (int queue[SIZE])
Step 4 - Define two integer variables 'front' and 'rear' and initialize both with '-1'. (int
front = -1, rear = -1)
Step 5 - Then implement main method by displaying menu of operations list and make
suitable function calls to perform operation selected by the user on queue.
void enQueue(int);
void deQueue();
void display();
void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void enQueue(int value){
if(rear == SIZE-1)
printf("\nQueue is Full!!! Insertion is not possible!!!");
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
}
}
void deQueue(){
if(front == rear)
printf("\nQueue is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", queue[front]);
front++;
if(front == rear)
front = rear = -1;
}
}
void display(){
if(rear == -1)
printf("\nQueue is Empty!!!");
else{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue[i]);
}
}
Insertion
Deletion
Display
Before we implement actual operations, first we need to set up an empty list. First, perform the
following steps before implementing actual operations.
Step 1 - Include all the header files which are used in the program.
Step 2 - Declare all the user defined functions.
Step 3 - Define a Node structure with two members data and next
Step 4 - Define a Node pointer 'head' and set it to NULL.
Step 5 - Implement the main method by displaying operations menu and make suitable
function calls in the main method to perform user selected operation.
Insertion
In a single linked list, the insertion operation can be performed in three ways. They are as
follows...
Step 1 - Create a newNode with given value and newNode → next as NULL.
Step 2 - Check whether list is Empty (head == NULL).
Step 3 - If it is Empty then, set head = newNode.
Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next is equal to NULL).
Step 6 - Set temp → next = newNode.
Deletion
In a single linked list, the deletion operation can be performed in three ways. They are as
follows...
PROGRAM :
#include<stdio.h>
#include <stdlib.h>
int main()
{
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *head, *first, *temp=0;
int count = 0;
int choice = 1;
first = 0;
while(choice)
{
head =(NODE*) malloc(sizeof(NODE));
printf("Enter the data item: ");
scanf("%d", &head-> num);
if(first != 0)
{
temp->ptr = head;temp = head;
}
else
{
first = temp = head;
}
fflush(stdin);
printf("Do you want to continue(Type 0 or )?\n\n");
scanf("%d", &choice);
}
temp->ptr = 0;
temp = first; /* reset temp to the beginning*/
printf("\n Status of the linked list is\n");
while(temp!=0)
{
printf("%d=>", temp->num);
count++;
temp = temp -> ptr;
}
printf("NULL\n");
printf("No. of nodes in the list = %d\n", count);
return 0;
}
1
Enter the data item: 20
Do you want to continue(Type 0 or 1)?
1
Enter the data item: 40
Do you want to continue(Type 0 or 1)?
0
Status of the linked list is
10=>20=>40=>NULL
No. of nodes in the list = 3
1. Push: It adds an item in the stack. If the stack is full, then the stack is said to be in
Overflow condition.
2. Pop: It deletes an item from the stack. The items are popped out in the reverse order in
which they were pushed in. If the stack is empty, then it is said to be in Underflow
condition i.e no more items can be deleted.
3. Peek or Top: It returns the top element of the stack.
4. isEmpty: It returns true if stack is empty, otherwise false.
1. PUSH() Operation:
Step 1: Start
2. POP() Operation:
Step 1: Start
Step 3: If top is not Null, assign the top's link part to ptr and assign ptr to stack_head's link
part.
Step 4: Stop
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("\n1.Push\n2.Pop\n3.Show\n4.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:
{
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()
{
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;
}
}
}
Step 1 - Include all the header files which are used in the program. And declare all
the user defined functions.
Step 2 - Define a 'Node' structure with two members data and next.
Step 3 - Define two Node pointers 'front' and 'rear' and set both to NULL.
Step 4 - Implement the main method by displaying Menu of list of operations and make
suitable function calls in the main method to perform user selected operation.
Step 1 - Create a newNode with given value and set 'newNode → next' to NULL.
Step 2 - Check whether queue is Empty (rear == NULL)
Step 3 - If it is Empty then, set front = newNode and rear = newNode.
Step 4 - If it is Not Empty then, set rear → next = newNode and rear = newNode.
#include<stdio.h>
#include<conio.h>
struct Node
{
int data;
struct Node *next;
}*front = NULL,*rear = NULL;
void insert(int);
void delete();
void display();
void main()
{
int choice, value;
clrscr();
printf("\n:: Queue Implementation using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
insert(value);
break;
case 2: delete(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void insert(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else{
rear -> next = newNode;
rear = newNode;
}
printf("\nInsertion is Success!!!\n");
}
void delete()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
struct Node *temp = front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp);
}
}
void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
struct Node *temp = front;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL\n",temp->data);
}
}
8. BUBBLE SORT :
ALGORITHM:
Start at index zero, compare the element with the next one (a[0] & a[1] (a is the name of the
array)), and swap if a[0] > a[1]. Now compare a[1] & a[2] and swap if a[1] > a[2]. Repeat this
process until the end of the array. After doing this, the largest element is present at the end. This
whole thing is known as a pass. In the first pass, we process array elements from [0,n-1].
1. Repeat step one but process array elements [0, n-2] because the last one, i.e., a[n-1], is
present at its correct position. After this step, the largest two elements are present at the
end.
2. Repeat this process n-1 times.
PROGRAM:
#include<stdio.h>
#define maxsize 20
int main()
{
int array[maxsize];
int i, j, num, t;
printf("How many numbers you want to enter : ");
scanf("%d", &num);
printf("\nEnter %d numbers :\n",num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("\nInput array is : \n\n");
for (i = 0; i < num; i++)
{
printf("%d ", array[i]);
}
/* Bubble sorting begins */
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
t = array[j];
array[j] = array[j + 1];
array[j + 1] = t;
}
}
} // end of sorting
printf("\n\nSorted array is : \n\n");
for (i = 0; i < num; i++)
{
printf("%d ", array[i]);
}
return 0;
}
OUTPUT : How many numbers you want to enter : 6
Enter 6 numbers :
23
2
44
10
50
5
Input array is :
23 2 44 10 50 5
Sorted array is :
2 5 10 23 44 50
Step 3 – swap the first location with the minimum value in the array
PROGRAM
#include<stdio.h>
int main(){
for(i=0;i<count;i++)
scanf("%d",&number[i]);
for(i=0;i<count;i++){
for(j=i+1;j<count;j++){
if(number[i]>number[j]){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}
return 0;
}
OUTPUT: How many numbers u are going to enter?:
5
Enter 5 elements
23
2
54
5
67
Sorted elements
2
5
23
54
67
ALGORITHM :
PROGRAM:
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
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]);
}
for (c = 1 ; c <= n - 1; c++)
{
d = c;
while ( d > 0 && array[d] < array[d-1])
{
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++)
{
printf("%d\n", array[c]);
}
return 0;
}
OUTPUT :
Enter number of elements
5
Enter 5 integers
23
2
54
5
67
Sorted list in ascending order:
2
5
23
54
67
10.QUICK SORT :
ALGORITHM;
Divide and conquer strategy. In quick sort, the partition of the list is performed based on the
element called pivot. Here pivot element is one of the elements in the list.
The list is divided into two partitions such that "all elements to the left of pivot are smaller than
the pivot and all elements to the right of pivot are greater than or equal to the pivot".
In Quick sort algorithm, partitioning of the list is performed using following steps...
Step 1 - Consider the first element of the list as pivot (i.e., Element at first position in the
list).
Step 2 - Define two variables i and j. Set i and j to first and last elements of the list
respectively.
Step 3 - Increment i until list[i] > pivot then stop.
Step 4 - Decrement j until list[j] < pivot then stop.
Step 5 - If i < j then exchange list[i] and list[j].
Step 6 - Repeat steps 3,4 & 5 until i > j.
Step 7 - Exchange the pivot element with list[j] element.
#include<stdio.h>
void quicksort(int x[20],int first,int last)
{
int pivot,j,temp,i;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j)
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
int main()
{
int x[20],size,i;
printf("\tQuick sort\n");
printf("-----------------------------------\n");
printf(" How many numbers you want to sort?: ");
scanf("%d",&size);
printf("\n Enter %d elements: \n",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("\n Sorted elements after applying quick sort: \n\n");
for(i=0;i<size;i++)
printf(" %d",x[i]);
return 0;
}
Enter 6 elements:
23
4
53
6
77
33
Sorted elements after applying quick sort:
3 6 23 33 53 77
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
OR
LINEAR_SEARCH(A, N, VAL)
SET POS = I
PRINT POS
Go to Step 6
[END OF IF]
SET I = I + 1
[END OF LOOP]
Step 5: IF POS = –1
[END OF IF]
Step 6: EXIT
return 0;
}
OUTPUT :
Step 2: Repeat Steps 3 and 4 while BEG <= END Step 3: SET MID = (BEG + END)/2 Step 4:
IF A[MID] = VAL SET POS = MID PRINT POS Go to Step 6 ELSE
IF A[MID] > VAL
ELSE
[END OF IF]
[END OF LOOP]
Step 5: IF POS = -1
[END OF IF]
Step 6: EXIT
PROGRAM :
#include<stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100],t,i,j;
printf(" How many numbers you want to enter?: ");
scanf("%d",&n);
printf("\n Enter %d numbers: \n\n ", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
//buuble sort starts
for (i = 0; i < n; i++)
{
for (j = 0; j < (n - i - 1); j++)
{
if (array[j] > array[j + 1])
{
t = array[j];
array[j] = array[j + 1];
array[j + 1] = t;
}
}
}
printf("\nSorted array is : \n\n");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\n\nEnter number to be searched: ");
scanf("%d", &search);
// Binary search begins
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("\n%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("\nNot found!! %d is not present in the list.\n", search);
return 0;
}
OUTPUT:
How many numbers you want to enter?: 6
Enter 6 numbers:
10
15
20
25
30
35
Sorted array is :
10 15 20 25 30 35
Enter number to be searched: 30
30 found at location 5.