Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DS Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

Data Structures & Applications

Subject Code: 22CSE134


Programs of IA1

Programs Page No.

1. Program for array operations 2-5


2. Program for conversion of infix to postfix expression 5-6
3. Program for evaluation of postfix expression 6-8
4. Program for stack operations 8-9
5. Program for linear queue operations 9-11
6. Program for circular queue operations 11-13
7. Program for double ended queue operations 13-15
8. Program for priority queue operations 15-17
9. Program for string-pattern matching (Brute force Method) 17-18
10. Program for string-pattern matching (KMP Method) 19-20
11. Program for singly linked list operations 20-26
12. Program for doubly linked list operations 26-32

13. Program for circular linked list operations 32-38


14. Program for circular doubly linked list operations 38-45
15. Program for merging 2 linked lists 45-47
16. Program for addition of polynomials 47-51

Shreyas S Tiko kar

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;

case 7: printf("Exiting the program ...\n");


break;

default: printf("Invalid choice\n");


break;

}
}
}

2. Conversion of infix to postfix expression

#include<stdio.h>
#include<string.h>

int input(char symbol)


{
switch(symbol)
{
case ’+’:
case ’-’:return 1;
case ’*’:
case ’/’:return 3;
case ’^’:
case ’$’:return 6;
case ’(’:return 9;
case ’)’:return 0;
default :return 7;
}
}
int stack(char symbol)
{
switch(symbol)
{
case ’+’:
case ’-’:return 2;
case ’*’:
case ’/’:return 4;
case ’^’:
case ’$’:return 5;
case ’(’:return 0;
case ’#’:return -1;
default :return 8;
}
}
void infix_to_postfix (char infix [], char postfix [])
{
int top,i,j=0;
char s[20],symbol;
top=-1;
s[++top] = ’#’;
for(i=0; i<strlen (infix); i++)
{
symbol=infix[i];
while(stack(s[top])>input(symbol))
{
postfix [j++]=s[top--];
}
if(stack(s[top]) != input(symbol))
s[++top] =symbol;
else
top--;
}
while(s[top] !=’#’)
{
postfix[j++]=s[top--];
}
postfix[j]= ’\0’;
return;
}
void main()
{
char infix [20], postfix [20];
printf ("Enter the infix expression:\n");
scanf ("%s", infix);
infix_to_postfix (infix, postfix);
printf ("The converted postfix expression is: %s\n", postfix);
}

3. Evaluation of postfix expression

#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 ’-’: push(op1-op2);


break;

case ’*’: push(op1*op2);


break;

case ’/’: push(op1/op2);


break;

case ’%’: push(op1%op2);


break;

case ’$’:

case ’^’: push(pow(op1,op2));


break;

default: printf("Invalid Operator\n");


exit(0);
}
}
}
res=pop();
printf("Result= %d\n",res);
}

4. Program for stack operations

#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);

default: printf("Invalid choice\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;
}

5. Program for Linear Queue operations

#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);

default: printf("Invalid choice\n");


return;
}
}
}
void insert()
{
if(r==max-1)
{
printf("Queue overflow\n");
return;
}
else
{
printf("Enter the element to be inserted into queue: ");
scanf("%d",&ele);
r++;
q[r]=ele;
printf("Element %d inserted\n",ele);
return;
}

}
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;
}

6. Program for Circular Queue operations

#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);

default: printf("Invalid choice\n");


return;
}
}
}
void insert()
{
if(count==max)
{
printf("Queue overflow\n");
return;
}
else
{
printf("Enter the element to be inserted into queue: ");
scanf("%d",&ele);
r=(r+1)%max;
q[r]=ele;
printf("Element %d inserted\n",ele);
count++;
return;
}

}
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;
}

7. Program for Double ended Queue operations

#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);

default: printf("Invalid choice\n");


return;
}
}
}
void insert_rear()
{
if(r==max-1)
{
printf("Queue overflow\n");
return;
}
else
{
printf("Enter the element to be inserted into queue: ");
scanf("%d",&ele);
r++;
q[r]=ele;
printf("Element %d inserted\n",ele);
return;
}

}
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;
}

8. Program for Priority Queue operations

#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);

default: printf("Invalid choice\n");


return;
}
}
}
void insert()
{
if(r==max-1)
{
printf("Queue overflow\n");
return;
}
else
{
printf("Enter the element to be inserted into queue: ");
scanf("%d",&ele);
j=r;
while(j>=0 && ele<q[j])
{
q[j+1]=q[j];
j--;
}
q[++j]=ele;
r++;
printf("Element %d inserted\n",ele);
return;
}

}
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;
}

9. Program for String Pattern Matching(Brute force Method)


#include<stdio.h>
#include<string.h>
int i,j,flag,max;
char s[70],p[50];
int pattern_match();
void main()
{
int a;
a=pattern_match();
if(a==-1)
{
printf("The entered pattern not found\n");
}
else
printf("The entered pattern found from position %d\n",a+1);
}
int pattern_match()
{
printf("Enter the main string: \n");
gets(s);
printf("Enter the pattern: \n");
gets(p);
int LS=strlen(s);
int LP=strlen(p);
max=LS-LP+1;
for(i=0;i<=max;i++)
{
flag=1;
for(j=0;j<LP && flag==1;j++)
{
if(p[j]!=s[j+i])
{
flag=0;
}
}
if(flag==1)
{
return i;
}
}
return -1;
}
10. Program for KMP Method of String Pattern Matching

#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);
}
}

11. Program for Singly Linked List Operations

#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");

}
}
}

12.Program for Doubly Linked list Operations

#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");
}
}
}

13. Program for Circular Linked List Operations

#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");
}
}
}

14. Program for Circular Doubly Linked List Operations

#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");
}
}
}

15. Program for Merging two Linked Lists

#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 insert_rear(NODE *f)


{
NODE cur, temp;
temp = getnode();
if (*f == NULL)
{
*f = temp;
}
else
{
cur = *f;
while (cur->link != NULL)
{
cur = cur->link;
}
cur->link = temp;
}
}

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");

printf("Enter the number of nodes in second Linked List: ");


scanf("%d", &n2);
for (int i = 0; i < n2; i++)
{
insert_rear(&f2);
}
printf("Linked List 2 created\n");

NODE cur = f1;


while (cur->link != NULL)
{
cur = cur->link;
}
cur->link = f2;
n1 = n1 + n2;

for (int i = 0; i < n1 - 1; i++)


{
NODE cur1 = f1;
NODE cur2 = f1->link;
for (int j = 0; j < n1 - i - 1; j++)
{
if (cur2->info < cur1->info)
{
int temp = cur1->info;
cur1->info = cur2->info;
cur2->info = temp;
}
cur1 = cur1->link;
cur2 = cur2->link;
}
}

printf("Linked lists are concatenated successfully and sorted\n");


display(f1);

16. Program for Addition of Polynomials


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

struct Node
{
int co, xe, ye, ze;
struct Node* link;
};

typedef struct Node* NODE;

NODE first_polynomial, second_polynomial, result;

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;
}

void read(NODE* list)


{
char ch = 'n';
do
{
int c, xe, ye, ze;
printf("\nEnter Coefficient, x-exponent, y-exponent, z-exponent: ");
scanf("%d%d%d%d", &c, &xe, &ye, &ze);
attach(c, xe, ye, ze, list);
label:
printf("\nEnter another? (Y / N): ");
scanf(" %c", &ch);
} while (ch == 'Y');
if(ch!='N')
{
printf("Invalid choice\n");
goto label;
}
}

void display(NODE list)


{
if (!list)
{
printf("Empty polynomial\n");
return;
}

NODE cur = list;


printf("\n");
while (cur != NULL)
{
if(cur->co<0)

printf(" %d x^%d y^%d z^%d",cur->co, cur->xe, cur->ye,cur->ze);


else
printf(" +%d x^%d y^%d z^%d",cur->co, cur->xe, cur->ye,cur->ze);

cur = cur->link;
}
printf("\n");
}

void add(NODE A, NODE B, NODE* R)


{
while (A != NULL && B != NULL)
{
if (A->xe == B->xe)
{
if (A->ye == B->ye)
{
if (A->ze == B->ze)
{
int sum = A->co + B->co;
attach(sum, A->xe, A->ye, A->ze, R);
A = A->link;
B = B->link;
}
else if (A->ze > B->ze)
{
attach(A->co, A->xe, A->ye, A->ze, R);
A = A->link;
}
else
{
attach(B->co, B->xe, B->ye, B->ze, R);
B = B->link;
}
}
else if (A->ye > B->ye)
{
attach(A->co, A->xe, A->ye, A->ze, R);
A = A->link;
}
else
{
attach(B->co, B->xe, B->ye, B->ze, R);
B = B->link;
}
}
else if (A->xe > B->xe)
{
attach(A->co, A->xe, A->ye, A->ze, R);
A = A->link;
}
else
{
attach(B->co, B->xe, B->ye, B->ze, R);
B = B->link;
}
}

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");
}
}

You might also like