DS Programs
DS Programs
node*));
#include<stdio.h> printf("Enter your data: ");
#include<stdlib.h> scanf("%d",&new->data);
void inatend(); new->next=NULL;
void inatmid(); if(head==NULL){
void display(); head=new;
void inatbeg(); }
void delbeg(); else{
void delmid(); new->next=head;
void delend(); head=new;
void search(); }
struct node{ }
int data; void inatend(){
struct node* next; new=(struct node*)malloc(sizeof(struct
}*head=NULL,*temp,*new,*temp1; node*));
void main(){ printf("Enter your data: ");
int ch; scanf("%d",&new->data);
while(1){ new->next=NULL;
printf("\nEnter your choice:\n1-Insert at if(head==NULL){
end\n2-Insert at beginning\n3-Insert in head=new;
middle\n4-Delete at beginning\n5- }
Delete in middle\n6-Delete at end\n7- else{
Display\n8-Search\n9-Exit\n"); temp=head;
scanf("%d",&ch); while(temp->next!=NULL){
switch(ch){ temp=temp->next;
case 1:inatend(); }
break; temp->next=new;
case 2:inatbeg(); }
break; }
case 3:inatmid(); void inatmid(){
break; if(head==NULL)
case 4:delbeg(); printf("List is empty\n");
break; else{
case 5:delmid(); int l;
break; printf("Enter the location to insert a
case 6:delend(); node: ");
break; scanf("%d",&l);
case 7:display(); if(l<=1)
break; printf("Invalid location\n");
case 8:search(); else{
break; new=(struct node*)malloc(sizeof(struct
case 9:exit(0); node*));
break; printf("Enter your data: ");
default:printf("Enter a proper scanf("%d",&new->data);
choice\n"); new->next=NULL;
} temp=head;
} for(int i=1;i<l-1;i++){
} temp=temp->next;
void inatbeg(){ if(temp==NULL){
printf("Invalid location\n");
return; return;
} }
} }
new->next=temp->next; temp1->next=temp->next;
temp->next=new; free(temp);
} }
} }
} }
void delbeg(){ void display(){
if(head==NULL) if(head==NULL){
printf("List is empty\n"); printf("List is empty\n");
else{ }
temp=head; else{
head=temp->next; temp=head;
free(temp); while(temp!=NULL){
} printf("%d->",temp->data);
} temp=temp->next;
void delend(){ }
if(head==NULL) printf("\n");
printf("List is empty\n"); }
temp=head; }
if(temp->next==NULL){ void search(){
free(temp); if(head==NULL){
head==NULL; printf("List is empty\n");
} }
else{ else{
while(temp->next!=NULL){ int e;
temp1=temp; int f=0;
temp=temp->next; printf("Enter the element to search: ");
} scanf("%d",&e);
temp1->next=NULL; temp=head;
free(temp); int i=1;
} while(temp!=NULL){
} if(temp->data==e){
void delmid(){ printf("Data is found at position:
if(head==NULL) %d\n",i);
printf("List is empty\n"); f=1;
else{ return;
int l; }
printf("Enter the position you want to i=i+1;
delete: "); temp=temp->next;
scanf("%d",&l); }
temp=head; printf("Data not found\n");
if(l<=1) }
printf("Invalid location!\n"); }
else{
for(int i=1;i<l;i++){
temp1=temp;
temp=temp->next;
if(temp==NULL){
printf("Invalid location\n");
DLLIST printf("Enter your data: ");
scanf("%d",&new->data);
#include<stdio.h> new->prev=NULL;
#include<stdlib.h> new->next=NULL;
void inatend(); if(head==NULL){
void inatmid(); head=new;
void display(); tail=head;
void inatbeg(); }
void delbeg(); else{
void delmid(); new->next=head;
void delend(); head->prev=new;
void search(); head=new;
struct node{ }
int data; }
struct node* prev; void inatend(){
struct node* next; new=(struct node*)malloc(sizeof(struct
}*head=NULL,*tail,*temp,*temp1,*new; node*));
void main(){ printf("Enter your data: ");
int ch; scanf("%d",&new->data);
while(1){ new->prev=NULL;
printf("\nEnter your choice:\n1-Insert at new->next=NULL;
end\n2-Insert at beginning\n3-Insert in if(head==NULL){
middle\n4-Delete at beginning\n5- head=new;
Delete in middle\n6-Delete at end\n7- tail=head;
Display\n8-Search\n9-Exit\n"); }
scanf("%d",&ch); else{
switch(ch){ tail->next=new;
case 1:inatend(); new->prev=tail;
break; tail=new;
case 2:inatbeg(); }
break; }
case 3:inatmid(); void inatmid(){
break; if(head==NULL)
case 4:delbeg(); printf("List is empty\n");
break; else{
case 5:delmid(); int l;
break; printf("Enter the location to insert a
case 6:delend(); node: ");
break; scanf("%d",&l);
case 7:display(); if(l<=1)
break; printf("Invalid location\n");
case 8:search(); else{
break; new=(struct node*)malloc(sizeof(struct
case 9:exit(0); node*));
break; printf("Enter your data: ");
default:printf("Enter a proper scanf("%d",&new->data);
choice\n"); new->next=NULL;
}}} temp=head;
void inatbeg(){ for(int i=1;i<l;i++){
new=(struct node*)malloc(sizeof(struct temp1=temp;
node*)); temp=temp->next;
if(temp==NULL){ if(l<=1)
printf("Invalid location\n"); printf("Invalid location!\n");
return; else{
} temp=head;
} for(int i=1;i<l;i++){
temp1->next=new; temp=temp->next;
new->next=temp; if(temp==NULL){
new->prev=temp1; printf("Invalid location\n");
temp->prev=new; return;
}}} }}
void delbeg(){ temp->next->prev=temp->prev;
if(head==NULL) temp->prev->next=temp->next;
printf("List is empty\n"); free(temp);
else{ }}}
if(head==tail){ void display(){
head=NULL; if(head==NULL){
tail=NULL; printf("List is empty\n");
free(temp); }
} else{
else{ temp=head;
temp=head; while(temp!=NULL){
head=head->next; printf("%d<->",temp->data);
head->prev=NULL; temp=temp->next;
free(temp); }
} printf("\n");
} }
} }
void delend(){ void search(){
if(head==NULL) if(head==NULL)
printf("List is empty\n"); printf("List is empty\n");
else{ else{
temp=head; int e;
if(head==tail){ printf("Enter the element to search: ");
head=NULL; scanf("%d",&e);
tail=NULL; temp=head;
free(temp); int i=1;
} while(temp!=NULL){
else{ if(temp->data==e){
temp=tail; printf("Data is found at position:
tail=tail->prev; %d\n",i);
tail->next=NULL; return;
free(temp); }
}}} i=i+1;
void delmid(){ temp=temp->next;
if(head==NULL) }
printf("List is empty\n"); printf("Data not found\n");
else{ }
int l; }
printf("Enter the position you want to
delete: ");
scanf("%d",&l);
CLLIST new=(struct node*)malloc(sizeof(struct
node*));
#include<stdio.h> printf("Enter your data: ");
#include<stdlib.h> scanf("%d",&new->data);
void inatend(); new->next=NULL;
void inatmid(); if(head==NULL){
void display(); head=new;
void inatbeg(); tail=head;
void delbeg(); head->next=head;
void delmid(); }
void delend(); else{
void search(); new->next=head;
struct node{ head=new;
int data; tail->next=head;
struct node* next; }
}*head=NULL,*tail,*temp,*temp1,*new; }
void main(){ void inatend(){
int ch; new=(struct node*)malloc(sizeof(struct
while(1){ node*));
printf("\nEnter your choice:\n1-Insert at printf("Enter your data: ");
end\n2-Insert at beginning\n3-Insert in scanf("%d",&new->data);
middle\n4-Delete at beginning\n5- new->next=NULL;
Delete in middle\n6-Delete at end\n7- if(head==NULL){
Display\n8-Search\n9-Exit\n"); head=new;
scanf("%d",&ch); tail=head;
switch(ch){ head->next=head;
case 1:inatend(); }
break; else{
case 2:inatbeg(); new->next=head;
break; tail->next=new;
case 3:inatmid(); tail=new;
break; }
case 4:delbeg(); }
break; void inatmid(){
case 5:delmid(); if(head==NULL)
break; printf("List is empty\n");
case 6:delend(); else{
break; int l;
case 7:display(); printf("Enter the location to insert a
break; node: ");
case 8:search(); scanf("%d",&l);
break; if(l<=1)
case 9:exit(0); printf("Invalid location\n");
break; else{
default:printf("Enter a proper new=(struct node*)malloc(sizeof(struct
choice\n"); node*));
} printf("Enter your data: ");
} scanf("%d",&new->data);
} new->next=NULL;
void inatbeg(){ temp=head;
for(int i=1;i<l-1;i++){
temp=temp->next; printf("Invalid location!\n");
} else{
new->next=temp->next; for(int i=1;i<l;i++){
temp->next=new; temp1=temp;
} temp=temp->next;
} }
} temp1->next=temp->next;
void delbeg(){ free(temp);
if(head==NULL) }
printf("LIst is empty\n"); }
else{ }
temp=head; void display(){
if(head==head->next){ if(head==NULL)
head=NULL; printf("Circle list is empty\n");
free(temp); else{
return; temp=head;
} while(temp->next!=head){
head=temp->next; printf("%d~",temp->data);
tail->next=head; temp=temp->next;
free(temp); }
} printf("%d",temp->data);
} }
void delend(){ }
if(head==NULL) void search(){
printf("LIst is empty\n"); if(head==NULL){
else{ printf("Circle list is empty\n");
temp=head; }
if(head==head->next){ else{
head=NULL; int e;
free(temp); printf("Enter the element to search: ");
return; scanf("%d",&e);
} temp=head;
while(temp->next!=tail){ int i=1;
temp=temp->next; while(temp->next!=head){
} if(temp->data==e){
temp1=tail; printf("Data is found at position:
tail=temp; %d\n",i);
tail->next=head; return;
free(temp1); }
} i=i+1;
} temp=temp->next;
void delmid(){ }
if(head==NULL) if(temp->data==e){
printf("List is empty\n"); printf("Data is found at position:
else{ %d\n",i);
int l; }
printf("Enter the position you want to else{
delete: "); printf("Data not found\n");
scanf("%d",&l); }
temp=head; }
if(l<=1) }
QUEUE ARRAY f=f+1;
}
#include<stdio.h> }
#include<stdlib.h> void display(){
void insert(); if(f==-1){
void delete(); printf("Queue is empty\n");
void display(); }
int q[100],f=-1,r=-1,n; else{
void main(){ printf("\n");
int ch; while(f<=r){
printf("Enter the size of array: "); printf("%d\n",q[f]);
scanf("%d",&n); f=f+1;
while(1){ }
printf("\nEnter your choice:\n1- }
Insert\n2-Delete\n3-Display\n4-Exit\n"); }
scanf("%d",&ch);
switch(ch){
case 1:insert();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:exit(0);
break;
default: printf("Enter a proper
choice\n");
}
}
}
void insert(){
if(r==n-1){
printf("The Stack is full\n");
}
else{
f=0;
r=r+1;
printf("Enter an element: ");
scanf("%d",&q[r]);
}
}
void delete(){
if(f==-1){
printf("Queue is empty\n");
}
if(f>=r+1){
f=-1;
r=-1;
}
else{
printf("Deleted element is: %d",q[f]);
QUEUE LIST f=f->next;
free(temp);
#include<stdio.h> }
#include<stdlib.h> }
void insert(); void display(){
void delete(); if(f==NULL){
void display(); printf("Queue is empty\n");
struct node{ }
int data; else{
struct node* next; temp=f;
}*f=NULL,*r,*temp,*new; printf("\n");
void main(){ while(temp!=NULL){
int ch; printf("%d\n",temp->data);
while(1){ temp=temp->next;
printf("\n1-Insert\n2-Delete\n3- }
Display\n4-Exit\nEnter your choice: "); }
scanf("%d",&ch); }
switch(ch){
case 1:insert();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:exit(0);
break;
default:printf("Enter a proper
choice\n");
}
}
}
void insert(){
new=(struct node*)malloc(sizeof(struct
node*));
printf("Enter your data: ");
scanf("%d",&new->data);
new->next=NULL;
if(f==NULL){
f=new;
r=f;
}
else{
r->next=new;
r=r->next;
}
}
void delete(){
if(f==NULL)
printf("Queue is empty\n");
else{
temp=f;
STACK ARRAY STACK LIST
#include<stdio.h> #include<stdio.h> #include<stdlib.h>
#include<stdlib.h> void push();
void push(); void pop();
void pop(); void display();
void display(); struct node{
int s[100],top=-1,n; int data;
void main(){ struct node* next;
int ch; }*top=NULL,*temp,*new;
printf("Enter the size of array: "); void main(){
scanf("%d",&n); int ch;
while(1){ while(1){
printf("\nEnter your choice:\n1- printf("\nEnter your choice:\n1-
Push\n2-Pop\n3-Display\n4-Exit\n"); Push\n2-Pop\n3-Display\n4-Exit\n");
scanf("%d",&ch); scanf("%d",&ch);
switch(ch){ switch(ch){
case 1:push(); case 1:push(); break;
break; case 2:pop(); break;
case 2:pop(); case 3:display(); break;
break; case 4:exit(0); break;
case 3:display(); default:printf(“proper choice?\n");
break; }}}
case 4:exit(0); void push(){
break; new=(struct node*)malloc(sizeof(struct
default: printf("Enter a proper node*));
choice\n"); printf("Enter your data: ");
}}} scanf("%d",&new->data);
void push(){ new->next=NULL;
if(top==n-1){ if(top==NULL){
printf("The Stack is full\n"); top=new;
}else{ }else{
top=top+1; new->next=top; top=new;
printf("Enter an element: "); }}
scanf("%d",&s[top]); void pop(){
}} if(top==NULL){
void pop(){ printf("Stack is empty\n");
if(top==-1){ }else{
printf("Stack is empty\n"); temp=top; top=top->next;
}else{ free(temp);
top=top-1; }}
}} void display(){
void display(){ if(top==NULL){
if(top==-1){ printf("Stack is empty\n");
printf("Stack is empty\n"); }else{
} temp=top;
else{ while(temp->next!=NULL){
printf("\n"); printf("%d\n",temp->data);
for(int i=top;i>=0;i--){ temp=temp->next;
printf("%d\n",s[i]); }
}}} printf("%d\n",temp->data);
}}
BST case 4:
printf("Preorder traversal: ");
preorder(root);
#include<stdio.h> printf("\n");
#include<stdlib.h> break;
struct node { case 5:
int data; printf("Postorder traversal: ");
struct node *right; postorder(root);
struct node *left; printf("\n");
}; break;
struct node* insertnode(struct node case 6:
*root, int val); exit(0);
struct node* getnode(int val); default:
void inorder(struct node *root); printf("Enter a proper choice\n");
void preorder(struct node *root); }
void postorder(struct node *root); }
struct node* delnode(struct node *root, }
int val);
struct node* getnode(int val) {
void main() { struct node *newNode = (struct
int ch; node*)malloc(sizeof(struct node));
int val; newNode->data = val;
struct node *root = NULL; newNode->left = NULL;
while (1) { newNode->right = NULL;
printf("\n1-Insert node\n2-Delete return newNode;
node\n3-Inorder\n4-Preorder\n5- }
Postorder\n6-Exit\nEnter your choice:
"); struct node* insertnode(struct node
scanf("%d", &ch); *root, int val) {
switch (ch) { if (root == NULL) {
case 1: return getnode(val);
printf("Performing insertion\n"); }
printf("Enter data: "); if (root->data > val) {
scanf("%d", &val); root->left = insertnode(root->left, val);
root = insertnode(root, val); }
break; else if (root->data < val) {
case 2: root->right = insertnode(root->right,
if (root == NULL) { val);
printf("BST is empty\n"); }
} return root;
else { }
printf("Enter the value to delete: ");
scanf("%d", &val); void inorder(struct node *root) {
root = delnode(root, val); if (root == NULL)
} return;
break; inorder(root->left);
case 3: printf("%d ", root->data);
printf("Inorder traversal: "); inorder(root->right);
inorder(root); }
printf("\n");
break; void preorder(struct node *root) {
if (root == NULL)
return;
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}