0% found this document useful (0 votes)
15 views25 pages

2nd Sem Final PDF

The document contains multiple C programs demonstrating various data structures and algorithms, including student information management, stack, queue (linear and circular), binary search, singly linked list, and doubly linked list implementations. Each section provides code for initializing, inserting, deleting, and displaying elements, along with user interaction for executing operations. The programs are designed to illustrate fundamental concepts in data structures and their operations.

Uploaded by

balarammuni17
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)
15 views25 pages

2nd Sem Final PDF

The document contains multiple C programs demonstrating various data structures and algorithms, including student information management, stack, queue (linear and circular), binary search, singly linked list, and doubly linked list implementations. Each section provides code for initializing, inserting, deleting, and displaying elements, along with user interaction for executing operations. The programs are designed to illustrate fundamental concepts in data structures and their operations.

Uploaded by

balarammuni17
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
You are on page 1/ 25

// Write a program to read the n No of students information NORMAL

#include<stdio.h>
#include <stdlib.h>

typedef struct student


{
char name[30];
int marks,roll;
}student;

int main()
{
int n,i;
student s[50];
printf("enter the number of students : ");
scanf("%d",&n);
printf("enter the details of students \nNAME\nRoll\nMarks: \n");

for(i=0;i<n;i++)
{
scanf("%s %d %d",s[i].name,&s[i].roll,&s[i].marks);
}
printf("THE DATA OF %d STUDENTS ARE:- ",n);
for(i=0;i<n;i++)
{
printf("\n%s\n%d\n%d\n",s[i].name,s[i].roll,s[i].marks);
}

return 0;
}
// 1) ARRAY IMPLEMENTATION OF STACK.

#include<stdio.h>
#include<stdlib.h>
#define size 5
typedef struct stack
{
int array[size];
int top;
}stack;

void initstack(stack *ptr)


{
ptr->top= -1;
printf("stack is initialized\n");
return;
}

void push(stack *ptr, int data)


{
if(ptr->top == (size-1))
{
printf("\n Stack overflow (Stack is full) \n");
}
else
{
ptr->top++;
ptr->array[ptr->top]=data;
}
return;
}

int pop(stack *ptr)


{
int data;
if(ptr->top == -1)
{
printf("Stack underflow.\n");
return -1;
}
else
{
data=ptr->array[ptr->top];
ptr->top--;
}
return data;
}

int peep(stack *ptr)


{
int x;
if(ptr->top == -1)
{
printf("Stack underflow\n");
return -1;
}
else
{
x= ptr->array[ptr->top];
return(x);
}
}

int main()
{
stack s;
int choice,element;
initstack(&s);
while(1)
{
printf("1: PUSH \n");
printf("2: POP \n");
printf("3: PEEP \n");
printf("4: EXIT \n");
printf("Enter your choice [1 - 4]: \n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n Enter the element to push onto stack \n");
scanf("%d",&element);
push(&s,element);
break;
case 2: element = pop(&s);
if(element != -1)
printf("\n The popped element is = %d \n", element);
break;
case 3: element = peep(&s);
printf("The element on the top is = %d \n", element);
break;
case 4: printf("Exit from the program\n");
exit(0);
default: printf("Enter choice :(1-4):\n");
}
}
return 0;
}
// Linear Queue

#include<stdio.h>
#include<stdlib.h>
#define max 5

typedef struct queue


{
int rear,front;
int arr[max];
} queue;

void init(queue *q)


{
q->rear=-1;
q->front=0;
}

int isFull(queue *q)


{
if(q->rear==max-1)
return 1;
else
return 0;
}

int isEmpty(queue *q)


{
if(q->front>q->rear)
return 1;
else
return 0;
}
void insert(queue *q,int ele)
{
if(isFull(q))
{
printf("Queue is full\n");
exit(0);
}
else
{
q->rear++;
q->arr[q->rear]=ele;
}
}

int delete(queue *q)


{
int ele;
if(isEmpty(q))
{
printf("Queue is empty\n");
return -1;
}
else
{
ele=q->arr[q->front];
q->front++;
return ele;
}
}

void display(queue *q)


{
int i;
if(isEmpty(q))
{
printf("Queue is empty\n");
}
else
{
for(i=q->front;i<=q->rear;i++)
{
printf("%d\t",q->arr[i]);
}
}
}
int count(queue *q)
{
int count=0,i;
for(i=q->front;i<=q->rear;i++)
{
count++;
}
return count;
}

int main()
{
int ch,element,number=0;
queue q;
init(&q);
while(1)
{
printf("\n1.Insert an element\n2.Delete an element\n3.Display an
elements\n4.Count elements\n5.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the element to insert\n");
scanf("%d",&element);
insert(&q,element);
break;
case 2: element=delete(&q);
printf("Deleted element=%d",element);
break;
case 3: display(&q);
break;
case 4: number=count(&q);
printf("Number of elements in queue=%d\n",number);
break;
case 5: exit(0);
default:printf("\nWrong choice!!!!!!\n");
break;
}
}
return 0;
}
// Menu based implementation of Circular - QUEUE

#include<stdio.h>
#include<stdlib.h>
#define max 5
typedef struct queue
{
int rear,front;
int arr[max];
} queue;

void init(queue *q)


{
q->rear=-1;
q->front= -1;
}

void insert(queue *q,int ele)


{
if (q->front==((q->rear +1 )% max))
printf("Queue is full\n");
else
{
if(q->front== -1)
{
q->front=q->rear=0;
}
else
{
q->rear = (q->rear+1)%max;
}
q->arr[q->rear]=ele;
}
}

int delete(queue *q)


{
int val;
if (q->front == -1)
{
printf("Queue underflow \n");
return -1;
}
else
{
val= q->arr[q->front];
if(q->front == q->rear )
{
q->front = q->rear = -1;
}
else
{
q->front = (q->front+1)%max;
}
}
return val;
}

void display(queue *q)


{
int i;
if (q->front == -1)
{
printf("Queue underflow \n");
return;
}
i=q->front;
if(q->front < q->rear)
{
for(i=q->front ; i<=q->rear; i++)
printf("\t %d",q->arr[i]);
}
else if(q->front == q->rear)
{
printf("%d\t",q->arr[q->front]);
}
else
{
for(i=0; i<=q->rear; i++)
printf("\t %d",q->arr[i]);
for(i=q->front; i< max; i++)
printf("\t %d",q->arr[i]);
}
}
int count(queue *q)
{
int count=0,i;
if(q->rear > q->front)
count=q->rear - q->front+1;
else if(q->rear < q->front)
{
count=(max- q->front+1)+ q->rear;
}
else if (q->rear == -1) // empty case
count =0;
else if(q->rear == q->front) count =1; // one element case
return count;
}
int main()
{
int ch,ele;
int number=0;
queue q;
init(&q);
int x;
while(1)
{

printf("\n1: Insert element \n2: Deleting element\n3: Display


elements\n4: count elements\n5: Exit \n");
printf("\n Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the element to be entered\n");
scanf("%d",&ele);
insert(&q,ele);
break;
case 2: ele=delete(&q);
printf("\nDeleted element=%d",ele);
break;
case 3: display(&q);
break;
case 4: x=count(&q);
printf("\n Total ele= %d",x);
break;
case 5: exit(0);
default:printf("\n Wrong choice! \n");
break;
}
}
return 0;
}
// BINARY SEARCH

#include <stdio.h>

void binsrch(int arr[],int key,int low,int high)


{
int mid ;
while(low<=high)
{
mid = (low+high)/2;
if(arr[mid]==key)
{
printf("the element is found at position %d\n",mid+1);
return;
}
else if (key>arr[mid])
{
low = mid+1;
}
else high = mid-1;
}
printf("the element is not found\n");
return;
}

int main()
{
int n,key;
printf("enter the size of array: ");
scanf("%d",&n);
int arr[n];
printf("enter the %d elements of array : ",n);
for (int i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
printf("enter the value you want to search : ");
scanf("%d",&key);
binsrch(arr,key,0,n-1);
return 0;
}
// SINGLY LINKED-LIST

#include<stdio.h>
#include<stdlib.h>

typedef struct node


{
int data;
struct node* next;
} node;

node* insertbegin(node* head)


{
node* nw;
nw = (node*) malloc(sizeof(node));
printf("enter the data for new node:- ");
scanf("%d",&nw->data);
nw->next = NULL;
if(head==NULL)
head = nw;
else
{
nw->next = head;
head = nw;
}
return head;
}

node* insertend(node* head)


{
node *nw,*p;
nw = (node*) malloc(sizeof(node));
printf("enter the data for new node:- ");
scanf("%d",&nw->data);
nw->next = NULL;
if(head==NULL)
head = nw;
else
{
p = head;
while(p->next)
{
p=p->next;
}
p->next = nw;
}
return head;
}

node* deletebegin(node* head)


{
node* p = head;
head = head->next;
p->next=NULL;
free(p);
return head;
}

node* deleteend(node* head)


{
node* p = head;
node* prev = NULL;
if (head->next == NULL)
{
free(head);
return NULL;
}
while (p->next)
{
prev = p;
p = p->next;
}
prev->next = NULL;
free(p);
return head;
}

node* traverse(node* head)


{
node *p = head;
while(p) //// same as while(p != NULL)
{
printf("%d\t",p->data);
p=p->next;
}
return head;
}

int count(node* head)


{
int c=0;
node *p = head;
while(p)
{
c++;
p=p->next;
}
return c;
}

node* reverse(node* head)


{
node *current,*prev,*next;
current=head;
prev=NULL;
while(current)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
return head;
}

int main()
{
node* head = NULL;
int choice;
while (1)
{
printf("\n----- Linked List Menu -----\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Delete from Beginning\n");
printf("4. Delete from End\n");
printf("5. Traverse\n");
printf("6. Count Nodes\n");
printf("7. Reverse List\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1: head = insertbegin(head);
break;
case 2: head = insertend(head);
break;
case 3: head = deletebegin(head);
break;
case 4: head = deleteend(head);
break;
case 5: traverse(head);
break;
case 6: printf("Total Nodes: %d\n", count(head));
break;
case 7: head = reverse(head); printf("List Reversed!\n");
break;
case 8: printf("Exiting program.\n");
exit(0);
default: printf("Invalid choice! Try again.\n");
}
}
return 0;
}
// Doubly linked list

#include <stdio.h>
#include <stdlib.h>

typedef struct node


{
int data;
struct node* prev,*next;
} node;

node* insertbegin(node *head)


{
node *nw = (node*)malloc(sizeof(node));
printf("Enter the information:- ");
scanf("%d",&nw->data);
nw->next=nw->prev=NULL;
if(head==NULL)
{
head=nw;
}
else
{
nw->next=head;
head->prev=nw;
head =nw;
}
return head;
}

node* insertend(node* head)


{
node*p;
node *nw = (node*)malloc(sizeof(node));
printf("Enter the information:- ");
scanf("%d",&nw->data);
nw->next=nw->prev=NULL;
if(head==NULL)
{
head=nw;
}
else
{
p=head;
while(p->next)
{
p=p->next;
}
p->next=nw;
nw->prev=p;
}
return head;
}

node* deletebegin(node* head)


{
node* p=head;
head=head->next;
if(head!=NULL)
head->prev=NULL;
free(p);
return head;
}

node* deleteend(node* head)


{
if(head->next==NULL)
{
free(head);
head=NULL;
return head;
}
node*p=head;
while(p->next)
{
p=p->next;
}
(p->prev)->next=NULL;
free(p);
return head;
}

void frwdtraverse(node *head)


{
node*p=head;
while(p)
{
printf("%d\t",p->data);
p=p->next;
}
}

void bwdtraverse(node *head)


{
node* p=head;
while(p->next)
{
p=p->next;
}
while(p)
{
printf("%d\t",p->data);
p=p->prev;
}
}
int main()
{
node *head=NULL;
int choice;
while(1)
{

printf("\n----- Linked List Menu -----\n");


printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Delete from Beginning\n");
printf("4. Delete from End\n");
printf("5. forward Traverse\n");
printf("6. Backward traverse\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1: head = insertbegin(head);
break;
case 2: head = insertend(head);
break;
case 3: head = deletebegin(head);
break;
case 4: head = deleteend(head);
break;
case 5: frwdtraverse(head);
break;
case 6: bwdtraverse(head);
break;
case 7: printf("Exiting program.\n");
exit(0);
default: printf("Invalid choice! Try again.\n");
}
}
return 0;
}
// Quick sort

#include <stdio.h>
#include <stdlib.h>
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}

// QuickSort function
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pivot = arr[low];
int i = low, j = high;

while (i < j)
{
while (arr[j] > pivot)
j--;

while (arr[i] <= pivot && i < high)


i++;

if (i < j)
{
swap(&arr[i], &arr[j]);
}
}
swap(&arr[low], &arr[j]);
quickSort(arr, low, j - 1);
quickSort(arr, j + 1, high);
}
}

void printArray(int arr[], int size)


{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}

int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);

int *arr = (int *)malloc(n * sizeof(int));


printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Original array: ");
printArray(arr, n);
quickSort(arr, 0, n - 1);
printf("Sorted array: ");
printArray(arr, n);
free(arr);
return 0;
}
// BST (BINARY SEARCH TREE- INSERTION INORDER TRAVERSE)
#include <stdio.h>
#include <stdlib.h>

typedef struct node


{
int data;
struct node *left;
struct node *right;
} node ;

// Function to create a new Node


node *createNode(int value)
{
struct node *nw = (struct node *)malloc(sizeof(struct node));
nw->data = value;
nw->left = nw->right = NULL;
return nw;
}

// insert a node recursively


node *insert(node *root, int value)
{
if (root == NULL)
{
return createNode(value);
}
if (value < root->data)
{
root->left = insert(root->left, value);
}
else if (value > root->data)
{
root->right = insert(root->right, value);
}
return root;
}

void inorderTraversal(node *root)


{
if (root != NULL)
{
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
return ;
}
node* findmax(node* root)
{
node*p=root;
if(root == NULL)
{ // Check if the tree is empty
return NULL;
}
while(p->right!=NULL)
{
p=p->right;
}
return p;
}

int main()
{
int ch, ele;
struct node *root = NULL,*m;
printf("Menu for BST :\n 1.insert \n 2.inorder traverse \n 3.find max\n
4.exit \n");
while (1)
{
printf("enter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1: printf("enter a value : ");
scanf("%d", &ele);
root = insert(root, ele);
break;
case 2: printf("inorder traverse : ");
inorderTraversal(root);
printf("\n");
break;
case 3: m=findmax(root);
if(m==NULL)
{
printf("Tree is empty\n");
}
else
printf("The maximum value is %d\n",m->data);
break;
case 4: printf("Exiting program");
exit(0);
default: printf("invalid choice\n");
break;
}
}
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy