0% found this document useful (0 votes)
12 views21 pages

Dst External Program (1)

The document contains various C programming implementations for data structures and algorithms, including array operations (creation, insertion, deletion, display), sorting algorithms (bubble sort, selection sort, insertion sort), searching algorithms (linear and binary search), stack operations, queue operations, linked list operations, and tree traversal methods (inorder, postorder, preorder). Each section includes code snippets demonstrating the respective operations and functionalities. The document serves as a comprehensive guide for understanding and implementing fundamental data structures and algorithms in C.

Uploaded by

saibachhav04
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)
12 views21 pages

Dst External Program (1)

The document contains various C programming implementations for data structures and algorithms, including array operations (creation, insertion, deletion, display), sorting algorithms (bubble sort, selection sort, insertion sort), searching algorithms (linear and binary search), stack operations, queue operations, linked list operations, and tree traversal methods (inorder, postorder, preorder). Each section includes code snippets demonstrating the respective operations and functionalities. The document serves as a comprehensive guide for understanding and implementing fundamental data structures and algorithms in C.

Uploaded by

saibachhav04
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/ 21

#include<stdio.

h>
void create();
void insertion();
void deletion();
void display();
int a[20],i,n,pos,item;

int main()
{
int choice;
printf("\n\n*************** MENU *****************");
printf("\n1. Create");
printf("\n2. Display");
printf("\n3. insertion");
printf("\n4. deletion");
printf("\n\nEnter Your Choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1 : create();
break;
case 2 : printf("\n************ DISPLAY DATA *************");
printf("\nEntered Data : ");
display();
break;
case 3 : insertion();
break;
case 4 : deletion();
break;
default : printf("\nEnter Valid Choice");
}
main();
return 0;
}

void create()
{

printf("enter the size of array ");


scanf("%d",&n);
printf("enter the array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

}
void insertion()
{
printf("\n insertion operation");
printf("\n enter the number to be inserted");
scanf("\n%d",&item);
printf("\nenter the position to insert an element");
scanf("\n%d",&pos);
if(pos<=n)
{
n++;
for(i=n-1;i>=pos;i--)
{
a[i]=a[i-1];
}
a[pos-1]=item;
}
else
{
printf("insertion is not possible");
}
}
void deletion()
{
printf("enter the position to be deleted");
scanf("\n%d",&pos);
if(pos>=n+1)
{
printf("\ndeletion is not possible");
}
else{
for(i=pos-1;i<n-1;i++ )
{
a[i]=a[i+1];

}n--;
}
}
void display()
{
printf("\n\narray is=====");
for(i=0;i<n;i++)
{
printf("\n\n%d",a[i]);
}
}
Bubble sort

//Bubble sort
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],N,i,j,temp;
clrscr();
prinƞ("\nEnter Array Size:");
scanf("%d",&N);
prinƞ("\nEnter Array Elements:");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<N;i++)
{
for(j=0;j<N-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
prinƞ("\nSorted Array Elements:");
for(i=0;i<N;i++)
{
prinƞ("%d\t",a[i]);
}
getch();
}

//SelecƟon sort
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],N,i,j,k,temp;
clrscr();
prinƞ("\nEnter Array Size:");
scanf("%d",&N);
prinƞ("\nEnter Array Elements:");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<N-1;i++)
{
k=i;
for(j=i+1;j<N;j++)
{
if(a[j]<a[k])
{
k=j;
}
}
if(k!=i)
{
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
}
prinƞ("\nSorted Array Elements:");
for(i=0;i<N;i++)
{
prinƞ("%d\t",a[i]);
}
getch();
}

//InserƟon sort
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],N,i,j,k,temp;
clrscr();
prinƞ("\nEnter Array Size:");
scanf("%d",&N);
prinƞ("\nEnter Array Elements:");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<N;i++)
{
temp=a[i];
for(j=i-1;j>=0&&a[j]>temp;j--)
{
a[j+1]=a[j];
}
a[j+1]=temp;
}
prinƞ("\nSorted Array Elements:");
for(i=0;i<N;i++)
{
prinƞ("%d\t",a[i]);
}
getch();
}

//Linear Search
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],N,i,key,flag=0;
clrscr();
prinƞ("\nEnter Array Size:");
scanf("%d",&N);
prinƞ("\nEnter Array Elements:");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
prinƞ("\nEnter key element for searching:");
scanf("%d",&key);
for(i=0;i<N;i++)
{
if(key==a[i])
{
flag=1;
break;
}
}
if(flag==1)
{
prinƞ("\nElement is found");
}
else
{
prinƞ("\nElement is not found");
}

}
//Binary Search
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],N,i,j,mid,key,temp;
clrscr();
prinƞ("\nEnter Array Size:");
scanf("%d",&N);
prinƞ("\nEnter Array Elements:");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
prinƞ("\nEnter key element for searching:");
scanf("%d",&key);
for(i=1;i<N;i++)
{
for(j=0;j<N-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
i=0;
j=N-1;
mid=(i+j)/2;
while(key!=a[mid]&&i<=j)
{
if(key<a[mid])
{
j=mid-1;
}
else
{
i=mid+1;
}
mid=(i+j)/2;
}
if(i<=j)
{
printf("\nElement is Found");
}
else
{
prinƞ("\nElement is not Found");
}
getch();
}

Stack operation

#include<stdio.h>
#include<conio.h>
#define MAX 10
int data[MAX],top;

void initialize();
int empty();
int full();
void push();
void pop();
void print();

int main()
{
int ch;

initialize();
do{
printf("\n *******STACK OPERATION****** ");
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.PRINT");
printf("\n4.EXIT");
printf("\n\nENTER YPUR CHOICE");
scanf("\n%d",&ch);

switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:print();
break;
case 4: printf("thenks for using our software");
break;
default:printf("enter the invalid choice");
break;
}
}while(ch!=4);
return 0;
}
void initialize()
{
top=-1;
}
int empty()
{
if(top==-1)
{
return(1);
}
else{
return(0);
}

}
int full()
{
if(top==MAX-1)
{
return(1);
}
else{
return(0);
}
}
void push()
{
int x;
if(full()==0)
{
printf("\n enter the data");
scanf("%d",&x);
top=top+1;
data[top]=x;
printf("your data is pushed successfully");

}
else{
printf("stack is full");

}
}
void pop()
{
int x;
if(empty()==0)
{
x=data[top];
top=top-1;
printf("popped data =%d",x);

}
else{
printf("stack is empty()");

}
}
void print()
{
int i;
printf("stack dta");
for(i=top;i>=0;i--)
{
printf("%d \t",data[i]);

}
}

Postfix expression

#include<stdio.h>

#define MAX 20

typedef
struct stack
{
int data[MAX];
int top;
}stack;

void init(stack *);


int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *,int);
int evaluate(char x,int op1,int op2);

int main()
{
stack s;
char x;
int op1,op2,val;
init(&s);
printf("Enter the expression(eg: 59+3*)\nSingle digit operand and operators
only:");
while((x=getchar())!='\n')
{
if(isdigit(x))
push(&s,x-48); //x-48 for removing the effect of ASCII
else
{
op2=pop(&s);
op1=pop(&s);
val=evaluate(x,op1,op2);
push(&s,val);
}
}
val=pop(&s);
printf("\nValue of expression=%d",val);

return 0;
}

int evaluate(char x,int op1,int op2)


{
if(x=='+')
return(op1+op2);
if(x=='-')
return(op1-op2);
if(x=='*')
return(op1*op2);
if(x=='/')
return(op1/op2);
if(x=='%')
return(op1%op2);
}

void init(stack *s)


{
s->top=-1;
}

int empty(stack *s)


{
if(s->top==-1)
return(1);
return(0);
}

int full(stack *s)


{
if(s->top==MAX-1)
return(1);
return(0);
}

void push(stack *s,int x)


{
s->top=s->top+1;
s->data[s->top]=x;
}

int pop(stack *s)


{
int x;
x=s->data[s->top];
s->top=s->top-1;
return(x);
}

Queue operations

#include <stdio.h>
#define MAX 5
int data[MAX],rear,front;
void initialize();
int full();
int empty();
void insertion();
void deletion();
void print();
int main()
{
int ch;
initialize();
do{

printf("\n 1. insertion");
printf("\n 2. deletion");
printf("\n 3. display");
printf("\n 4. exit");
printf("\n . enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:insertion();
break;
case 2:deletion();
break;
case 3:print();
break;
case 4:printf("\nthanks for using my software");
default:printf("\nenter valid choice");

}
}while(ch!=4);
return 0;
}
void initialize()
{
rear=front=-1;
}
int empty()
{
if(rear==-1)
{
return(1);
}
else{
return(0);
}

}
int full()
{
if (rear==MAX-1)
{
return(1);
}
else{
return(0);
}
}
void insertion()
{
int x;
if(full()==0)
{
printf("\nenter the data");
scanf("%d",&x);
if(rear==-1)
{
front=rear=0;
data[rear]=x;
}
else{
rear=rear+1;
data[rear]=x;
}
printf("\ndata inserted succesfully");
}
else{
printf("\nqueue is full");
}
}
void deletion()
{
int x;
if (empty()==0)
{
x=data[front];
if(rear==front)
{
rear=front=-1;
}
else{
front=front+1;
}
printf("\n\ndata deleted sucessfully=%d",x);
}
else{

printf("\nqueue is empty");
}
}
void print()
{
int i;
printf("\n queue data");
for(i=front;i<=rear;i++)
{
printf("\n%d",data[i]);
}
}

Linked list

#include<stdio.h>
#include<stdlib.h>
void create();
void display();
void insert_begin();
void insert_end();
void insert_pos();
int i;
struct node* head = NULL;
struct node
{
int data;
struct node* next;
};
int main()
{
int choice;
while(1)
{
printf("\n****LINKED LIST PROGRAM****\n");
printf("1. Create\n");
printf("2. display\n");
printf("3. Insert Node at beginning\n");
printf("4. Insert Node in specific position\n");
printf("5. Insert Node at end of LinkedList\n");

printf("9. ** To exit **");

printf("\n Enter your choice: ");


scanf("%d",&choice);
switch(choice)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert_begin();
break;
case 4: insert_pos();
break;
case 5: insert_end();
break;

case 9: exit(0);
default:printf("\n Wrong Choice");
break;
}
}
}
//creates a node
void create()
{
struct node* temp;
//creating new node
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data: ");
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL) {
head = temp;
}
else{
struct node* ptr = head;
while(ptr->next!=NULL)
{
ptr = ptr->next;
}
ptr->next = temp; //inserting at end of List
}
}
// prints the entire LinkedList
void display()
{
if(head==NULL)
{
printf("Linked List is Empty\n");
return;
}
printf("LinkedList: ");
struct node* ptr = head;
while(ptr!=NULL) // start from first node
{
printf("%d ",ptr->data);
ptr = ptr->next;
}
printf("\n");
}
// to insert node at start of LinkedList
void insert_begin()
{
struct node* temp;
// creating a new node
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data: ");
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL)
{
head = temp;
return;
}
else
{
temp->next = head; //point it to old head node
head = temp; //point head to new first node
}
}
// to insert node at given position
void insert_pos()
{
struct node* temp;
// creating a new node
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data: ");
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL) // if list empty we return
{
head = temp;
return;
}
else
{
struct node* prev_ptr;
struct node* ptr = head;
int pos;
printf("Enter position: ");
scanf("%d",&pos);
for(i=0;i<pos;i++)
{
prev_ptr = ptr;
ptr = ptr->next;
}
//new node pointing to node in that pos
temp->next = ptr;
//prevptr pointing to new node
prev_ptr->next = temp;
}
}
// to insert node at end of LinkedList
void insert_end()
{
struct node* temp;
//creating new node
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data: ");
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL)
{
head = temp; //if list is empty, we return
return;
}
else{
struct node* ptr = head;
while(ptr->next!=NULL)
{
ptr = ptr->next;
}
// tail node pointing to new node
ptr->next = temp;
}
}

Tree Traversal

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

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

void inorder(struct node *root){


if(root == NULL){
return;
}
inorder(root->left);
printf("%d-->" , root->data);
inorder(root->right);

void postorder(struct node *root){


if(root == NULL){
return;
}
postorder(root->left);
postorder(root->right);
printf("%d-->" , root->data);
}

void preorder(struct node *root){


if(root==NULL){
return;
}
printf("%d-->" , root->data);
preorder(root->left);
preorder(root->right);
}

struct node *create_node(value){


struct node *new_node;
new_node = (struct node *)malloc(sizeof(struct
node));
new_node->data = value;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}

struct node *insert_left(struct node *root , int value){


root->left = create_node(value);
return root->left;
}

struct node *insert_right(struct node *root , int


value){
root->right = create_node(value);
return root->right;
}

int main() {
struct node *root = create_node(1);
insert_left(root , 12);
insert_right(root , 9);

insert_left(root->left , 5);
insert_right(root->left , 6);

printf("Inorder Traversal: ");


inorder(root);
printf("\n");

printf("Preorder Traversal: ");


preorder(root);
printf("\n");

printf("Postorder Traversal: ");


postorder(root);
printf("\n");

return 0;
}

Adjacency matrix

#include <stdio.h>

#define MAX_VERTICES 100

int graph[MAX_VERTICES][MAX_VERTICES];

void displayAdjacencyMatrix(int numVertices) {


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

int main() {
int numVertices, numEdges;

printf("Enter the number of vertices: ");


scanf("%d", &numVertices);

printf("Enter the number of edges: ");


scanf("%d", &numEdges);

// Initialize the graph with zeros


for (int i = 0; i < numVertices; ++i) {
for (int j = 0; j < numVertices; ++j) {
graph[i][j] = 0;
}
}

// Input edges and populate the adjacency matrix


for (int i = 0; i < numEdges; ++i) {
int vertex1, vertex2;
printf("Enter edge (vertex1 vertex2): ");
scanf("%d %d", &vertex1, &vertex2);

// Assuming an undirected graph


graph[vertex1][vertex2] = 1;
graph[vertex2][vertex1] = 1;
}

// Display the adjacency matrix


printf("\nAdjacency Matrix:\n");
displayAdjacencyMatrix(numVertices);

return 0;
}

Programs for external oral…

# viva…

You might also like