0% found this document useful (0 votes)
16 views

Ds

Uploaded by

ADITI NEGI
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)
16 views

Ds

Uploaded by

ADITI NEGI
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/ 34

CODE :-

/* Sumit Pal Singh - 63 - B


Q1. Write a C program to implement priority queue using doubly linked list (Priority
depends on identity number. Small identity number has greater priority. If identity
numbers are equal. Then FIFO rules
are used) with following functions,
1) insert 2) serve 3) display
*/
#include<stdio.h>
#include<stdlib.h>
//STRUCTURE OF DOUBLY LINKEDLIST
typedef struct linkedlist{
int val;
struct linkedlist *next, *prev;
}node;
node * createNode();
node * insert(node*);
node * delete(node*);
void display(node *);
void main(){
node *head=NULL;
int ch;
do{
printf("1 > insert\n2 > serve\n3 > display\n");
scanf("%d",&ch);
switch(ch){
case 1 :
head = insert(head); break;
case 2 :
head = delete(head); break;
case 3 :
display(head); break;
default :
printf("Invalid Input");
}
printf("\ndo you want to continue (press 1) : ");
scanf("%d",&ch);
}while(ch==1);
}
//CREATE NODE AND RETURN IT
node* createNode(){
node *temp=(node*)malloc(sizeof(node));
printf("enter identity number : ");
scanf("%d",&temp->val);
temp->next=NULL;
temp->prev=NULL;
return temp;
}
//INSERT NEW CREATED NODE IN LINKEDLIST
node* insert(node *head){
node *curr=head, *temp=createNode();
if(curr==NULL) return temp;
while(curr->next!=NULL && curr->val<temp->val){
curr=curr->next;
}
if(curr->val<temp->val){
curr->next = temp;
temp->prev = curr;
}
else{
if(curr->prev!=NULL) curr->prev->next = temp;
else head = temp;
temp->prev = curr->prev;
temp->next = curr;
curr->prev = temp;
}
return head;
}
node* delete(node *head){
node *curr=head;
if(curr==NULL || curr->next==NULL){
if(curr==NULL) return head;
printf("top element : %d",head->val);
return NULL;
}
printf("top element : %d",head->val);
head = head->next;
head->prev=NULL;
free(curr);
return head;
}
void display(node *head){
while(head!=NULL){
printf("%d -> ",head->val);
head=head->next;
}
printf("NULL");
}
OUTPUT :-
CODE :-
/* Sumit Pal Singh - 63 - B
Q2.Write a C program to Insert and Delete elements form a Queue using link list ,each
node
should have the following inforamaion about a product Product_Id(char) ,
Product_Name(string) , Total_sale(integer),Product_Grade(Char)
*/
#include<stdio.h>
#include<stdlib.h>
//STRUCTURE OF LINKEDLIST
typedef struct linkedlist{
char product_id, product_grade, product_name[30];
int total_sales;
struct linkedlist *next;
}node;
node * createNode();
node * insert(node*);
node * delete(node*);
void main(){
node *head=NULL;
int ch;
do{
printf("1 > insert\n2 > serve\n");
scanf("%d",&ch);
switch(ch){
case 1 :
head = insert(head); break;
case 2 :
head = delete(head); break;
default :
printf("Invalid Input");
}
printf("\ndo you want to continue (press 1) : ");
scanf("%d",&ch);
}while(ch==1);
}
//CREATE A NEW NODE AND INSERT DATA
node* createNode(){
node *temp=malloc(sizeof(node));
fflush(stdin);
printf("enter product id : ");
scanf("%c",&temp->product_id);
fflush(stdin);
printf("enter product grade : ");
scanf("%c",&temp->product_grade);
fflush(stdin);
printf("enter product name : ");
scanf("%s",&temp->product_name);
fflush(stdin);
printf("enter total sale : ");
scanf("%d",&temp->total_sales);
temp->next=NULL;
return temp;
}
//INSERT NODE IN LINKEDLIST
node* insert(node *head){
node *curr=head;
if(curr==NULL) return createNode();
while(curr->next!=NULL){
curr = curr->next;
}
curr->next = createNode();
return head;
}
node* delete(node *head){
node *curr = head;
if(curr==NULL) return head;
printf("product id : %c\n",curr->product_id);
printf("product name : %s\n",curr->product_name);
printf("product grade : %c\n",curr->product_grade);
printf("total sales : %d",curr->total_sales);
head=head->next;
free(curr);
return head;
}
OUTPUT :-
CODE :-
/* Sumit Pal Singh - 63 - B
Q3. Write a C program to create a single linked list then input a value V, partition it
such that all nodes less than V come before nodes greater than or equal to V
*/
#include<stdio.h>
#include<stdlib.h>
//STRUCTURE OF LINNKEDLIST
typedef struct linkedlist{
int val;
struct linkedlist *next;
}node;
node * createNode();
node * insert(node*);
void partition(node *);
void display(node *);
void main(){
node *head=NULL;
int ch;
do{
printf("1 > insert\n2 > partition\n3 > display\n");
scanf("%d",&ch);
switch(ch){
case 1 :
head = insert(head); break;
case 2 :
partition(head); break;
case 3 :
display(head); break;
default :
printf("Invalid Input");
}
printf("\ndo you want to continue (press 1) : ");
scanf("%d",&ch);
}while(ch==1);
}
//CREATE NEW NODE AND INSERT DATA
node* createNode(){
node *temp=malloc(sizeof(node));
printf("enter value : ");
scanf("%d",&temp->val);
temp->next=NULL;
return temp;
}
//INSERT NODE IN LINKEDLIST
node* insert(node *head){
node *temp=createNode();
if(head==NULL) return temp;
temp->next=head->next;
head->next = temp ;
return head;
}
//DO PARTITION AND ARRANGE ALL NUMBERS
void partition(node *head){
int v,temp;
node *curr=head, *swap=head;
printf("enter pivot value : ");
scanf("%d",&v);
while(curr!=NULL){
if(curr->val<v){
temp = curr->val;
curr->val = swap->val;
swap->val = temp;
swap = swap->next;
}
curr = curr->next;
}
}
void display(node *head){
while(head!=NULL){
printf("%d -> ",head->val);
head = head->next;
}
printf("NULL");
}
OUTPUT :-
CODE :-
/* Sumit Pal Singh - 63 - B
Q4. Write a C program to create a linked list P, then write a ‘C’ function named split
to create two linked lists Q & R from P So that Q contains all elements in odd positions
of P and R contains the remaining elements. Finally print both linked lists i.e. Q and R.
*/
#include<stdio.h>
#include<stdlib.h>
//STRUCTURE OF LINKEDLIST
typedef struct linkedlist{
int val;
struct linkedlist *next;
}node;
node * createNode();
node * insert(node*);
void split(node *);
void display(node *);
void main(){
node *head=NULL;
int ch;
do{
printf("1 > insert\n2 > partition\n3 > display\n");
scanf("%d",&ch);
switch(ch){
case 1 :
head = insert(head); break;
case 2 :
split(head); break;
case 3 :
display(head); break;
default :
printf("Invalid Input");
}
printf("\ndo you want to continue (press 1) : ");
scanf("%d",&ch);
}while(ch==1);
}
//CREATE NEW NODE AND INSERT DATA
node* createNode(int val){
node *temp=malloc(sizeof(node));
temp->val = val;
temp->next=NULL;
return temp;
}
//ADD NEW NODE IN LINKEDLIST
node* insert(node *head){
node *temp=NULL;
int val;
printf("enter value : ");
scanf("%d",&val);
temp=createNode(val);
if(head==NULL) return temp;
temp->next=head->next;
head->next = temp ;
return head;
}
//CREATE TWO NEW LINKEDLIST AND SPLIT THE DATA
void split(node *head){
node *q_head=NULL, *q_tail=NULL, *r_head=NULL, *r_tail=NULL, *curr=head;
int odd=1;
while(curr!=NULL){
if(odd%2==1){
if(q_head==NULL){
q_head = createNode(curr->val);
q_tail = q_head;
}
else{
q_tail->next = createNode(curr->val);
q_tail = q_tail->next;
}
}
else{
if(r_head==NULL){
r_head = createNode(curr->val);
r_tail = r_head;
}
else{
r_tail->next = createNode(curr->val);
r_tail = r_tail->next;
}
}
curr = curr->next;
odd++;
}
printf("q : ");
display(q_head);
printf("\nr : ");
display(r_head);
}
void display(node *head){
while(head!=NULL){
printf("%d -> ",head->val);
head = head->next;
}
printf("NULL");
}
OUTPUT :-
CODE :-
/* Sumit Pal Singh - 63 - B
Q5. W.A.P. to create a binary search tree and perform following operations:
1) Search a particular key.
2) Delete a node from the tree.
3) Find total number of leaf nodes
4) Find height of a binary search tree
5) Count total numbers of nodes from right hand side of root node
*/
#include<stdio.h>
#include<stdlib.h>
//STRUCTURE OF BINARY SEARCH TREE
typedef struct binary_search_tree{
int val;
struct binary_search_tree *left, *right;
}node;
node * createNode(int);
void insert(node **, int);
node ** search(node **, int);
void delete(node **, int);
void leaf_node(node *, int *);
int height(node *, int);
void right(node *, int *);
void main(){
node *root=NULL, *temp=NULL;
int ch;
do{
printf("1 > insert\n2 > search\n3 > delete a node\n4 > total leaf node\n5 > height of
bst\n6 > total node in right side of root\n");
scanf("%d",&ch);
switch(ch){
case 1 : {
printf("enter value : ");
scanf("%d",&ch);
insert(&root, ch);
break;
}
case 2 : {
printf("enter value : ");
scanf("%d",&ch);
temp = *(search(&root, ch));
if(temp==NULL) printf("not found");
else printf("%d",temp->val);
break;
}
case 3 : {
printf("enter value : ");
scanf("%d",&ch);
delete(&root, ch);
break;
}
case 4 :{
ch=0;
leaf_node(root,&ch);
printf("%d",ch);
break;
}
case 5 : {
printf("%d",height(root,-1));
break;
}
case 6 : {
ch=0;
if(root!=NULL) right(root,&ch);
printf("%d",ch);
break;
}
}
printf("\ndo you want to continue (press 1) : ");
scanf("%d",&ch);
}while(ch==1);
}
//CREATE NEW NODE ON GIVEN VALUE
node* createNode(int val){
node *temp = (node *)malloc(sizeof(node));
temp->val=val;
temp->left = NULL;
temp->right = NULL;
return temp;
}
//INSERT NEW NODE IN BST
void insert(node **root, int val){
if(*root==NULL) *root = createNode(val);
if((*root)->val>val) insert(&((*root)->left), val);
if((*root)->val<val) insert(&((*root)->right), val);
}
//SEARCH ELEMENT AND RETURN ADDRESS OF THAT NODE
node** search(node **root, int val){
if(*root==NULL) return root;
if((*root)->val == val) return root;
if((*root)->val > val) return search(&((*root)->left), val);
else return search(&((*root)->right), val);
}
//DELETE ELEMENT FROM BST
void delete(node **root, int val){
node *del=NULL, *prev=NULL;
//SEARCH FUNCTION RETURN THAT ADDRESS OF ADDRESS WHICH POINT
DELETE NODE
root = search(root, val);
del = *root;
if(del==NULL) return;
if(del->left == del->right) *root=NULL;
else if(del->left!=NULL && del->right!=NULL){
prev = del;
del = del->right;
while(del->left!=NULL){
//USING PREV POINTER MAINTAIN PARENT ADDRESS
prev = del;
del = del->left;
}
if(prev==*root) prev->right = del->right;
else prev->left = del->right;
(*root)->val = del->val;
}
else if(del->left!=NULL){
*root = del->left;
}
else{
*root = del->right;
}
free(del);
}
//COUNT LEAF NODES
void leaf_node(node *root, int *count){
if(root==NULL) return;
if(root->left==root->right) (*count)++;
leaf_node(root->left, count);
leaf_node(root->right, count);
}
//COUNT HEIGHT OF BST
int height(node *root, int count){
if(root==NULL) return count;
int left = height(root->left, count+1);
int right= height(root->right, count+1);
return left<right?right:left;
}
//COUNT NODES RIGHT SIDE OF ROOT
void right(node *root, int *count){
if(root==NULL) return;
right(root->left, count);
(*count)++;
right(root->right, count);
}
OUTPUT:-
Insert:-
Search :-

Delete :-

Leaf_node:-
Height:-

Total right side nodes:-


CODE:
/* Sumit Pal Singh - 63 - B
Q6. Write a program to add of two polynomials of degree n, using linked list
For example p1=anx¬n+an-1xn-1 + an-2xn-2 + …….. a0x0
P2=bnx¬n+bn-1xn-1 + bn-2xn-2 + …….. b0x0
p1 = first polynomial
p2 = second polynomial
Find out p3= p1+p2
*/
#include<stdio.h>
#include<stdlib.h>
//STRUCTURE OF LINKEDLIST
typedef struct linkedlist{
int coef,pow;
struct linkedlist *next;
}node;
node * createNode(int , int);
node * insert(node*, int , int);
void add(node *, node *);
void display(node *);
void main(){
node *first=NULL, *second=NULL;
int ch, coef, pow;
do{
printf("1 > insert in first\n2 > insert in second\n3 > addtition\n");
scanf("%d",&ch);
switch(ch){
case 1 :
printf("enter coefficient \t enter power\n");
scanf("%d %d",&coef,&pow);
first = insert(first, coef, pow); break;
case 2 :
printf("enter coefficient \t enter power\n");
scanf("%d %d",&coef,&pow);
second = insert(second, coef, pow); break;
case 3 :
add(first, second); break;
default :
printf("Invalid Input");
}
printf("\ndo you want to continue (press 1) : ");
scanf("%d",&ch);
}while(ch==1);
}
//CREATE NEW NODE AND INSERT DATA
node* createNode(int coef, int pow){
node *temp=malloc(sizeof(node));
temp->coef = coef;
temp->pow = pow;
temp->next=NULL;
return temp;
}
//INSERT NODE IN LINKEDLIST IN SORTED ORDER
node* insert(node *head, int coef, int pow){
node *curr=head, *temp=NULL, *prev=NULL;
temp = createNode(coef, pow);
if(curr==NULL) return temp;
while(curr!=NULL && curr->pow<temp->pow){
prev = curr;
curr=curr->next;
}
if(curr!=NULL && curr->pow==temp->pow) curr->coef+=temp->coef;
else if(curr==head && temp->pow<curr->pow){
temp->next=curr;
head=temp;
}
else{
temp->next=curr;
prev->next=temp;
}
return head;
}
//CREATE NEW LINKEDLIST AND PERFORM ADDITION
void add(node *first, node *second){
node *ans=NULL;
while(first!=NULL || second!=NULL){
if(first!=NULL && second!=NULL){
if(first->pow==second->pow){
ans = insert(ans, first->coef + second->coef, first->pow);
first = first->next;
second = second->next;
}
else if(first->pow > second->pow){
ans = insert(ans, first->coef, first->pow);
first = first->next;
}
else{
ans = insert(ans, second->coef, second->pow);
second = second->next;
}
}
else if(first!=NULL){
ans = insert(ans, first->coef, first->pow);
first = first->next;
}
else{
ans = insert(ans, second->coef, second->pow);
second = second->next;
}
}
display(ans);
}
void display(node *head){
if(head!=NULL){
display(head->next);
printf("%dx^%d ",head->coef,head->pow);
}
}
OUTPUT:-
CODE :-
/* Sumit Pal Singh - 63 - B
Q7. Write a C program to sort a sequence of characters given by user in an array, using
Merge sort
technique.
*/
#include<stdio.h>
#define max 100
//MERGE_SORT FUNCTIONS
void merge_sort(char[], int, int);
void merge(char[], int, int, int);
void main(){
char ch[max];
int n=0,i;
do{
printf("1 > insert\n2 > sort\n3 > display\n");
scanf("%d",&i);
switch(i){
case 1 : {
fflush(stdin);
printf("enter character : ");
scanf("%c",&ch[n++]);
break;
}
case 2 : {
merge_sort(ch, 0, n-1);
break;
}
case 3 : {
for(i=0; i<n; i++){
printf("%c ",ch[i]);
}
break;
}
default : printf("Invalid Input");
}
printf("\ndo you want to continue (press 1) : ");
scanf("%d",&i);
}while(i==1);
}
//IT DIVIDE THE ARRAY INTO SMALL ARRAY
void merge_sort(char ch[], int i, int j){
int mid;
if(i<j){
mid = (i+j)/2;
merge_sort(ch, i,mid);
merge_sort(ch, mid+1,j);
merge(ch, i, j, mid);
}
}
//MERGE SMALL ARRAY USIN SORTING ORDER
void merge(char ch[], int i, int j, int mid){
char temp[max];
int l=mid,n=j,k=j;
while(l>=i || j>mid){
if(l>=i && j>mid){
if(ch[l]<ch[j])
temp[k--]=ch[j--];
else
temp[k--]=ch[l--];
}
else if(j>mid){
temp[k--]=ch[j--];
}
else{
temp[k--]=ch[l--];
}
}
for(;n>=i; n--){
ch[n]=temp[n];
}
}
OUTPUT :-
CODE:-
/* Sumit Pal Singh - 63 - B
Q8. Using circular linked list allocate time slots of 10ms for given processes in time
sharing Environment and then print which process will be completed in how much time
*/
#include<stdio.h>
#include<stdlib.h>
//STRUCTURE OF LINKEDLIST
typedef struct linkedlist{
int id,time;
struct linkedlist *next;
}node;
node * createNode();
node * insert(node*);
node * delete(node*);
node * process(node *);
void main(){
node *head=NULL;
int ch;
do{
printf("1 > insert\n2 > process start\n");
scanf("%d",&ch);
switch(ch){
case 1 :
head = insert(head); break;
case 2 :
if(head!=NULL) head = process(head->next); break;
default :
printf("Invalid Input");
}
printf("\ndo you want to continue (press 1) : ");
scanf("%d",&ch);
}while(ch==1);
}
//CREATE NEW NODE AND INSERT DATA
node* createNode(){
static int id=1;
node *temp=malloc(sizeof(node));
printf("cpu burst : ");
scanf("%d",&temp->time);
temp->id=id++;
temp->next=NULL;
return temp;
}
//INSERT NEW NODE IN CIRCULAR LINKEDLIST
node* insert(node *head){
node *temp=createNode();
if(head==NULL){
temp->next = temp;
return temp;
}
temp->next = head->next;
head->next = temp;
head = temp;
return head;
}
//DELETE AN ELEMENT FROM LINKEDLIST
node* delete(node *head){
node *curr=head;
if(head==NULL) return head;
if(head == head->next){
free(head);
return NULL;
}
curr = curr->next;
head->id = curr->id;
head->time=curr->time;
head->next = curr->next;
free(curr);
return head;
}
//PROCESS ONE BY ONE ALL PRECESS AND DISPLAY COMPLETION TIME
node* process(node *head){
int time_quantum = 10, curr_time = 0;
while(head!=NULL){
if(head->time<=time_quantum){
curr_time+=head->time;
printf("process id : %d\n",head->id);
printf("process completed : %dms\n",curr_time);
head = delete(head);
}
else{
curr_time+=time_quantum;
head->time-=time_quantum;
head = head->next;
}
}
return head;
}
OUTPUT :-
CODE :-
/* Sumit Pal Singh - 63 - B
Q9. Write a C program to store the details of a weighted graph (Use array of pointers
concept).
*/
#include<stdio.h>
#include<stdlib.h>
#define max 100
// GRAPH STRUCT WHICH STORE NODE VALUE & WEIGHT FROM SOURCE
typedef struct graph_type{
int val,weight;
struct graph_type *next;
}node;
// INSERT SOURCE AND DESTINATION IN GRAPH
void insert(node *[], int , int, int);
// DISPLAY ALL LINKEDLIST ELEMENT OF GRAPH
void display(node *, int);
// STARTING POINT OF PROGRAM
void main(){
node *graph[max]={NULL}; //ARRAY OF GRAPH POINTER
int ch,src,dst;
do{
printf("1 > insert\n2 > display\n");
scanf("%d",&ch);
switch(ch){
case 1 :{
printf("enter source \tenter destination \tenter weight\n");
scanf("%d%d%d",&src,&dst,&ch);
insert(graph, src, dst, ch);
break;
}
case 2 :
for(ch=0; ch<max; ch++){
if(graph[ch]!=NULL){
display(graph[ch], ch);
printf("\n");
}
}
break;
default :
printf("Invalid Input");
}
printf("\ndo you want to continue (press 1) : ");
scanf("%d",&ch);
}while(ch==1);
}
void insert(node *graph[], int src, int dst, int weight){
node *t1 = malloc(sizeof(node));
node *t2 = malloc(sizeof(node));
//ADD DESTINATION VALUE AND WEIGHT AT SOURCE
t1->val = dst;
t1->weight=weight;
t1->next = graph[src];
graph[src]=t1;
//ADD SOURCE VALUE AND WEIGHT AT DESTINATION BECAUSE OF BI-
DIRECTIONAL
t2->val =src;
t2->weight=weight;
t2->next= graph[dst];
graph[dst]=t2;
}
void display(node *head, int src){
while(head!=NULL){
//DISPLAY SORUCE -> DESTINATION (WEIGHT)
printf("%d --> %d (weight : %d)\n",src,head->val,head->weight);
head = head->next;
}
}
OUTPUT:-
CODE :l-
/* Sumit Pal Singh - 63 - B
Q10. Write a C program to implement DFS.
*/
#include<stdio.h>
#include<stdlib.h>
#define max 100
// GRAPH STRUCT WHICH STORE NODE VALUE FROM SOURCE
typedef struct graph_type{
int val;
struct graph_type *next;
}node;
// INSERT SOURCE AND DESTINATION IN GRAPH
void insert(node *[], int , int);
// IMPLEMENTATION OF DFS
void dfs(node *[], int[], int);
// STARTING POINT OF PROGRAM
void main(){
node *graph[max]={NULL}; //ARRAY OF GRAPH POINTER
int visited[max]={0};
int ch,src,dst;
do{
printf("1 > insert\n2 > display\n");
scanf("%d",&ch);
switch(ch){
case 1 :{
printf("enter source \tenter destination\n");
scanf("%d%d",&src,&dst);
insert(graph, src, dst); break;
}
case 2 :
for(ch=0; ch<max; ch++){
visited[ch]=0;
}
src = 1;
for(ch=0; ch<max; ch++){
if(!visited[ch] && graph[ch]!=NULL){
printf("graph no. %d :- \n",src++);
dfs(graph, visited, ch);
printf("graph end\n");
}
}
break;
default :
printf("Invalid Input");
}
printf("\ndo you want to continue (press 1) : ");
scanf("%d",&ch);
}while(ch==1);
}
void insert(node *graph[], int src, int dst){
node *t1 = malloc(sizeof(node));
node *t2 = malloc(sizeof(node));
//ADD DESTINATION VALUE AT SOURCE
t1->val = dst;
t1->next = graph[src];
graph[src]=t1;
//ADD SOURCE VALUE AT DESTINATION BECAUSE OF BI-DIRECTIONAL
t2->val =src;
t2->next= graph[dst];
graph[dst]=t2;
}
//DFS FUNCTION TO TRAVERSE ENTIRE GRAPH BY RECURSION
void dfs(node *graph[], int visited[], int v){
node *curr=graph[v];
int u,w;
if(visited[v]) return;
visited[v]=1;
while(curr!=NULL){
u = curr->val;
printf("%d --> %d\n",v,u);
if(!visited[u]){
dfs(graph, visited, u);
}
curr = curr->next;
}
}
OUTPUT :-

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