You are on page 1of 11

printf("Enter the location where you wish to

C program to insert an element in an array delete element\n");


scanf("%d", &position);
#include <stdio.h>
if (position >= n+1)
printf("Deletion not possible.\n");
int main()
else
{
{
int array[100], position, c, n, value;
for (c = position - 1; c < n - 1; c++)
array[c] = array[c+1];
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Resultant array:\n");
printf("Enter %d elements\n", n);
for (c = 0; c < n - 1; c++)
printf("%d\n", array[c]);
for (c = 0; c < n; c++)
}
scanf("%d", &array[c]);
return 0;
printf("Enter the location where you wish to
}
insert an element\n");
scanf("%d", &position);
Linear search in C
printf("Enter the value to insert\n");
scanf("%d", &value); #include <stdio.h>

for (c = n - 1; c >= position - 1; c--) int main()


array[c+1] = array[c]; {
int array[100], search, c, n;
array[position-1] = value;
printf("Enter number of elements in array\n");
printf("Resultant array is\n"); scanf("%d", &n);

for (c = 0; c <= n; c++) printf("Enter %d integer(s)\n", n);


printf("%d\n", array[c]);
for (c = 0; c < n; c++)
return 0; scanf("%d", &array[c]);
}
C program to delete an element from an array printf("Enter a number to search\n");
scanf("%d", &search);
#include <stdio.h>
for (c = 0; c < n; c++)
int main() {
{ if (array[c] == search) /* If required element
int array[100], position, c, n; is found */
{
printf("Enter number of elements in array\n"); printf("%d is present at location %d.\n",
scanf("%d", &n); search, c+1);
break;
printf("Enter %d elements\n", n); }
}
for (c = 0; c < n; c++) if (c == n)
scanf("%d", &array[c]); printf("%d isn't present in the array.\n",
search);
return 0; int array[100], n, c, d, swap;
}
printf("Enter number of elements\n");
C program for binary search scanf("%d", &n);

#include <stdio.h> printf("Enter %d integers\n", n);

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


{ scanf("%d", &array[c]);
int c, first, last, middle, n, search, array[100];
for (c = 0 ; c < n - 1; c++)
printf("Enter number of elements\n"); {
scanf("%d",&n); for (d = 0 ; d < n - c - 1; d++)
{
printf("Enter %d integers\n", n); if (array[d] > array[d+1]) /* For decreasing
order use < */
for (c = 0; c < n; c++) {
scanf("%d",&array[c]); swap = array[d];
array[d] = array[d+1];
printf("Enter value to find\n"); array[d+1] = swap;
scanf("%d", &search); }
}
first = 0; }
last = n - 1;
middle = (first+last)/2; printf("Sorted list in ascending order:\n");

while (first <= last) { for (c = 0; c < n; c++)


if (array[middle] < search) printf("%d\n", array[c]);
first = middle + 1;
else if (array[middle] == search) { return 0;
printf("%d found at location %d.\n", }
search, middle+1); Selection sort algorithm implementation in C
break;
} #include <stdio.h>
else
last = middle - 1; int main()
{
middle = (first + last)/2; int array[100], n, c, d, position, swap;
}
if (first > last) printf("Enter number of elements\n");
printf("Not found! %d isn't present in the scanf("%d", &n);
list.\n", search);
printf("Enter %d integers\n", n);
return 0;
} for (c = 0; c < n; c++)
scanf("%d", &array[c]);

Bubble sort algorithm implementation in C for (c = 0; c < (n - 1); c++)


{
#include <stdio.h> position = c;

int main() for (d = c + 1; d < n; d++)


{ {
if (array[position] > array[d]) return 0;
position = d; }
}
if (position != c) C program to implement stack data structure
{
swap = array[c]; #include <stdio.h>
array[c] = array[position]; #include <stdlib.h>
array[position] = swap;
} int stack[100];
} void push();
printf("Sorted list in ascending order:\n"); int pop();
void traverse();
for (c = 0; c < n; c++) int is_empty();
printf("%d\n", array[c]); int top_element();
int top = 0;
return 0;
} int main()
{
int element, choice;
Insertion sort algorithm implementation in C
for (;;)
#include <stdio.h> {
printf("Stack Operations.\n");
int main() printf("1. Insert into stack (Push
{ operation).\n");
int n, array[1000], c, d, t; printf("2. Delete from stack (Pop
operation).\n");
printf("Enter number of elements\n"); printf("3. Print top element of stack.\n");
scanf("%d", &n); printf("4. Check if stack is empty.\n");
printf("5. Traverse stack.\n");
printf("Enter %d integers\n", n); printf("6. Exit.\n");
printf("Enter your choice.\n");
for (c = 0; c < n; c++) scanf("%d",&choice);
scanf("%d", &array[c]);
switch (choice)
for (c = 1 ; c <= n - 1; c++) { {
d = c; case 1:
if (top == 5)
while ( d > 0 && array[d-1] > array[d]) { printf("Error: Overflow\n\n");
t = array[d]; else {
array[d] = array[d-1]; printf("Enter a value to insert.\n");
array[d-1] = t; scanf("%d", &element);
push(element);
d--; }
} break;
}
case 2:
printf("Sorted list in ascending order:\n"); if (top == 0)
printf("Error: Underflow.\n\n");
for (c = 0; c <= n - 1; c++) { else {
printf("%d\n", array[c]); element = pop();
} printf("Element removed from the stack is
%d.\n", element);
} printf("\n");
break; }

case 3: int is_empty() {


if (!is_empty()) { if (top == 0)
element = top_element(); return 1;
printf("Element at the top of the stack is else
%d\n\n", element); return 0;
} }
else
printf("The stack is empty.\n\n"); int top_element() {
break; return stack[top-1];
}
case 4:
if (is_empty()) C Program to Implement a Queue using an
printf("The stack is empty.\n\n"); Array
else
printf("The stack isn't empty.\n\n"); #include <stdio.h>
break; #define MAX 50
void insert();
case 5: void delete();
traverse(); void display();
break; int queue_array[MAX];
int rear = - 1;
case 6: int front = - 1;
exit(0); main()
} {
} int choice;
} while (1)
{
void push(int value) { printf("1.Insert element to queue \n");
stack[top] = value; printf("2.Delete element from queue \n");
top++; printf("3.Display all elements of queue \n");
} printf("4.Quit \n");
printf("Enter your choice : ");
int pop() { scanf("%d", &choice);
top--; switch (choice)
return stack[top]; {
} case 1:
insert();
void traverse() { break;
int d; case 2:
delete();
if (top == 0) { break;
printf("The stack is empty.\n\n"); case 3:
return; display();
} break;
case 4:
printf("There are %d elements in the stack.\n", exit(1);
top); default:
printf("Wrong choice \n");
for (d = top - 1; d >= 0; d--) } /* End of switch */
printf("%d\n", stack[d]); } /* End of while */
} /* End of main() */ IN A CIRCULARLY QUEUE */
#include<stdio.h>
void insert() #include<malloc.h>
{ #include<stdio.h>
int add_item; #include<conio.h>
if (rear == MAX - 1)
printf("Queue Overflow \n"); struct que
else {
{ int info;
if (front == - 1) struct que *next;
/*If queue is initially empty */ };
front = 0; void inqueue(struct que**,struct que**,int);
printf("Inset the element in queue : "); void delqueue(struct que**,struct que**);
scanf("%d", &add_item); void display(struct que*,struct que*);
rear = rear + 1;
queue_array[rear] = add_item; /* main function */
} void main(void)
} /* End of insert() */ {
struct que *front, *rear;
void delete() int choice, item;
{ clrscr();
if (front == - 1 || front > rear) front = NULL;
{ rear = NULL;
printf("Queue Underflow \n"); do
return ; {
} do
else {
{ printf("\n\n\n\t MAIN MENU FOR CIRCULAR
printf("Element deleted from queue is : QUEUE OPERATIONS");
%d\n", queue_array[front]); printf("\n\t 1. Insert in Queue ");
front = front + 1; printf("\n\t 2. Delete from Queue ");
} printf("\n\t 3. Display the Queue ");
} /* End of delete() */ printf("\n\t 4. Exit ");
printf("\n\t Enter choice : ");
void display() scanf(" %d",&choice);
{ if(choice<1 || choice>4)
int i; printf("\n Invalid choice - try again");
if (front == - 1) }while (choice<1 || choice>4);
printf("Queue is empty \n"); switch(choice)
else {
{ case 1:
printf("Queue is : \n"); printf("\n Enter new element: ");
for (i = front; i <= rear; i++) scanf("%d", &item);
printf("%d ", queue_array[i]); inqueue(&front,&rear,item);
printf("\n"); //printf("\n\tQueue after insertion is : ");
} display(front,rear);
} /* End of display() */ break;
case 2:
delqueue(&front,&rear);
display(front,rear);
Insertion and Deletion into a Circular Queue break;
/* C LANGUAGE PROGRAM TO PERFORM case 3:
INSERTION AND DELETION OPERATIONS display(front,rear);
break; // printf("\n save->info = %d,front->info=
default: %d,rear->info=%d",save->info,(*front)->info,
printf("\n End of program "); (*rear)->info);
} /* end of switch */ free(save);
}while(choice !=4); // printf("\n After deletion save->info =
return; %d,front->info=%d,rear->info=%d",save->info,
} (*front)->info,(*rear)->info);
}
/*end of main function */ }

/*function to insert elements into queue */ /*function to display queue elements */


void inqueue(struct que **front,struct que void display(struct que *front,struct que *rear)
**rear,int x) {
{ printf ("\n Now queue is : ");
struct que *nw; if (front==NULL)
nw=(struct que*)malloc(sizeof(struct que)); printf(" Queue is empty, nothing to display");
nw->info = x; else
// new->next = NULL; {
if(front==rear)
if((*front)==NULL) printf(" ->%d",front->info);
(*front)=nw; else
else {
{ while(front != rear)
(*rear)->next=nw; {
} printf(" ->%d",front->info);
(*rear)=nw; front=front->next;
(*rear)->next=*front; }
// printf("\n Now front= %d and rear->next = printf(" ->%d",rear->info);
%d",(*front)->info,(*rear)->next->info); }
} }
} /*end of display */
/*function to delete element from queue */ /* END OF PROGRAM */
void delqueue(struct que **front,struct que
**rear) C program to implement linked list
{ #include <stdio.h>
struct que *save; #include <stdlib.h>
if((*front)==NULL)
printf("\n Queue is already empty, cannot struct node {
delete"); int data;
else if ((*front)== (*rear)) struct node *next;
{ };
save=(*front); struct node *start = NULL;
(*front)=NULL; void insert_at_begin(int);
(*rear)=NULL; void insert_at_end(int);
printf("\n\t After deleting this node now void traverse();
queue has become empty\n"); void delete_from_begin();
} void delete_from_end();
else int count = 0;
{
save = *front; int main () {
(*front)=(*front)->next; int input, data;
(*rear)->next=*front;
for (;;) {
printf("1. Insert an element at beginning of }
linked list.\n");
printf("2. Insert an element at end of linked void insert_at_end(int x) {
list.\n"); struct node *t, *temp;
printf("3. Traverse linked list.\n");
printf("4. Delete element from t = (struct node*)malloc(sizeof(struct node));
beginning.\n"); count++;
printf("5. Delete element from end.\n");
printf("6. Exit\n"); if (start == NULL) {
start = t;
scanf("%d", &input); start->data = x;
start->next = NULL;
if (input == 1) { return;
printf("Enter value of element\n"); }
scanf("%d", &data);
insert_at_begin(data); temp = start;
}
else if (input == 2) { while (temp->next != NULL)
printf("Enter value of element\n"); temp = temp->next;
scanf("%d", &data);
insert_at_end(data); temp->next = t;
} t->data = x;
else if (input == 3) t->next = NULL;
traverse(); }
else if (input == 4)
delete_from_begin(); void traverse() {
else if (input == 5) struct node *t;
delete_from_end();
else if (input == 6) t = start;
break;
else if (t == NULL) {
printf("Please enter valid input.\n"); printf("Linked list is empty.\n");
} return;
}
return 0;
} printf("There are %d elements in linked
list.\n", count);
void insert_at_begin(int x) {
struct node *t; while (t->next != NULL) {
printf("%d\n", t->data);
t = (struct node*)malloc(sizeof(struct node)); t = t->next;
count++; }
printf("%d\n", t->data);
if (start == NULL) { }
start = t;
start->data = x; void delete_from_begin() {
start->next = NULL; struct node *t;
return; int n;
}
if (start == NULL) {
t->data = x; printf("Linked list is already empty.\n");
t->next = start; return;
start = t; }
n = start->data; struct node {
t = start->next; int data;
free(start); int key;
start = t;
count--; struct node *next;
};
printf("%d deleted from beginning
successfully.\n", n); struct node *head = NULL;
} struct node *current = NULL;

void delete_from_end() { bool isEmpty() {


struct node *t, *u; return head == NULL;
int n; }

if (start == NULL) { int length() {


printf("Linked list is already empty.\n"); int length = 0;
return;
} //if list is empty
if(head == NULL) {
count--; return 0;
}
if (start->next == NULL) {
n = start->data; current = head->next;
free(start);
start = NULL; while(current != head) {
printf("%d deleted from end successfully.\n", length++;
n); current = current->next;
return; }
}
return length;
t = start; }

while (t->next != NULL) { //insert link at the first location


u = t; void insertFirst(int key, int data) {
t = t->next;
} //create a link
struct node *link = (struct node*)
n = t->data; malloc(sizeof(struct node));
u->next = NULL; link->key = key;
free(t); link->data = data;

printf("%d deleted from end successfully.\n", if (isEmpty()) {


n); head = link;
} head->next = head;
} else {
//point it to old first node
Circular Linked List Program in C link->next = head;

#include <stdio.h> //point first to new first node


#include <string.h> head = link;
#include <stdlib.h> }
#include <stdbool.h> }
printf("(%d,%d) ",temp->key,temp->data);
//delete first item }
struct node * deleteFirst() {
printf("\nList after deleting all items: ");
//save reference to first link printList();
struct node *tempLink = head; }

if(head->next == head) {
head = NULL; Binary search tree
return tempLink;
} #include <stdio.h>
#include <stdlib.h>
//mark next to first link as first
head = head->next; struct node
{
//return the deleted link int data; //node will store an integer
return tempLink; struct node *right_child; // right child
} struct node *left_child; // left child
};
//display the list
void printList() { struct node* search(struct node *root, int x)
{
struct node *ptr = head; if(root==NULL || root->data==x) //if root-
printf("\n[ "); >data is x then the element is found
return root;
//start from the beginning else if(x>root->data) // x is greater, so we will
if(head != NULL) { search the right subtree
return search(root->right_child, x);
while(ptr->next != ptr) { else //x is smaller than the data, so we will
printf("(%d,%d) ",ptr->key,ptr->data); search the left subtree
ptr = ptr->next; return search(root->left_child,x);
} }
}
//function to find the minimum value in a node
printf(" ]"); struct node* find_minimum(struct node *root)
} {
if(root == NULL)
void main() { return NULL;
insertFirst(1,10); else if(root->left_child != NULL) // node with
insertFirst(2,20); minimum value will have no left child
insertFirst(3,30); return find_minimum(root->left_child); //
insertFirst(4,1); left most element will be minimum
insertFirst(5,40); return root;
insertFirst(6,56); }

printf("Original List: "); //function to create a node


struct node* new_node(int x)
//print list {
printList(); struct node *p;
p = malloc(sizeof(struct node));
while(!isEmpty()) { p->data = x;
struct node *temp = deleteFirst(); p->left_child = NULL;
printf("\nDeleted value:"); p->right_child = NULL;
else
return p; {
} struct node *temp = find_minimum(root-
>right_child);
struct node* insert(struct node *root, int x) root->data = temp->data;
{ root->right_child = delete(root-
//searching for the place to insert >right_child, temp->data);
if(root==NULL) }
return new_node(x); }
else if(x>root->data) // x is greater. Should be return root;
inserted to right }
root->right_child = insert(root->right_child,
x); void inorder(struct node *root)
else // x is smaller should be inserted to left {
root->left_child = insert(root->left_child,x); if(root!=NULL) // checking if the root is not
return root; null
} {
inorder(root->left_child); // visiting left
// funnction to delete a node child
struct node* delete(struct node *root, int x) printf(" %d ", root->data); // printing data at
{ root
//searching for the item to be deleted inorder(root->right_child);// visiting right
if(root==NULL) child
return NULL; }
if (x>root->data) }
root->right_child = delete(root->right_child,
x); int main()
else if(x<root->data) {
root->left_child = delete(root->left_child, x); struct node *root;
else root = new_node(20);
{ insert(root,5);
//No Children insert(root,1);
if(root->left_child==NULL && root- insert(root,15);
>right_child==NULL) insert(root,9);
{ insert(root,7);
free(root); insert(root,12);
return NULL; insert(root,30);
} insert(root,25);
insert(root,40);
//One Child insert(root, 45);
else if(root->left_child==NULL || root- insert(root, 42);
>right_child==NULL)
{ inorder(root);
struct node *temp; printf("\n");
if(root->left_child==NULL)
temp = root->right_child; root = delete(root, 1);
else
temp = root->left_child; root = delete(root, 40);
free(root); root = delete(root, 45);
return temp; root = delete(root, 9);
} inorder(root);
printf("\n");
//Two Children
return 0;
}

You might also like