DS Programs
DS Programs
DS Programs
CSE C
1. Program for array operations
#include<stdio.h>
int i,n,arr[30],pos;
void create()
{
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter array elements: \n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("Array created.\n");
}
void display()
{
printf("The array elements are: \n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
printf("\n");
}
void insert()
{
int ele;
printf("Enter the element to be inserted and its position: \n");
scanf("%d %d",&ele,&pos);
if(pos>n || pos<0)
{
printf("Inavalid position.\n");
return;
}
printf("The element inserted into the array is %d at location %d\n",ele,pos);
for(i=n-1;i>=pos;i--)
{
arr[i+1]=arr[i];
}
arr[pos]=ele;
n++;
printf("The array after insertion is: \n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
printf("\n");
return;
}
void delete_b()
{
pos=0;
printf("The element deleted from the array is %d\n",arr[0]);
for(i=pos;i<n;i++)
{
arr[i]=arr[i+1];
}
n--;
printf("The array after deletion is: \n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
printf("\n");
return;
}
void delete_e()
{
pos=n-1;
printf("The element deleted from the array is %d\n",arr[pos]);
for(i=pos;i<n;i--)
{
arr[i]=arr[i+1];
}
n--;
printf("The array after deletion is: \n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
printf("\n");
return;
}
void delete()
{
int ele;
printf("Enter the element to be deleted: \n");
scanf("%d",&ele);
for(i=0;i<n;i++)
{
if(arr[i]==ele)
{
pos=i;
}
}
if(pos>n || pos<0)
{
printf("Inavalid position.\n");
return;
}
printf("The element deleted from the array is %d at location %d\n",ele,pos);
for(i=pos;i<n;i++)
{
arr[i]=arr[i+1];
}
n--;
printf("The array after deletion is: \n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
printf("\n");
return;
}
void main()
{
int ch;
for( ; ; )
{
printf("\nWelcome to Array Operations\n");
printf("1. Create an array.\n");
printf("2. Display the array.\n");
printf("3. Insert an element into the array.\n");
printf("4. Delete an element from the array at beginning.\n");
printf("5. Delete an element from the array at end.\n");
printf("6. Delete an element from the array from specific position.\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: if(n==0)
{
printf("Array is empty\n");
}
else
{
display();
}
break;
case 3: if(n==0)
{
printf("Array is empty\n");
}
else
{
insert();
}
break;
case 4: if(n==0)
{
printf("Array is empty\n");
}
else
{
delete_b();
}
break;
case 5: if(n==0)
{
printf("Array is empty\n");
}
else
{
delete_e();
}
break;
case 6: if(n==0)
{
printf("Array is empty\n");
}
else
{
delete();
}
break;
}
}
}
#include<stdio.h>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
int i=0,top=-1;
int op1,op2,res,s[20];
char postfix[50], symbol;
void push(int item)
{
top++;
s[top]=item;
}
int pop()
{
int item;
item=s[top--];
return item;
}
void main()
{
printf("Enter a valid postfix expression:\n");
scanf("%s",postfix);
for(i=0; postfix[i]!=’\0’;i++)
{
symbol=postfix[i];
//Here symbol is read by its ASCII value not digit value
if(isdigit(symbol))
{
push(symbol-’0’);
}
else
{
op2=pop();
op1=pop();
switch(symbol)
{
case ’+’: push(op1+op2);
break;
case ’$’:
#include<stdio.h>
#include<stdlib.h>
int s[10],max=5,top=-1,i,n,ele;
void push();
void pop();
void display();
void main()
{
int ch;
while(1)//Infinite loop condition instead of for(;;)
{
printf("\nWelcome to Stack Operations\n");
printf("1. Push Operation\n");
printf("2. Pop Operation\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: printf("Exiting...\n");
exit(0);
}
}
}
void push()
{
if(top==max-1)
{
printf("Stack Overflow\n");
return;
}
else
{
printf("Enter the element to be pushed into stack\n");
scanf("%d",&ele);
top++;
s[top]=ele;
printf("Element %d pushed\n",ele);
return;
}
}
void pop()
{
if(top==-1)
{
printf("Stack Underflow\n");
return;
}
else
{
ele=s[top];
top--;
printf("Element popped is %d\n",ele);
return;
}
}
void display()
{
if(top==-1)
{
printf("Stack is empty\n");
}
else
{
printf("Elements of Stack are: \n");
for(i=top;i>=0;i--)
{
printf("%d\t",s[i]);
}
printf("\n");
}
return;
}
#include<stdio.h>
#include<stdlib.h>
#define max 5
int f=0,r=-1,ch,q[max],ele,i;
void insert();
void delete();
void display();
void main()
{
while(1)
{
printf("\nWelcome to Queue Operations\n");
printf("1. Insert element to queue\n");
printf("2. Delete element from queue\n");
printf("3. Display elements of queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: printf("Exiting...\n");
exit(0);
}
void delete()
{
if(f>r)
{
printf("Queue underflow\n");
return;
}
else
{
ele=q[f];
printf("Element deleted from queue is %d \n",ele);
f++;
}
}
void display()
{
if(f>r)
{
printf("Queue is empty\n");
}
else
{
printf("The elements of queue are: \n");
for(i=f;i<=r;i++)
{
printf("%d\t",q[i]);
}
}
return;
}
#include<stdio.h>
#include<stdlib.h>
#define max 5
int f=0,r=-1,count=0,ch,q[max],ele,i;
void insert();
void delete();
void display();
void main()
{
while(1)
{
printf("\nWelcome to Queue Operations\n");
printf("1. Insert element to queue\n");
printf("2. Delete element from queue\n");
printf("3. Display elements of queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: printf("Exiting...\n");
exit(0);
}
void delete()
{
if(count==0)
{
printf("Queue underflow\n");
return;
}
else
{
ele=q[f];
printf("Element deleted from queue is %d \n",ele);
f=(f+1)%max;
count--;
}
}
void display()
{
if(count==0)
{
printf("Queue is empty\n");
}
else
{
printf("The elements of queue are: \n");
for(i=f;i!=r;i=(i+1)%max)
{
printf("%d\t",q[i]);
}
printf("%d\n",q[i]);
}
return;
}
#include<stdio.h>
#include<stdlib.h>
#define max 5
int f=0,r=-1,ch,q[max],ele,i;
void insert_rear();
void insert_front();
void delete_front();
void delete_rear();
void display();
void main()
{
while(1)
{
printf("\nWelcome to Queue Operations\n");
printf("1. Insert element to queue at rear end\n");
printf("2. Insert element to queue at front end\n");
printf("3. Delete element from queue at front end\n");
printf("4. Delete element from queue at rear end\n");
printf("5. Display elements of queue\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_rear();
break;
case 2: insert_front();
break;
case 3: delete_front();
break;
case 4: delete_rear();
break;
case 5: display();
break;
case 6: printf("Exiting...\n");
exit(0);
}
void insert_front()
{
if(f==0 && r==-1)
{
printf("Enter the element to be inserted into queue: ");
scanf("%d",&ele);
r++;
q[r]=ele;
printf("Element %d inserted\n",ele);
return;
}
if(f!=0)
{
printf("Enter the element to be inserted into queue: ");
scanf("%d",&ele);
f--;
q[f]=ele;
printf("Element %d inserted\n",ele);
return;
}
else
{
printf("Front insertion not possible\n");
return;
}
}
void delete_front()
{
if(f>r)
{
printf("Queue underflow\n");
return;
}
else
{
ele=q[f];
printf("Element deleted from queue is %d \n",ele);
f++;
}
if(f>r)
{
f=0;
r=-1;
}
}
void delete_rear()
{
if(f>r)
{
printf("Queue underflow\n");
return;
}
else
{
ele=q[r];
printf("Element deleted from queue is %d \n",ele);
r--;
}
if(f>r)
{
f=0;
r=-1;
}
}
void display()
{
if(f>r)
{
printf("Queue is empty\n");
}
else
{
printf("The elements of queue are: \n");
for(i=f;i<=r;i++)
{
printf("%d\t",q[i]);
}
}
return;
}
#include<stdio.h>
#include<stdlib.h>
#define max 5
int f=0,r=-1,ch,q[max],ele,i,j;
void insert();
void delete();
void display();
void main()
{
while(1)
{
printf("\nWelcome to Priority Queue Operations\n");
printf("1. Insert element to queue\n");
printf("2. Delete element from queue\n");
printf("3. Display elements of queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: printf("Exiting...\n");
exit(0);
}
void delete()
{
if(f>r)
{
printf("Queue underflow\n");
return;
}
else
{
ele=q[f];
printf("Element deleted from queue is %d \n",ele);
f++;
}
}
void display()
{
if(f>r)
{
printf("Queue is empty\n");
}
else
{
printf("The elements of queue are: \n");
for(i=f;i<=r;i++)
{
printf("%d\t",q[i]);
}
}
return;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char str[40],pat[40];
void failure(int f[],char p[])
{
int i=0,j=1;
f[i]=0;
while(j<strlen(p))
{
if(p[i]==p[j])
{
f[j]=i+1;
i++;
j++;
}
else if(i==0)
{
f[j++]=i;
}
else
i=f[j-1];
}
}
int pat_match(char p[],char t[],int f[])
{
int i=0,j=0;
while(i<strlen(t) && j<strlen(p))
{
if(t[i]==p[j])
{
i++;
j++;
}
else if(j==0)
i++;
else
j=f[j-1];
}
if(j==strlen(p))
return i-strlen(p);
else
return -1;
}
void main()
{
int f[40];
printf("Enter the string : \n");
gets(str);
printf("Enter the pattern: \n");
scanf(" %s",pat);
failure(f,pat);
int a;
a=pat_match(pat,str,f);
if(a==-1)
{
printf("Pattern not found\n");
}
else
{
printf("Pattern found at position %d\n",a+1);
}
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node * link;
};
typedef struct node * NODE;
NODE first;
int count=0;
NODE getnode()
{
NODE newnode;
newnode=(NODE)malloc(sizeof(struct node));
printf("Enter the element to be inserted: ");
scanf("%d",&newnode->info);
newnode->link=NULL;
count++;
return newnode;
}
void insert_front()
{
NODE temp;
temp=getnode();
temp->link=first;
first=temp;
printf("Element %d inserted into node\n",temp->info);
return;
}
void insert_rear()
{
NODE cur,temp,prev;
temp=getnode();
cur=first;
prev=NULL;
while(cur->link!=NULL)
{
prev=cur;
cur=cur->link;
}
cur->link=temp;
printf("Element %d inserted into node\n",temp->info);
return;
}
void display()
{
NODE cur;
if(first==NULL)
{
printf("Linked list empty\n");
return;
}
cur=first;
printf("\nThere are %d nodes in linked list.\n",count);
printf("Elements of linked list are: \n");
while(cur!=NULL)
{
printf("%d\t",cur->info);
cur=cur->link;
}
printf("\n");
return;
}
void delete_front()
{
NODE temp;
if(first==NULL)
{
printf("Linked list empty\n");
return;
}
if(first->link==NULL)
{
printf("Only 1 node exists. Element deleted is %d\n",first->info);
free(first);
first=NULL;
count--;
return;
}
temp=first;
first=first->link;
printf("Element deleted is %d\n",temp->info);
free(temp);
temp=NULL;
count--;
return;
}
void delete_rear()
{
NODE cur,prev;
if(first==NULL)
{
printf("Linked list empty\n");
return;
}
if(first->link==NULL)
{
printf("Only 1 node exists. Element deleted is %d\n",first->info);
free(first);
first=NULL;
count--;
return;
}
cur=first;
prev=NULL;
while(cur->link!=NULL)
{
prev=cur;
cur=cur->link;
}
prev->link=NULL;
printf("Element deleted is %d\n",cur->info);
free(cur);
cur=NULL;
count--;
return;
}
void search()
{
printf("Enter the element\n");
int num;
scanf("%d",&num);
NODE cur;
cur=first;
for(int i=1;cur!=NULL;i++)
{
if(cur->info==num)
{
printf("Element %d found at position %d\n",num,i);
return;
}
else
cur=cur->link;
}
printf("Element %d not found.\n",num);
return;
}
void insert_pos()
{
int pos;
printf("Enter the position: ");
scanf("%d",&pos);
if(pos>count || pos<=1)
{
printf("Invalid position\n");
return;
}
NODE cur,temp,prev=NULL;
cur=first;
for(int i=1;i<=count;i++)
{
if(i==pos)
{
temp=getnode();
temp->link=prev->link;
prev->link=temp;
printf("Element %d inserted at position %d\n",temp->info,pos);
return;
}
else
{
prev=cur;
cur=cur->link;
}
}
}
void delete_pos()
{
int pos;
printf("Enter the position: ");
scanf("%d",&pos);
if(pos>=count || pos<=1)
{
printf("Invalid position\n");
return;
}
NODE cur,prev=NULL;
cur=first;
for(int i=1;i<=count;i++)
{
if(i==pos)
{
prev->link=cur->link;
printf("Element %d deleted at position %d\n",cur->info,pos);
count--;
free(cur);
cur=NULL;
return;
}
else
{
prev=cur;
cur=cur->link;
}
}
}
void display_pos()
{
int pos;
printf("Enter the position: ");
scanf("%d",&pos);
if(pos>count || pos<1)
{
printf("Invalid position\n");
return;
}
NODE cur;
cur=first;
for(int i=1;i<=count;i++)
{
if(i==pos)
{
printf("Element %d found at position %d\n",cur->info,i);
return;
}
else
cur=cur->link;
}
return;
}
void reverse()
{
NODE cur,temp;
cur=NULL;
while(first!=NULL)
{
temp=first->link;
first->link=cur;
cur=first;
first=temp;
}
first=cur;
printf("Linked list reversed.\n");
display();
return;
}
void main()
{
int ch;
while(1)
{
printf("\n Singly Linked list operations: \n");
printf("1. Insert at front\n");
printf("2. Delete at front\n");
printf("3. Insert at rear\n");
printf("4. Delete at rear\n");
printf("5. Display\n");
printf("6. Search\n");
printf("7. Insert at specific position\n");
printf("8. Delete at specific position\n");
printf("9. Display specific position\n");
printf("10. Reverse the elements order\n");
printf("11. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_front();
break;
case 2: delete_front();
break;
case 3: insert_rear();
break;
case 4: delete_rear();
break;
case 5: display();
break;
case 6: search();
break;
case 7: insert_pos();
break;
case 8: delete_pos();
break;
case 9: display_pos();
break;
case 10: reverse();
break;
case 11: printf("Exiting... \n");
exit(0);
default: printf("Invalid choice\n");
}
}
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
int n;
struct node* llink;
struct node* rlink;
};
typedef struct node* NODE;
NODE first;
int count=0;
NODE getnode()
{
NODE newnode;
int ele;
newnode=(NODE)malloc(sizeof(struct node));
if(newnode == NULL)
{
printf("\nMemory is not available");
return 0;
}
printf("\nEnter the element to be inserted: ");
scanf("%d", &ele);
newnode->info=ele;
printf("Element %d inserted into node\n",ele);
newnode->rlink=NULL;
newnode->llink=NULL;
count++;
newnode->n=count;
return newnode;
}
void insert_front()
{
NODE temp;
temp = getnode();
if(first==NULL)
{
first=temp;
}
else
{
temp->rlink = first;
first->llink=temp;
first=temp;
return;
}
}
void delete_front()
{
NODE temp;
if(first == NULL)
{
printf("\nLinked list is empty");
return;
}
if(first->rlink == NULL)
{
printf(" Node deleted is %d\n",first->info);
free(first);
first=NULL;
return;
}
temp = first;
first = first->rlink;
first->llink=NULL;
printf("Node deleted is %d\n ",temp->info);
free(temp);
return;
}
void display()
{
NODE cur;
if(first == NULL)
{
printf("Linked List empty\n");
return;
}
printf("\nThere are %d nodes in linked list.\n",count);
printf("Elements of linked list are: \n");
cur=first;
while(cur!= NULL)
{
printf("%d\t",cur->info);
cur = cur->rlink;
}
}
void insert_rear()
{
NODE cur,temp;
temp=getnode();
if(first==NULL)
{
first=temp;
first->rlink=NULL;
first->llink=NULL;
return;
}
if(first->rlink==NULL)
{
first->rlink=temp;
temp->llink=first;
return;
}
cur=first;
while(cur->rlink!=NULL)
{
cur=cur->rlink;
}
cur->rlink=temp;
temp->llink=cur;
return;
}
void delete_rear()
{
NODE cur,prev;
if(first==NULL)
{
printf("Linked list empty\n");
return;
}
if(first->rlink==NULL)
{
printf("Node deleted is %d\n",first->info);
free(first);
first=NULL;
return;
}
prev=NULL;
cur=first;
while(cur->rlink!=NULL)
{
prev=cur;
cur=cur->rlink;
}
printf("Node deleted is %d\n",cur->info);
prev->rlink=NULL;
free(cur);
cur=NULL;
return;
}
void insert_pos()
{
int pos;
printf("Enter the position: ");
scanf("%d",&pos);
if(pos>count || pos<=1)
{
printf("Invalid position\n");
return;
}
NODE cur,temp,prev=NULL;
cur=first;
for(int i=1;i<=count;i++)
{
if(i==pos)
{
temp=getnode();
prev->rlink=temp;
temp->rlink=cur;
cur->llink=temp;
temp->llink=prev;
printf("Element %d inserted at position %d\n",temp->info,pos);
return;
}
else
{
prev=cur;
cur=cur->rlink;
}
}
}
void delete_pos()
{
int pos;
printf("Enter the position: ");
scanf("%d",&pos);
if(pos>=count || pos<=1)
{
printf("Invalid position\n");
return;
}
NODE cur,prev=NULL,temp;
cur=first;
for(int i=1;i<=count;i++)
{
if(i==pos)
{
temp=cur->rlink;
temp->llink=prev;
prev->rlink=temp;
printf("Element %d deleted at position %d\n",cur->info,pos);
count--;
free(cur);
cur=NULL;
return;
}
else
{
prev=cur;
cur=cur->rlink;
}
}
}
void display_pos()
{
int pos;
printf("Enter the position: ");
scanf("%d",&pos);
if(pos>count || pos<1)
{
printf("Invalid position\n");
return;
}
NODE cur;
cur=first;
for(int i=1;i<=count;i++)
{
if(i==pos)
{
printf("Element %d found at position %d\n",cur->info,i);
return;
}
else
cur=cur->rlink;
}
return;
}
void search()
{
printf("Enter the element: ");
int num;
scanf("%d",&num);
NODE cur;
cur=first;
for(int i=1;cur!=NULL;i++)
{
if(cur->info==num)
{
printf("Element %d found at position %d\n",num,i);
return;
}
else
cur=cur->rlink;
}
printf("Element %d not found.\n",num);
return;
}
void reverse()
{
printf("\nLink list reversed\n");
printf("Elements of reversed linked list are: \n");
NODE cur=first;
while(cur->rlink!=NULL)
{
cur=cur->rlink;
}
while(cur!=NULL)
{
printf("%d\t",cur->info);
cur=cur->llink;
}
}
void main()
{
int ch;
while(1)
{
printf("\n Doubly Linked List Operations ");
printf("\n1. Insert Node at front");
printf("\n2. Delete Node at front");
printf("\n3. Insert Node at rear");
printf("\n4. Delete Node at rear");
printf("\n5. Display");
printf("\n6. Search");
printf("\n7. Insert at specific position");
printf("\n8. Delete at specific position");
printf("\n9. Display at specific position");
printf("\n10. Reverse the elements order");
printf("\n11. Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1 : insert_front();
break;
case 2 : delete_front();
break;
case 3 : insert_rear();
break;
case 4: delete_rear();
break;
case 5: display();
break;
case 6: search();
break;
case 7: insert_pos();
break;
case 8: delete_pos();
break;
case 9: display_pos();
break;
case 10: reverse();
break;
case 11: printf("Exiting...\n");
exit(0);
default: printf("\n Enter the valid choice");
}
}
}
#include <stdio.h>
#include <stdlib.h>
int count = 0;
struct node
{
int info;
struct node *link;
};
typedef struct node *NODE;
NODE last;
int count;
NODE getnode()
{
int ele;
NODE newnode = (NODE)malloc(sizeof(struct node));
if (newnode == NULL)
{
printf("Insufficient memory\n");
return NULL;
}
printf("Enter the element to be inserted: ");
scanf("%d", &ele);
newnode->info = ele;
count++;
return newnode;
}
void insert_front()
{
NODE temp;
temp = getnode();
if (last == NULL)
{
last = temp;
last->link = temp;
printf("Element %d inserted\n",last->info);
return;
}
temp->link = last->link;
last->link = temp;
printf("Element %d inserted\n",temp->info);
return;
}
void insert_rear()
{
NODE temp = getnode();
if (last == NULL)
{
last = temp;
last->link = last;
printf("Element %d inserted\n",last->info);
return;
}
temp->link = last->link;
last->link = temp;
last = temp;
printf("Element %d inserted\n",temp->info);
return;
}
void delete_front()
{
if (last == NULL)
{
printf("Linked list is empty\n");
return;
}
if (last->link == last)
{
printf("Only 1 node exists. Element deleted is %d\n", last->info);
free(last);
last = NULL;
count--;
return;
}
NODE first;
first = last->link;
last->link = first->link;
printf("Element deleted is %d\n", first->info);
count--;
free(first);
return;
}
void delete_rear()
{
NODE prev;
if (last == NULL)
{
printf("Linked list is empty\n");
}
if (last->link == last)
{
printf("Only 1 node exists. Element deleted is %d\n", last->info);
free(last);
last = NULL;
count--;
return;
}
prev = last->link;
while (prev->link != last)
{
prev = prev->link;
}
printf("Element deleted is %d\n", last->info);
prev->link = last->link;
free(last);
last = prev;
count--;
}
void display()
{
NODE cur;
if (last == NULL)
{
printf("Linked List is empty\n");
return;
}
cur = last->link;
printf("\nThere are %d nodes in linked list\n",count);
printf("Elements of linked list are: \n");
while (cur != last)
{
printf("%d\t", cur->info);
cur = cur->link;
}
printf("%d", last->info);
printf("\n");
return;
}
void search()
{
printf("Enter the element: ");
int num,i;
scanf("%d",&num);
NODE cur;
cur=last->link;
for(i=1;cur!=last;i++)
{
if(cur->info==num)
{
printf("Element %d found at position %d\n",num,i);
return;
}
else
cur=cur->link;
}
if(num==cur->info)
{
printf("Element %d found at position %d\n",num,i);
return;
}
else
{
printf("Element %d not found.\n",num);
return;
}
}
void insert_pos()
{
int pos;
printf("Enter the position: ");
scanf("%d",&pos);
if(pos>count || pos<=1)
{
printf("Invalid position\n");
return;
}
NODE cur,temp,prev=NULL;
cur=last->link;
for(int i=1;i<=count;i++)
{
if(i==pos)
{
temp=getnode();
temp->link=prev->link;
prev->link=temp;
printf("Element %d inserted at position %d\n",temp->info,pos);
return;
}
else
{
prev=cur;
cur=cur->link;
}
}
}
void delete_pos()
{
int pos;
printf("Enter the position: ");
scanf("%d",&pos);
if(pos>=count || pos<=1)
{
printf("Invalid position\n");
return;
}
NODE cur,prev=NULL;
cur=last->link;
for(int i=1;i<=count;i++)
{
if(i==pos)
{
prev->link=cur->link;
printf("Element %d deleted at position %d\n",cur->info,pos);
count--;
free(cur);
cur=NULL;
return;
}
else
{
prev=cur;
cur=cur->link;
}
}
}
void display_pos()
{
int pos;
printf("Enter the position: ");
scanf("%d",&pos);
if(pos>count || pos<1)
{
printf("Invalid position\n");
return;
}
NODE cur;
cur=last->link;
for(int i=1;i<=count;i++)
{
if(i==pos)
{
printf("Element %d found at position %d\n",cur->info,i);
return;
}
else
cur=cur->link;
}
return;
}
void reverse() {
NODE cur, temp, first,prev;
first = last->link;
temp=first->link;
first->link=last;
cur=first;
prev=cur;
while (temp != first)
{
cur=temp;
temp=cur->link;
cur->link=prev;
prev=cur;
}
last=first;
printf("\nLinked list reversed.");
display();
}
void main()
{
int ch;
while (1)
{
printf("\n Circular Singly Linked List Operations ");
printf("\n1. Insert Node at front");
printf("\n2. Delete Node from front");
printf("\n3. Insert Node at rear");
printf("\n4. Delete Node from rear");
printf("\n5. Display");
printf("\n6. Search");
printf("\n7. Insert at specific position");
printf("\n8. Delete at specific position");
printf("\n9. Display specific position");
printf("\n10. Reverse the elements order");
printf("\n11. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1: insert_front();
break;
case 2: delete_front();
break;
case 3: insert_rear();
break;
case 4: delete_rear();
break;
case 5: display();
break;
case 6: search();
break;
case 7: insert_pos();
break;
case 8: delete_pos();
break;
case 9: display_pos();
break;
case 10: reverse();
break;
case 11: printf("Exiting...\n");
exit(0);
default: printf("Invalid choice\n");
}
}
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *rlink;
struct node *llink;
};
int count=0;
typedef struct node * NODE;
NODE first;
NODE getnode()
{
NODE newnode;
newnode=(NODE)malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("Insuffient memory\n");
return 0;
}
newnode->llink=NULL;
newnode->rlink=NULL;
printf("Enter the element to be inserted: ");
scanf("%d",&newnode->info);
return newnode;
}
void insert_front()
{
NODE temp,last;
temp=getnode();
if(first==NULL)
{
first=temp;
first->llink=first;
first->rlink=first;
printf("Element %d inserted\n",first->info);
count++;
return;
}
last=first->llink;
temp->llink=last;
temp->rlink=first;
first->llink=temp;
last->rlink=temp;
first=temp;
printf("Element %d inserted\n",temp->info);
count++;
return;
}
void insert_rear()
{
NODE temp,last;
temp=getnode();
if(first==NULL)
{
first=temp;
first->llink=first;
first->rlink=first;
printf("Element %d inserted\n",first->info);
count++;
return;
}
last=first->llink;
last->rlink=temp;
temp->llink=last;
temp->rlink=first;
first->llink=temp;
printf("Element %d inserted\n",temp->info);
count++;
return;
}
void delete_front()
{
if(first==NULL)
{
printf("Linked list empty\n");
return;
}
if(first->rlink==first && first->llink==first)
{
printf("Only 1 node exists. Element deleted is %d\n",first->info);
count--;
free(first);
first=NULL;
return;
}
NODE cur,temp,last;
cur=first->rlink;
last=first->llink;
last->rlink=cur;
cur->llink=last;
temp=first;
printf("Element deleted is %d\n",first->info);
count--;
first=cur;
return;
}
void delete_rear()
{
NODE prev,last;
if(first==NULL)
{
printf("Linked list empty\n");
return;
}
if(first->rlink==first && first->llink==first)
{
printf("Only 1 node exists. Element deleted is %d\n",first->info);
count--;
free(first);
first=NULL;
return;
}
last=first->llink;
prev=last->llink;
prev->rlink=last->rlink;
first->llink=prev;
printf("Element deleted is %d\n",last->info);
count--;
free(last);
last=NULL;
}
void search()
{
int num,i=1;
NODE cur;
cur=first->rlink;
printf("Enter the element: ");
scanf("%d",&num);
while(cur!=first)
{
i++;
if(cur->info==num)
{
printf("Element %d found at position %d\n",num,i);
return;
}
else
{
cur=cur->rlink;
}
}
if(first->info==num)
{
printf("Element %d found at position 1\n",num);
return;
}
else
{
printf("Element not found\n");
return;
}
}
void display()
{
NODE cur,last;
if(first==NULL)
{
printf("Linked list empty\n");
return;
}
printf("\nThere are %d nodes in linked list\n",count);
printf("Elements of linked list are: \n");
cur=first;
last=first->llink;
while(cur!=last)
{
printf("%d\t",cur->info);
cur=cur->rlink;
}
printf("%d\n",cur->info);
return;
}
void insert_pos()
{
NODE cur,prev,temp;
int pos;
cur=first;
prev=NULL;
printf("Enter the position: ");
scanf("%d",&pos);
if(pos>count || pos<=1)
{
printf("Invalid position\n");
return;
}
else
{
for(int i=1;i<=count;i++)
{
if(i==pos)
{
temp=getnode();
temp->rlink=cur;
cur->llink=temp;
prev->rlink=temp;
temp->llink=prev;
printf("Element %d inserted at position
%d\n",temp->info,i);
count++;
return;
}
else
{
prev=cur;
cur=cur->rlink;
}
}
}
}
void delete_pos()
{
NODE cur,prev,temp;
int pos;
cur=first;
prev=NULL;
printf("Enter the position: ");
scanf("%d",&pos);
if(pos>count || pos<=1)
{
printf("Invalid position\n");
return;
}
else
{
for(int i=1;i<=count;i++)
{
if(i==pos)
{
prev->rlink=cur->rlink;
temp=cur->rlink;
temp->llink=prev;
printf("Element %d deleted at position
%d\n",cur->info,i);
free(cur);
count--;
return;
}
else
{
prev=cur;
cur=cur->rlink;
}
}
}
}
void display_pos()
{
NODE cur;
int pos;
cur=first;
printf("Enter the position: ");
scanf("%d",&pos);
if(pos>count || pos<1)
{
printf("Invalid position\n");
return;
}
else
{
for(int i=1;i<=count;i++)
{
if(i==pos)
{
printf("Element %d found at position
%d\n",cur->info,i);
return;
}
else
{
cur=cur->rlink;
}
}
}
}
void reverse()
{
printf("\nLink list reversed\n");
printf("Elements of reversed linked list are: \n");
NODE cur=first->llink;
while(cur!=first)
{
printf("%d\t",cur->info);
cur=cur->llink;
}
printf("%d\n",first->info);
}
void main()
{
int ch;
while(1)
{
printf("\n Circular Doubly Linked List Operations \n");
printf("1.Insert an element at front\n");
printf("2.Delete an element at front\n");
printf("3.Insert an element at rear\n");
printf("4.Delete an element at rear\n");
printf("5.Display\n");
printf("6.Search\n");
printf("7.Insert at specific position\n");
printf("8.Delete at specific position\n");
printf("9.Display at specific position\n");
printf("10.Reverse the elements order\n");
printf("11.Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch (ch)
{
case 1: insert_front();
break;
case 2: delete_front();
break;
case 3: insert_rear();
break;
case 4: delete_rear();
break;
case 5: display();
break;
case 6: search();
break;
case 7: insert_pos();
break;
case 8: delete_pos();
break;
case 9: display_pos();
break;
case 10: reverse();
break;
case 11: printf("Exiting...\n");
exit(0);
default: printf("Invalid choice\n");
}
}
}
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *link;
};
typedef struct node *NODE;
NODE f1 = NULL, f2 = NULL;
NODE res = NULL;
NODE getnode()
{
NODE new;
new = (NODE)malloc(sizeof(struct node));
if (new == NULL)
{
printf("Memory allocation failed\n");
exit(1);
}
printf("Enter the element: ");
scanf("%d", &new->info);
new->link = NULL;
return new;
}
void display(NODE f)
{
NODE cur;
if (f == NULL)
{
printf("\nLinked list empty\n");
return;
}
cur = f;
printf("\nElements of linked list are: \n");
while (cur != NULL)
{
printf("%d\t", cur->info);
cur = cur->link;
}
printf("\n");
}
void main()
{
int n1, n2;
printf("Enter the number of nodes in first Linked List: ");
scanf("%d", &n1);
for (int i = 0; i < n1; i++)
{
insert_rear(&f1);
}
printf("Linked List 1 created\n");
struct Node
{
int co, xe, ye, ze;
struct Node* link;
};
void attach(int co, int xe, int ye, int ze, NODE* list)
{
NODE temp = (NODE)malloc(sizeof(struct Node));
temp->co = co;
temp->xe = xe;
temp->ye = ye;
temp->ze = ze;
temp->link = NULL;
if (!(*list))
{
*list = temp;
return;
}
NODE cur = (*list);
while (cur->link != NULL)
{
cur = cur->link;
}
cur->link = temp;
}
cur = cur->link;
}
printf("\n");
}
while (A != NULL)
{
attach(A->co, A->xe, A->ye, A->ze, R);
A = A->link;
}
while (B != NULL)
{
attach(B->co, B->xe, B->ye, B->ze, R);
B = B->link;
}
void main()
{
int ch;
printf("\n1.Polynomial operations\n");
printf("2. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
if(ch==1)
{
printf("Enter 1st polynomial\n");
read(&first_polynomial);
printf("Enter 2nd polynomial\n");
read(&second_polynomial);
add(first_polynomial, second_polynomial, &result);
printf("Resultant polynomial is: \n");
display(result);
}
else if(ch==2)
{
printf("Exiting...\n");
exit(0);
}
else
{
printf("Invalid choice\n");
}
}