Professional Documents
Culture Documents
HIT
HIT
HIT
#include<stdio.h>
#include<stdlib.h>
#define max_size 5
int stack[max_size],top=-1;
/*------ Function Prototype------------*/
void push();
void pop();
void peep();
void display();
/*-------------------------------------*/
int main()
{
int choice;
do{
//printf("\n");
printf("\n\n--------STACK OPERATIONS-----------\n");
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Peep\n");
printf("4.Display\n");
printf("5.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peep();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("\nInvalid choice:\n");
break;
}
}while(choice!=5);
return 0;
}
void push() //Inserting element in to the stack
{
int item;
if(top==(max_size-1))
{
printf("\nStack Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
void pop() //deleting an element from the stack
{
int item;
if(top==-1)
{
printf("Stack Underflow:");
}
else
{
item=stack[top];
top=top-1;
printf("\nThe poped element: %d\t",item);
}
}
void peep()
{
if(top==-1)
{
printf("\nStack is empty:");
}
else
{
printf("The topmost element of the stack is %d",stack[top]);
}
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is Empty:");
}
else
{
printf("\nThe stack elements are:\n" );
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
}
}
#include<stdio.h>
#include<stdlib.h>
#define max_size 5
int queue[max_size],front=-1,rear=-1;
/*------ Function Prototype------------*/
void insert();
void del();
void display();
/*-------------------------------------*/
int main()
{
int choice;
do{
printf("\n\n--------QUEUE OPERATIONS-----------\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nInvalid choice:\n");
break;
}
}while(choice!=4);
return 0;
}
void insert() //Inserting an element in to the queue
{
int item;
if(rear==(max_size-1))
{
printf("\nQueue Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
rear=rear+1;
queue[rear]=item;
if(front==-1)
front=0;
}
}//end of insert()
void del() //deleting an element from the queue
{
int item;
if(front==-1)
{
printf("\nQueue Underflow:");
}
else
{
item=queue[front];
printf("\nThe deleted element: %d\t",item);
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front=front+1;
}
}
}//end of del()
void display() //To display the queue elements
{
int i;
if(front==-1)
{
printf("\nQueue is Empty:");
}
else
{
printf("\nThe queue elements are:\n" );
for(i=front;i<=rear;i++)
{
printf("%d\t",queue[i]);
}
}
}//end of display()
#include<stdio.h>s
#include<stdlib.h>
#define max_size 5
int cqueue[max_size],front=-1,rear=-1;
/*------ Function Prototype------------*/
void insert();
void del();
void display();
/*-------------------------------------*/
main()
{
int choice;
do{
}
void insert() //Inserting an element in to the queue
{
int item;
if(front==(rear+1)%max_size)
{
printf("\nQueue Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
rear=(rear+1)%max_size;
cqueue[rear]=item;
if(front==-1)
{
front=0;
rear=0;
}
}//end of insert()
}
}//end of del()
}//end of display()
#include<stdlib.h>
/*----Function Prototypes-----*/
void create();
void display();
void insert_begin();
void insert_end();
void insert_pos();
void delete_begin();
void delete_end();
void delete_pos();
/*-----------------------------*/
struct node
{
int info;
struct node *next;
};
struct node *start=NULL;
int main() //main() starts
{
int choice;
while(1){
printf("\n***SINGLE LINKED LIST OPERATIONS:****\n");
printf("\n MENU \n");
printf("---------------------------------------\n");
printf("\n 1.Create \n");
printf("\n 2.Display \n");
printf("\n 3.Insert at the beginning \n");
printf("\n 4.Insert at the end \n");
printf("\n 5.Insert at specified position \n");
printf("\n 6.Delete from beginning \n");
printf("\n 7.Delete from the end \n");
printf("\n 8.Delete from specified position \n");
printf("\n 9.Exit \n");
printf("\n--------------------------------------\n");
printf("Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_begin();
break;
case 4:
insert_end();
break;
case 5:
insert_pos();
break;
case 6:
delete_begin();
break;
case 7:
delete_end();
break;
case 8:
delete_pos();
break;
case 9:
exit(0);
break;
default:
printf("\n Wrong Choice:\n");
break;
}//end of switch()
}
return 0;
}//end of main()
void create()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
exit(0);
}
printf("\nEnter the data value for the node:\t");
scanf("%d",&temp->info);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}//end of create()
void display()
{
struct node *ptr;
if(start==NULL)
{
printf("\nList is empty:\n");
return;
}
else
{
ptr=start;
printf("\nThe List elements are:\n");
while(ptr!=NULL)
{
printf("%d\t",ptr->info );
ptr=ptr->next ;
}//end of while
}//end of else
}//end of display()
void insert_begin()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->info);
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
temp->next=start;
start=temp;
}
}//end of insert_begin()
void insert_end()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->info );
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next !=NULL)
{
ptr=ptr->next ;
}
ptr->next =temp;
}
}//end of insert_end
void insert_pos()
{
struct node *ptr,*temp;
int i,pos;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the position for the new node to be inserted:\t");
scanf("%d",&pos);
printf("\nEnter the data value of the node:\t");
scanf("%d",&temp->info) ;
temp->next=NULL;
if(pos==0)
{
temp->next=start;
start=temp;
}
else
{
for(i=0,ptr=start;i<pos-1;i++)
{
ptr=ptr->next;
if(ptr==NULL)
{
printf("\nPosition not found:[Handle with care]\n");
return;
}
}
temp->next =ptr->next ;
ptr->next=temp;
}//end of else
}//end of insert_pos
void delete_begin()
{
struct node *ptr;
if(ptr==NULL)
{
printf("\nList is Empty:\n");
return;
}
else
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is :%d\t",ptr->info);
free(ptr);
}
}//end of delete_begin()
void delete_end()
{
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nList is Empty:");
exit(0);
}
else if(start->next ==NULL)
{
ptr=start;
start=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
}//end of delete_begin()
void delete_pos()
{
int i,pos;
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nThe List is Empty:\n");
exit(0);
}
else
{
printf("\nEnter the position of the node to be deleted:\t");
scanf("%d",&pos);
if(pos==0)
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is:%d\t",ptr->info );
free(ptr);
}
else
{
ptr=start;
for(i=0;i<pos;i++)
{
temp=ptr;
ptr=ptr->next ;
if(ptr==NULL)
{
printf("\nPosition not Found:\n");
return;
}
}
temp->next =ptr->next ;
printf("\nThe deleted element is:%d\t",ptr->info );
free(ptr);
}
}//end of else
}//end of delete_pos()
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct pnode
{
float coef;
int exp;
struct pnode *next;
}p;
p *getnode();
//Main function starts here
void main()
{
p *p1,*p2,*p3;
p *getpoly(),*add(p*,p*);
void display(p*);
clrscr();
printf(“\n Enter first polynomial”);
p1=getpoly();
printf(“\n Enter second polynomial”);
p2=getpoly();
printf(“\nThe first polynomial is”);
display(p1);
printf(“\nThe second polynomial is”);
display(p2);
p3=add(p1,p2);
printf(“\nAddition of two polynomial is :\n”);
display(p3);
}
/*
* C Program to Implement a Doubly Linked List & provide Insertion,
Deletion & Display Operations
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
struct node *prev;
int n;
struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;
void insert1();
void insert2();
void insert3();
void traversebeg();
void traverseend(int);
void sort();
void search();
void update();
void delete();
int count = 0;
void main()
{
int ch;
h = NULL;
temp = temp1 = NULL;
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert1();
break;
case 2:
insert2();
break;
case 3:
insert3();
break;
case 4:
delete();
break;
case 5:
traversebeg();
break;
case 6:
temp2 = h;
if (temp2 == NULL)
printf("\n Error : List empty to display ");
else
{
printf("\n Reverse order of linked list is : ");
traverseend(temp2->n);
}
break;
case 7:
search();
break;
case 8:
sort();
break;
case 9:
update();
break;
case 10:
exit(0);
default:
printf("\n Wrong choice menu");
}
}
}
/* TO insert at beginning */
void insert1()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}
/* To insert at end */
void insert2()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
/* To delete an element */
void delete()
{
int i = 1, pos;
if (temp2 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : ");
if (temp2 == NULL)
{
printf("\n Error : List empty to search for data");
return;
}
printf("\n Enter value to search : ");
scanf("%d", &data);
while (temp2 != NULL)
{
if (temp2->n == data)
{
printf("\n Data found in %d position",count + 1);
return;
}
else
temp2 = temp2->next;
count++;
}
printf("\n Error : %d not found in list", data);
}
temp2->n = data1;
traversebeg();
return;
}
else
temp2 = temp2->next;
}
temp2 = h;
temp4 = h;
if (temp2 == NULL)
{
printf("\n List empty to sort");
return;
}
#include <stdio.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start = NULL;
struct node *create_hll(struct node *);
struct node *display(struct node *);
int main()
{
int option;
do
{
printf("\n\n -----Main Menu-----");
printf("\n 1. Create a list");
printf("\n 2. Display the list");
printf("\n 3. Exit");
printf("\n Enter your choice : ");
scanf("%d", &option);
switch(option)
{
case 1:
start = create_hll(start);
printf("\n Header Linked List Created Successfully");
break;
case 2:
start = display(start);
break;
}
}while(option != 3);
return 0;
}
struct node *create_hll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter -1 to end");
printf("\n Enter the data :");
scanf("%d", &num);
while(num != -1)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data = num;
new_node -> next = NULL;
if(start == NULL)
{
start = (struct node*)malloc(sizeof(struct node));
start -> next = new_node;
}
else
{
ptr = start;
while(ptr -> next != NULL)
ptr = ptr -> next;
ptr -> next = new_node;
}
printf("\n Enter the data :");
scanf("%d", &num);
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr = start;
ptr = ptr -> next;
while(ptr != NULL)
{
printf("\t %d",ptr -> data);
ptr = ptr -> next;
}
return start;
}
Searching
#include<stdio.h>
int key;
int binary(int a[],int low,int high);
int linear(int a[],int n);
main()
{
int n,i,a[10],pos,ch;
do
{
printf("-------Menu--------\n1.Binary Search\n2.Linear Search\n3.Exit");
printf("\nEnter your choice ");
scanf("%d",&ch);
switch(ch)
{
case 1 :
printf("\n Enter how many elements: ");
scanf("%d",&n);
printf("\n Enter elements of array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter the elements to be searched");
scanf("%d",&key);
pos=linear(a,n);
if(pos==-1)
printf("\n element not found");
else
printf("\n element found at position %d",pos+1);
break;
case 2 :
printf("\n Enter how many elements: ");
scanf("%d",&n);
printf("\n Enter elements of array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter the elements to be searched");
scanf("%d",&key);
pos=linear(a,n);
if(pos==-1)
printf("\n element not found");
else
printf("\n element found at position %d",pos+1);
break;
case 3 :exit(0);
if(low>high)
{
return(-1);
}
mid=(low+high)/2;
if(key==a[mid])
{
return(mid);
}
if(key<a[mid])
{
return(binary(a,low,mid-1));
}
else
{
return(binary(a,mid+1,high));
}
}
Sorting
#include<stdio.h>
#include<stdlib.h>
//-----------------Main Function----------------------
int main()
{
int n,choice,i;
char ch[20];
printf("Enter no. of elements u want to sort : ");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
printf("Enter %d Element : ",i+1);
scanf("%d",&arr[i]);
}
printf("Please select any option Given Below for Sorting : \n");
while(1)
{
printf("\n1. Bubble Sort\n2. Selection Sort\n3. Insertion Sort\n4. Display Array.\n5. Exit
the Program.\n");
printf("\nEnter your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
bubble_sort(arr,n);
break;
case 2:
selection_sort(arr,n);
break;
case 3:
insertion_sort(arr,n);
break;
case 4:
display(arr,n);
break;
case 5:
return 0;
default:
printf("\nPlease Select only 1-5 option ----\n");
}
}
return 0;
}
//-------------------Display Function-----------------
}
printf("After Selection sort Elements are : ");
display(arr,n);
}
merge(arr, l, m, r);
}
}
/* UTILITY FUNCTIONS */
/* Function to print an array */
void printArray(int A[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
BST
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node NODE;
}
if(item<prev->info)
prev->llink=temp;
else
prev->rlink=temp;
return root;
}
NODE *construct_BST(NODE *root)
{
int a,n,i;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the elements to be inserted in the tree\n");
for (i=0;i<n;i++)
{
scanf("%d",&a);
root=insert(a,root);
}
printf("Tree Constructed Successfully!!!!!!\n");
return root;
}
void preorder(NODE *root)
{
if(root!=NULL)
{
printf("%d\t",root->info);
preorder(root->llink);
preorder(root->rlink);
}
}
void main()
{
int item,ch,key,n;
NODE *root;
root=NULL;
while (1)
{
printf("\nEnter the choice\n1.ConstructBST\n2.Preorder\n3.Inorder\n4.Postorder\n5.Search
an Element\n6.Delete an Element\n7:Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: root=construct_BST(root);
break;
case 2: preorder(root);
break;
case 3: inorder(root);
break;
case 4: postorder(root);
break;
case 5: if (root==NULL)
printf("List Empty\n");
else
{
/*
*/
KRUSKAL
#include<stdio.h>
#define MAX 30
edgelist elist;
int G[MAX][MAX],n;
edgelist spanlist;
void kruskal();
int find(int belongs[],int vertexno);
void union1(int belongs[],int c1,int c2);
void sort();
void print();
void main()
{
int i,j,total_cost;
printf("\nEnter number of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
kruskal();
print();
}
void kruskal()
{
int belongs[MAX],i,j,cno1,cno2;
elist.n=0;
for(i=1;i<n;i++)
for(j=0;j<i;j++)
{
if(G[i][j]!=0)
{
elist.data[elist.n].u=i;
elist.data[elist.n].v=j;
elist.data[elist.n].w=G[i][j];
elist.n++;
}
}
sort();
for(i=0;i<n;i++)
belongs[i]=i;
spanlist.n=0;
for(i=0;i<elist.n;i++)
{
cno1=find(belongs,elist.data[i].u);
cno2=find(belongs,elist.data[i].v);
if(cno1!=cno2)
{
spanlist.data[spanlist.n]=elist.data[i];
spanlist.n=spanlist.n+1;
union1(belongs,cno1,cno2);
}
}
}
void sort()
{
int i,j;
edge temp;
for(i=1;i<elist.n;i++)
for(j=0;j<elist.n-1;j++)
if(elist.data[j].w>elist.data[j+1].w)
{
temp=elist.data[j];
elist.data[j]=elist.data[j+1];
elist.data[j+1]=temp;
}
}
void print()
{
int i,cost=0;
for(i=0;i<spanlist.n;i++)
{
printf("\n%d\t%d\t%d",spanlist.data[i].u,spanlist.data[i].v,spanlist.data[i].w);
cost=cost+spanlist.data[i].w;
}
BFS
#include<stdio.h>
int a[20][20], q[20], visited[20], n, i, j, f = 0, r = -1;
void bfs(int v) {
for(i = 1; i <= n; i++)
if(a[v][i] && !visited[i])
q[++r] = i;
if(f <= r) {
visited[q[f]] = 1;
bfs(q[f++]);
}
}
void main() {
int v;
printf("\n Enter the number of vertices:");
scanf("%d", &n);