Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
163 views8 pages

PRGM

The document contains code snippets for common data structures and algorithms including arrays, stacks, queues, linear search, and binary search. The code shows how to implement insertion and deletion of elements in arrays, as well as push, pop and traversal operations for stacks. For queues, the code demonstrates insertion, deletion and display functions. Linear and binary search algorithms are also included to search for an element within an array.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views8 pages

PRGM

The document contains code snippets for common data structures and algorithms including arrays, stacks, queues, linear search, and binary search. The code shows how to implement insertion and deletion of elements in arrays, as well as push, pop and traversal operations for stacks. For queues, the code demonstrates insertion, deletion and display functions. Linear and binary search algorithms are also included to search for an element within an array.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1).INSERTION #include <stdio.

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

for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Enter the location where you wish to delete element\n"); scanf("%d", &position); if ( position >= n+1 ) printf("Deletion not possible.\n"); else { for ( c = position - 1 ; c < n - 1 ; c++ ) array[c] = array[c+1]; printf("Resultant array is\n"); for( c = 0 ; c < n - 1 ; c++ ) printf("%d\n", array[c]); } return 0; } 3).LINEAR SEARCH #include<stdio.h> #include<conio.h> void main() { int array[10],n,i,find; clrscr(); printf("\n Enter the no. of elements in array: "); scanf("%d",&n); printf("\n Enter the elements,\n\n"); for(i=0;i<n;i++) { scanf("%d",&array[i]); } printf("\n Enter element to find: "); scanf("%d",&find);

for(i=0;i<n;i++) { if(array[i]==find) { printf("\n Element is present at position %d",i+1); break; } } if(i==n) printf("\n element not found"); } 4). BINARY SEARCH #include<stdio.h> #include<conio.h> main() { int c, first, last, middle, n, search, array[100]; printf("Binary Search Program in C\n\n"); printf("Enter the number of values\n"); scanf("%d",&n); printf("Enter the values in ascending order\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d",&array[c]); printf("Enter the search value\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 is not present in the list.\n", search); getch(); return 0; } 5).STACK

#include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAXSIZE 10 void push(); int pop(); void traverse(); int stack[MAXSIZE]; int Top=-1; void main() { int choice; char ch; while(1) { printf("\n1. PUSH "); printf("\n2. POP "); printf("\n3. TRAVERSE "); printf("\nEnter your choice"); scanf("%d",&choice); switch(choice) { case 1: push(); break; case 2: printf("\nThe deleted element is %d",pop()); break; case 3: traverse(); break;

case 4: exit(0); default: printf("\nYou Entered Wrong Choice"); } } } void push() { int item; if(Top == MAXSIZE - 1) { printf("\nThe Stack Is Full"); exit(0); } else { printf("Enter the element to be inserted"); scanf("%d",&item); Top= Top+1; stack[Top] = item; } } int pop() { int item; if(Top == -1) { printf("The stack is Empty"); exit(0); } else { item = stack[Top]; Top = Top-1; } return(item); } void traverse()

{ int i; if(Top == -1) { printf("The Stack is Empty"); exit(0); } else { for(i=Top;i>=0;i--) { printf("\n%d",stack[i]); } } } 6).QUEUE #include<stdio.h> #include<stdlib.h> #define MAX 50 int queue_arr[MAX]; void insert(); void del(); void display(); int rear = - 1; int front = - 1; void main() { int choice; while (1) { printf("1.Insert\n"); printf("2.Delete\n"); printf("2.Display\n"); printf("4.Quit\n"); printf("Enter your choice : "); scanf("%d", &choice); switch (choice) { case 1: insert(); break; case 2:

del(); break; case 3: display(); break; case 4: exit(1); default: printf("Wrong choice\n"); } /*End of switch*/ } /*End of while*/ } /*End of main()*/ void insert() { int added_item; if (rear == MAX - 1) printf("Queue Overflow\n"); else { if (front == - 1) /*If queue is initially empty */ front = 0; printf("Input the element for adding in queue : "); scanf("%d", &added_item); rear = rear + 1; queue_arr[rear] = added_item; } } /*End of insert()*/ void del() { if (front == - 1 || front > rear) { printf("Queue Underflow\n"); exit(0); } else { printf("Element deleted from queue is : %d\n", queue_arr[front]); front = front + 1; } } /*End of del() */ void display() { int i; if (front == - 1) printf("Queue is empty\n");

else { printf("Queue is :\n"); for (i = front; i <= rear; i++) printf("%d ", queue_arr[i]); printf("\n"); } }

You might also like