0% found this document useful (0 votes)
16 views56 pages

Exp No: Date

The document describes a C program to perform polynomial addition using linked lists. It includes algorithms to: 1. Read in the coefficients and exponents of two polynomials and store them in linked list nodes. 2. Display the polynomials by traversing the linked lists. 3. Add the polynomials by traversing both lists simultaneously, adding coefficients of matching exponents and creating new nodes for the result polynomial.

Uploaded by

Kavi Sanjai
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 views56 pages

Exp No: Date

The document describes a C program to perform polynomial addition using linked lists. It includes algorithms to: 1. Read in the coefficients and exponents of two polynomials and store them in linked list nodes. 2. Display the polynomials by traversing the linked lists. 3. Add the polynomials by traversing both lists simultaneously, adding coefficients of matching exponents and creating new nodes for the result polynomial.

Uploaded by

Kavi Sanjai
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/ 56

EXP NO:

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.

3.Declare head pointer to keep track of the list.

4.Perform the following functions.

5.Insertion at beginning

 Create ptr and check for overflow condition.


 Initialise temp=head
 Traverse temp till the end and set next of temp to ptr.
 Set head=ptr.

6.Insertion at last

 Create ptr and check for overflow condition.


 Initialise temp=head
 Traverse temp till the end and set next of temp to ptr, next of ptr tro head.

7.Insertion at intermediate position

 Create ptr with data and next as NULL.


 Check overflow condition.
 Enter the location and traverse temp till the location.
 Set next of temp to next of ptr and ptr as next of temp.

8.Deletion at last

 Check for underflow condition.


 Set next of head to head.
 Delete head.
 Else set ptr to head and traverse till end (set preptr to ptr).
 Set next of ptr to the next of preptr and delete ptr.

9.Deletion at begining

 Check underflow condition.


 If only one node exists set next of head to head and delete head.
 Else set ptr to head and traverse ptr till the end.
 Set next of ptr to next of head and delete head.

10.Deletion at random position

 Enter the location.


 Set ptr to head , traverse ptr till the location by setting ptr1 as ptr and ptr as next of ptr.
 Set next of ptr to next of ptr1.
 Delete ptr.

11.Search

 Set ptr to head


 If data of ptr is the item ,then item is found.
 Else item is not found.

12.Display

 Set ptr to head.


 Traverse ptr till the end by printing the data of ptr.

13.Stop the program.

PROGRAM:
#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

};

struct node *head;

void beginsert ();

void lastinsert ();

void randominsert();

void begin_delete();
void last_delete();

void random_delete();

void display();

void search();

void main ()

int choice =0;

while(choice != 9)

printf("\n********Main Menu********\n");

printf("\nChoose one option from the following list ...\n");


printf("\n===============================================\n");

printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at random location\n4.Delete from


beginning\n5.Delete from last\n6.Delete at random location\n7.Search for an element\n8.Show\n 9.Exit\n”);

printf("\nEnter your choice?\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:

printf("Please enter valid choice..");

void beginsert()

struct node *ptr,*temp;

int item;

ptr = (struct node *)malloc(sizeof(struct node));

if(ptr == NULL)

printf("\nOVERFLOW");

else

printf("\nEnter the node data?");


scanf("%d",&item);

ptr -> data = item;

if(head == NULL)

head = ptr;

ptr -> next = head;

else

temp = head;

while(temp->next != head)

temp = temp->next;

ptr->next = head;

temp -> next = ptr;

head = ptr;

printf("\nnode inserted\n");

void lastinsert()

struct node *ptr,*temp;

int item;

ptr = (struct node *)malloc(sizeof(struct node));

if(ptr == NULL)

printf("\nOVERFLOW\n");

else

{
printf("\nEnter Data?");

scanf("%d",&item);

ptr->data = item;

if(head == NULL)

head = ptr;

ptr -> next = head;

else

temp = head;

while(temp -> next != head)

temp = temp -> next;

temp -> next = ptr;

ptr -> next = head;

printf("\nnode inserted\n");

void randominsert()

int i,loc,item;

struct node *ptr, *temp;

ptr = (struct node *) malloc (sizeof(struct node));

if(ptr == NULL)

printf("\nOVERFLOW");

}
else

printf("\nEnter element value");

scanf("%d",&item);

ptr->data = item;

printf("\nEnter the location after which you want to insert ");

scanf("\n%d",&loc);

temp=head;

for(i=0;i<loc;i++)

temp = temp->next;

if(temp == NULL)

printf("\ncan't insert\n");

return;

ptr ->next = temp ->next;

temp ->next = ptr;

printf("\nNode inserted");

void begin_delete()

struct node *ptr;

if(head == NULL)

printf("\nUNDERFLOW");

else if(head->next == head)


{

head = NULL;

free(head);

printf("\nnode deleted\n");

else

ptr = head;

while(ptr -> next != head)

ptr = ptr -> next;

ptr->next = head->next;

free(head);

head = ptr->next;

printf("\nnode deleted\n");

void last_delete()

struct node *ptr, *preptr;

if(head==NULL)

printf("\nUNDERFLOW");

else if (head ->next == head)

head = NULL;

free(head);

printf("\nnode deleted\n");

else
{

ptr = head;

while(ptr ->next != head)

preptr=ptr;

ptr = ptr->next;

preptr->next = ptr -> next;

free(ptr);

printf("\nnode deleted\n");

void random_delete()

struct node *ptr,*ptr1;

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;

ptr1 ->next = ptr ->next;


free(ptr);

printf("\nDeleted node %d ",loc+1);

void search()

struct node *ptr;

int item,i=0,flag=1;

ptr = head;

if(ptr == NULL)

printf("\nEmpty List\n");

else

printf("\nEnter item which you want to search?\n");

scanf("%d",&item);

if(head ->data == item)

printf("item found at location %d",i+1);

flag=0;

else

while (ptr->next != head)

if(ptr->data == item)

printf("item found at location %d ",i+1);

flag=0;

break;
}

else

flag=1;

i++;

ptr = ptr -> next;

if(flag != 0)

printf("Item not found\n");

void display()

struct node *ptr;

ptr=head;

if(head == NULL)

printf("\nnothing to print");

else

printf("\n printing values ... \n");

while(ptr -> next != head)

printf("%d\n", ptr -> data);

ptr = ptr -> next;


}

printf("%d\n", ptr -> data);

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.

3.Perform the following functions.

4.Reading the polynomial

 Create temp.
 Dynamically allocate memory.
 Read the coefficient and the power of temp.

5.Display the polynomial

 Dynamically allocate memory for the result.


 While temp is not NULL print the polynomial.
 Print+

6.Adding the polynomial

 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.

7. Stop the program.


PROGRAM:
#include<stdio.h>

#include<stdlib.h>

struct Node

int coeff;

int pow;

struct Node* next;

};

void readPolynomial(struct Node** poly)

int coeff, exp, cont;

struct Node* temp = (struct Node*)malloc(sizeof(struct Node));

*poly = temp;

do{

printf("\n Coeffecient: ");

scanf("%d", &coeff);

printf("\n Exponent: ");

scanf("%d", &exp);

temp->coeff = coeff;

temp->pow = exp;

temp-> next = NULL;

printf("\nHave more terms? 1 for y and 0 for no: ");

scanf("%d", &cont);

if(cont)

temp->next = (struct Node*)malloc(sizeof(struct Node));

temp = temp->next;

temp->next = NULL;

}
}

while(cont);

void displayPolynomial(struct Node* poly)

printf("\nPolynomial expression is: ");

while(poly != NULL)

printf("%dX^%d", poly->coeff, poly->pow);

poly = poly->next;

if(poly != NULL)

printf("+");

void addPolynomials(struct Node** result, struct Node* first, struct Node* second)

struct Node* temp = (struct Node*)malloc(sizeof(struct Node));

temp->next = NULL;

*result = temp;

while(first && second)

if(first->pow > second->pow)

temp->coeff = first->coeff;

temp->pow = first->pow;

first = first->next;

else if(first->pow < second->pow)

temp->coeff = second->coeff;
temp->pow = second->pow;

second = second->next;

else

temp->coeff = first->coeff + second->coeff;

temp->pow = first->pow;

first = first->next;

second = second->next;

if(first && second)

temp->next = (struct Node*)malloc(sizeof(struct Node));

temp = temp->next;

temp->next = NULL;

while(first || second)

temp->next = (struct Node*)malloc(sizeof(struct Node));

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()

struct Node* first = NULL;

struct Node* second = NULL;

struct Node* result = NULL;

printf("\nEnter the corresponding data:-\n");

printf("\nFirst polynomial:\n");

readPolynomial(&first);

displayPolynomial(first);

printf("\nSecond polynomial:\n");

readPolynomial(&second);

displayPolynomial(second);

addPolynomials(&result, first, 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.

3.Perform the following functions

4.Insert

 Dynamically allocate memory.


 Read the polynomial.
 If head is NULL or the exponent is greater than the exponent of the head.
 Else set head as temp and newpolynomial as the link of temp.

5.Create

 Enter the coefficients and power.


 Perform the insert function.

6.Print

 If head is NULL no polynomial exists.


 Else use temp and traverse it till the end by printing the temp.

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.

8.Print the result.

9.Stop the program.


PROGRAM:
#include <stdio.h>

#include <stdlib.h>

struct node

float coeff;

int expo;

struct node* link;

};

struct node* insert(struct node* head, float co, int ex)

struct node* temp;

struct node* newP = malloc(sizeof(struct node));

newP->coeff = co;

newP->expo = ex;

newP->link = NULL;

if(head == NULL || ex > head->expo)

newP->link = head;

head = newP;

else

temp = head;

while(temp->link != NULL && temp->link->expo >= ex)

temp = temp->link;

newP->link = temp->link;

temp->link = newP;

return head;
}

struct node* create(struct node* head)

int n, i;

float coeff;

int expo;

printf("Enter the number of terms: ");

scanf("%d", &n);

for(i=0; i<n; i++)

printf("Enter the coefficient for term %d: ", i+1);

scanf("%f", &coeff);

printf("Enter the exponent for term %d: ", i+1);

scanf("%d", &expo);

head = insert(head, coeff, expo);

return head;

void print(struct node* head)

if(head == NULL)

printf("No Polynomial.");

else

struct node* temp = head;

while(temp != NULL)

printf("(%.1fx^%d)", temp->coeff, temp->expo);

temp = temp->link;

if(temp!=NULL)
printf(" + ");

else printf("\n");

void polyMult(struct node* head1, struct node* head2)

struct node* ptr1 = head1;

struct node* ptr2 = head2;

struct node* head3 = NULL;

if(head1 == NULL || head2 == NULL)

printf("Zero polynomial\n");

return;

while(ptr1 != NULL)

while(ptr2 != NULL)

head3 = insert(head3, ptr1->coeff *

ptr2->coeff, ptr1->expo + ptr2->expo);

ptr2 = ptr2->link;

ptr1 = ptr1->link;

ptr2 = head2;

print(head3);

int main()

{
struct node* head1 = NULL;

struct node* head2 = NULL;

printf("Enter the first polynomial\n ");

head1 = create(head1);

printf("Enter the second polynomial\n ");

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.

2. Create a structure with data and a link to the next node.

3.Create a newnode with its value and next as NULL.

4.Initialise an empty stack.

5.Perform the recursion.

6.Push an element into the stack.

7.Traverse till the end by popping the element.

8.If the popped element is 0 then it has reached the base stack else push element-1.

9.Stop the program.

PROGRAM:
#include <stdio.h>

#include <stdlib.h>

struct StackNode

int data;

struct StackNode* next;

};
struct StackNode* createNode(int data)

struct StackNode* newNode = (struct StackNode*)malloc(sizeof(struct StackNode));

newNode->data = data;

newNode->next = NULL;

return newNode;

struct StackNode* stack = NULL;

void push(int data)

struct StackNode* newNode = createNode(data);

newNode->next = stack;

stack = newNode;

int pop() {

if (stack == NULL) {

printf("Stack is empty\n");

return -1;

struct StackNode* temp = stack;

int data = temp->data;

stack = temp->next;

free(temp);

return data;

void recursiveFunction(int n)

printf("\n Enter the number:") ;

scand("%d", &n) ;

push(n);
while (stack != NULL)

int current = pop();

if (current == 0)

printf("Reached the base case (n=0)\n");

else

printf("Current value: %d\n", current);

push(current - 1);

int main()

int n = 5;

printf("Recursive Function using Stack:\n");

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.

2.Check if the stack is empty.

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

int top = -1;

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;

if(x == '+' || x == '-')

return 1;

if(x == '*' || x == '/')

return 2;

return 0;

int main()

char exp[100];

char *e, x;

printf("Enter the expression : ");

scanf("%s",exp);

printf("\n");

e = exp;

while(*e != '\0')

{
if(isalnum(*e))

printf("%c ",*e);

else if(*e == '(')

push(*e);

else if(*e == ')')

while((x = pop()) != '(')

printf("%c ", x);

else

while(priority(stack[top]) >= priority(*e))

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:

You might also like