0% found this document useful (0 votes)
3 views5 pages

Programs

The document contains several C programming examples demonstrating data structures and algorithms, including a linked list with operations for insertion and deletion, a stack implemented using an array, a queue using a linked list, linear search, and selection sort. Each section provides a menu-driven interface for user interaction and showcases basic operations for each data structure or algorithm. The code snippets illustrate fundamental programming concepts such as memory management, control structures, and user input handling.

Uploaded by

darkash10710
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Programs

The document contains several C programming examples demonstrating data structures and algorithms, including a linked list with operations for insertion and deletion, a stack implemented using an array, a queue using a linked list, linear search, and selection sort. Each section provides a menu-driven interface for user interaction and showcases basic operations for each data structure or algorithm. The code snippets illustrate fundamental programming concepts such as memory management, control structures, and user input handling.

Uploaded by

darkash10710
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <stdio.

h>
#include <stdlib.h>
(Menu driven)
struct Node{int data;struct Node*next;} *head NULL;

void insertBeg() {
struct Node*n=malloc(sizeof(struct Node));
printf("Enter value: ");scanf("%d" ,&n->data);
n->next=head;head=n;
}

void insertEnd() {
struct Node*n=malloc(sizeof(struct Node));
printf("Enter value: ");scanf("%d",&n->data);
n->next=NULL;
if( !head)head=n;
else {struct N ode*t=head;while(t->next)t=t->next;t->next=n;}
}

void deleteBeg() {
if(!head)printf("Empty\n");
else {struct N ode*t=head;head=head->next;free(t); }
}

void deleteEnd() {
if(! head)printf("Empty\n");
else if( !head->next) {free(head);head NULL;}
else {struct Node*t=head;while(t->next->next)t=t->next;free(t->next);t->next NULL;}
}

void display() {
if(!head)printf("Empty\n");
else {struct Node*t=head;while(t) {printf("%d -> ",t->data);t=t->next;}printf("NULL\n");}
}

int main(){
int ch;
while(l){
printf("\[Link] [Link] [Link] [Link] [Link] [Link]\nChoice: ");
scanf("%d" ,&ch);
switch(ch){
case 1:insertBeg();break;
case 2: insertEnd();break;
case 3:deleteBeg();break;
case 4:deleteEnd();break;
case 5:display();break;
case 6:exit(0);
default:printf("Invalid\n");
}
}
}
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
Stack using array
int stack[MAX], top = -1;

void push() {
int data;
if (top== MAX - 1) printf("Stack Overflow!\n");
else {
printf("Enter data to push: ");
scanf("%d", &data);
stack[++top] = data;
}
}

void pop() {
if (top== -1) printf("Stack Underflow!\n");
else printf("%d deleted\n", stack[top--]);
}

void display() {
if (top== -1) printf("Stack is empty\n");
else {
printf("Elements in stack:\n");
for (int i = top; i >= 0; i--) printf("%d\n", stack[i]);
}
}

int main() {
int ch;
printf("Stack using array\n");
while (1) {
printf("\nl .Push [Link] [Link] [Link]\nEnter choice: ");
scanf("%d", &ch);
switch (ch) {
case 1: push(); break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("Invalid choice\n");
}
}
}
Q u e u e using linked list

#include <stdio.h>
#include <stdlib.h>
;} *front=NULL, *rear=NULL;
struct node {int data;struct node*next
void enqueue() {
node));
struct node*n=malloc(sizeof(struct ULL;
printf("Enter element to enqueue: ear");scanf("%d",&n->data);n->next=N
->next=n;
if(!front) front=rear=n; else rear=r
}

void dequeue() {
);
if(!front) printf("Queue is empty\n" a);front=front->next; if(! front) rear=NULL; free(t);}
¾d deleted\n ", t->dat
else {struct node*t=front;printf("
}

void display() {
if(!front) printf("Queue is empty
\n"); ");}
eue elements: \n ");while(t) {printf("%d ",t->data);t=t->next;} printf("\n
else {struct node*t=front;printf("
Qu
}

int main(){
int ch;
printf("Queue using linked list\n");
h · e: ");scani~t"o/cd" ,&ch)·,
{
(l) nl .Enqueue [Link] [Link] [Link]\nEnter c 01c \ o
whilentf ("\
pri
switch(ch){
case l : enqueue(); break;
case 2: dequeue(); break;
case 3: display(); break;
case 4: exit(O);
de fau lt printf("Invaiid choice\n");
}
}
}
Linear search

#include <stdio.h>

int main() {
int a[lO], n, num, flag=O, count=O;
printf("Linear search\nNumber of elements: ");
scanf("¾d", &n);
printf("Enter the elements: ");
for(int i=O; i<n; i++) scanf("¾d", &a[i]);

printf("Enter the number to search: ");


scanf("¾d", &num);

for(int i=O; i<n; i++) {


if(a[i]==num) {
printf("The element is found at index ¾d\n", i);
flag=l '• count++·'
}
}

if(!flag) printf("The element is not found\n");


else printf("The number of occurrence of ¾dis ¾d\n", num, count);

return O;
}
Selection sort
#include <stdio.h>
#include <stdlib.h>

int main() {
float arr[l00], temp;
int i, j, elements, min;

printf("How many elements in array (1-100): ");


scanf("%d", &elements);
if(elements<l II elements>l00){
printf("Element should be within (1-100)\n");
exit(l );
}

for(i=0; i<elements; i++){


printf("arr[%d] = ", i);
scanf("%f'', &arr[i]);
}

printf("Existing list: \n ");


for(i=0; i<elements; i++) printf("arr[%d] = %.2f\n", i, arr[i]);

// Selection Sort
for(i=0; i<elements-1; i++){
. .
min= 1·
for(j=i+' 1; j<elements; j++)
if(arr[j] < arr[min]) min= j;
if(min != i){ temp= arr[i]; arr[i] = arr[min]; arr[min] = temp; }
}

printf("Sorted list:\n");
for(i=0; i<elements; i++) printf("arr[%d] = %.2f\n", i, arr[i]);

return 0;
}

You might also like