Exp No: Date
Exp No: Date
DATE:
AIM:
To write a c program to perform implementation of circular singly linked list.
OBJECTIVE:
The objective is to learn how to create a data structure that’s allows efficient insertion and deletion at
both the ends.This is particularly useful in scenarios where you need to maintain a cyclic order of elements.
ALGORITHM:
1.Start the program.
2.Create a node with a data and pointer to the previous node and the next node.
5.Insertion at beginning
6.Insertion at last
8.Deletion at last
9.Deletion at begining
11.Search
12.Display
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
while(choice != 9)
printf("\n********Main Menu********\n");
scanf("\n%d",&choice);
switch(choice){
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert() ;
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete () ;
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
void beginsert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
if(head == NULL)
head = ptr;
else
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
head = ptr;
printf("\nnode inserted\n");
void lastinsert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW\n");
else
{
printf("\nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
head = ptr;
else
temp = head;
printf("\nnode inserted\n");
void randominsert()
int i,loc,item;
if(ptr == NULL)
printf("\nOVERFLOW");
}
else
scanf("%d",&item);
ptr->data = item;
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
temp = temp->next;
if(temp == NULL)
printf("\ncan't insert\n");
return;
printf("\nNode inserted");
void begin_delete()
if(head == NULL)
printf("\nUNDERFLOW");
head = NULL;
free(head);
printf("\nnode deleted\n");
else
ptr = head;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");
void last_delete()
if(head==NULL)
printf("\nUNDERFLOW");
head = NULL;
free(head);
printf("\nnode deleted\n");
else
{
ptr = head;
preptr=ptr;
ptr = ptr->next;
free(ptr);
printf("\nnode deleted\n");
void random_delete()
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
printf("\nCan't delete");
return;
void search()
int item,i=0,flag=1;
ptr = head;
if(ptr == NULL)
printf("\nEmpty List\n");
else
scanf("%d",&item);
flag=0;
else
if(ptr->data == item)
flag=0;
break;
}
else
flag=1;
i++;
if(flag != 0)
void display()
ptr=head;
if(head == NULL)
printf("\nnothing to print");
else
RESULT:
Thus, the c program for implementation of circular singly linked list was executed and the output was
verified.
FLOW CHART:
EXP NO:
DATE:
AIM:
To write a c program to perform polynomial addition using linked list.
OBJECTIVE:
This program ensures proficiency in traversing the linked list , adding corresponding terms, and creating
new nodes for result. It enhances the problem solving and algorithimic thinking of the learner.
ALGORITHM:
1.Start the program.
2.Create the node structure with coefficient , power and a link to the next node.
Create temp.
Dynamically allocate memory.
Read the coefficient and the power of temp.
If the power of first polynomial is greater than that of the second set first as temp and first ->next as
first.
If the power of second polynomial is greater than that of first set second as temp and second->next as
second.
Else temp->coefficient=first->coefficient +second->coefficient. Temp->power =first->power.
Finally set temp=temp->next and temp->next=NULL.
#include<stdlib.h>
struct Node
int coeff;
int pow;
};
*poly = temp;
do{
scanf("%d", &coeff);
scanf("%d", &exp);
temp->coeff = coeff;
temp->pow = exp;
scanf("%d", &cont);
if(cont)
temp = temp->next;
temp->next = NULL;
}
}
while(cont);
while(poly != NULL)
poly = poly->next;
if(poly != NULL)
printf("+");
void addPolynomials(struct Node** result, struct Node* first, struct Node* second)
temp->next = NULL;
*result = temp;
temp->coeff = first->coeff;
temp->pow = first->pow;
first = first->next;
temp->coeff = second->coeff;
temp->pow = second->pow;
second = second->next;
else
temp->pow = first->pow;
first = first->next;
second = second->next;
temp = temp->next;
temp->next = NULL;
while(first || second)
temp = temp->next;
temp->next = NULL;
if(first)
temp->coeff = first->coeff;
temp->pow = first->pow;
first = first->next;
else if(second)
{
temp->coeff = second->coeff;
temp->pow = second->pow;
second = second->next;
int main()
printf("\nFirst polynomial:\n");
readPolynomial(&first);
displayPolynomial(first);
printf("\nSecond polynomial:\n");
readPolynomial(&second);
displayPolynomial(second);
displayPolynomial(result);
return 0;
RESULT:
Thus, the c program for polynomial addition using linked list was executed and the output was verified.
FLOW CHART:
EXP NO:
DATE:
AIM:
To write a c program to perform polynomial multiplication using linked list.
OBJECTIVE:
Learning polynomial multiplication using linked list provides practical experience in working with this
data type. It enhances our algorithmic thinking.
ALGORITHM:
1.Start the program.
2.Create the structure with coefficient, power and a link to the next node.
4.Insert
5.Create
6.Print
7.Multiplication
If both ptr1 and ptr2 is not NULL then multiply the coefficients and powers of ptr1 and ptr2.
Perform insert function to insert the result.
Set ptr2=ptr2->link and ptr1=ptr1->link.
Set ptr2=head2.
#include <stdlib.h>
struct node
float coeff;
int expo;
};
newP->coeff = co;
newP->expo = ex;
newP->link = NULL;
newP->link = head;
head = newP;
else
temp = head;
temp = temp->link;
newP->link = temp->link;
temp->link = newP;
return head;
}
int n, i;
float coeff;
int expo;
scanf("%d", &n);
scanf("%f", &coeff);
scanf("%d", &expo);
return head;
if(head == NULL)
printf("No Polynomial.");
else
while(temp != NULL)
temp = temp->link;
if(temp!=NULL)
printf(" + ");
else printf("\n");
printf("Zero polynomial\n");
return;
while(ptr1 != NULL)
while(ptr2 != NULL)
ptr2 = ptr2->link;
ptr1 = ptr1->link;
ptr2 = head2;
print(head3);
int main()
{
struct node* head1 = NULL;
head1 = create(head1);
head2 = create(head2);
polyMult(head1, head2);
return 0;
RESULT:
Thus, the c program for polynomial multiplication using linked list was executed and the output was
verified.
FLOW CHART:
EXP NO:
DATE:
AIM:
To write a c program to perform recursion using stack.
OBJECTIVE:
The objective of this program is to improve the understanding of recursion. It provides a chance of
solving a problem by using an iterative process. This also help to gain knowledge of the real time
applications of stack using recursion.
ALGORITHM:
1. Start the program.
8.If the popped element is 0 then it has reached the base stack else push element-1.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct StackNode
int data;
};
struct StackNode* createNode(int data)
newNode->data = data;
newNode->next = NULL;
return newNode;
newNode->next = stack;
stack = newNode;
int pop() {
if (stack == NULL) {
printf("Stack is empty\n");
return -1;
stack = temp->next;
free(temp);
return data;
void recursiveFunction(int n)
scand("%d", &n) ;
push(n);
while (stack != NULL)
if (current == 0)
else
push(current - 1);
int main()
int n = 5;
recursiveFunction(n);
return 0;
RESULT:
Thus the c program for application of stack using recursion was executed and the output was verified.
FLOW CHART:
EXP NO:
DATE:
AIM:
To write a c program to convert infix to postfix.
OBJECTIVE:
This program helps us to understand and evaluate infix and postfix notations which is a different way to
represent mathematical expression. It helps our algorithmic thinking and deepen our understanding of
practical applications of data structures.
ALGORITHM:
1. Start the program.
3. Scan all the symbols one by one from left to right in the given Infix Expression.
4.If the reading symbol is an operand, then immediately append it to the Postfix Expression.
5.If the reading symbol is left parenthesis ‘( ‘, then Push it onto the Stack.
6.If the reading symbol is right parenthesis ‘)’, then Pop all the contents of the stack until the respective left
parenthesis is popped and append each popped symbol to Postfix Expression.
7.If the reading symbol is an operator (+, –, *, /), then Push it onto the Stack. However, first, pop the
operators which are already on the stack that have higher or equal precedence than the current operator and
append them to the postfix. If an open parenthesis is there on top of the stack then push the operator into the
stack.
8. If the input is over, pop all the remaining symbols from the stack and append them to the postfix.
PROGRAM:
#include<stdio.h>
#include<ctype.h>
char stack[100];
void push(char x)
{
stack[++top] = x;
char pop()
if(top == -1)
return -1;
else
return stack[top--];
int priority(char x)
if(x == '(')
return 0;
return 1;
return 2;
return 0;
int main()
char exp[100];
char *e, x;
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
push(*e);
else
printf("%c ",pop());
push(*e);
e++;
while(top != -1)
printf("%c ",pop());
return 0;
RESULT:
Thus the c program to implement the application of stack using infix to postfix was executed and the
output was verified.
FLOW CHART: