You are on page 1of 20

Name : Mhase Sanchit Kishor

Roll No. : 29
Div : E

Experiment No. 3

Name of the Experiment:


Insertion, deletion operations with Singly linked lists
AIM:
1. Refer LL.c to work with struct Node{int Coeff; int exp;
struct Node * next}; Perform Addition of two Polynomials
using two Linked Lists

2. Refer LL.c to work with struct Node{int Coeff; int exp;


struct Node * next}; Perform Subtraction of two Polynomials
using two Linked Lists

3. Refer LL.c to increment the value of Odd numbered


nodes by 5 and Even numbered nodes by 10

4. Refer LL.c to work with struct Node{int RNo, char name[10],


int marks} and create a Linked list of nodes arranged in
decreasing order of marks.

Theory :
One way chain or singly linked list can be traversed only in one
direction. In other words, we can say that each node contains
only next pointer, therefore we can not traverse the list in the
reverse direction.
In the above figure, the arrow represents the links. The data
part of every node contains the marks obtained by the student
in the different subject. The last node in the list is identified
by the null pointer which is present in the address part of the
last node. We can have as many elements we require, in the
data part of the list.
Operations on Singly Linked List
There are various operations which can be performed on
singly linked list. A list of all such operations is given below.

Insertion
The insertion into a singly linked list can be performed at
different positions. Based on the position of the new node
being inserted, the insertion is categorized into the following
categories.
Insertion at beginning It involves inserting any element at
the front of the list. We just need to a few link adjustments
to make the new node as the head of the list.
2 Insertion at end of the list It involves insertion at
the last of the linked list. The new node can be inserted as
the only node in the list or it can be inserted as the last
one. Different logics are implemented in each scenario.
3 Insertion after specified node It involves insertion
after the specified node of the linked list. We need to skip
the desired number of nodes in order to reach the node
after which the new node will be inserted. .
Deletion and Traversing
The Deletion of a node from a singly linked list can be
performed at different positions. Based on the position of the
node being deleted, the operation is categorized into the
following categories.
Deletion at beginning It involves deletion of a node from
the beginning of the list. This is the simplest operation
among all. It just need a few adjustments in the node
pointers.
2 Deletion at the end of the list It involves deleting the
last node of the list. The list can either be empty or full.
Different logic is implemented for the different scenarios.
3 Deletion after specified node It involves deleting the
node after the specified node in the list. we need to skip the
desired number of nodes to reach the node after which the
node will be deleted. This requires traversing through the
list.
4 Traversing In traversing, we simply visit each node
of the list at least once in order to perform some specific
operation on it, for example, printing data part of each node
present in the list.
5 Searching In searching, we match each element of
the list with the given element. If the element is found on
any of the location then location of that element is returned
otherwise null is returned. .

1. Refer LL.c to work with struct Node{int


Coeff; int exp; struct Node * next}; Perform
Addition of two Polynomials using two Linked
Lists
Program :
// C++ program for addition of two polynomials

// using Linked Lists


#include <bits/stdc++.h>
using namespace std;

// Node structure containing power and coefficient of


// variable
struct Node {
int coeff;
int pow;
struct Node* next;
};

// Function to create new node


void create_node(int x, int y, struct Node** temp)
{
struct Node *r, *z;
z = *temp;
if (z == NULL) {
r = (struct Node*)malloc(sizeof(struct Node));
r->coeff = x;
r->pow = y;
*temp = r;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
else {
r->coeff = x;
r->pow = y;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
}

// Function Adding two polynomial numbers


void polyadd(struct Node* poly1, struct Node* poly2,
struct Node* poly)
{
while (poly1->next && poly2->next) {
// If power of 1st polynomial is greater then 2nd,
// then store 1st as it is and move its pointer
if (poly1->pow > poly2->pow) {
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}

// If power of 2nd polynomial is greater then 1st,


// then store 2nd as it is and move its pointer
else if (poly1->pow < poly2->pow) {
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}

// If power of both polynomial numbers is same then


// add their coefficients
else {
poly->pow = poly1->pow;
poly->coeff = poly1->coeff + poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}

// Dynamically create new node


poly->next
= (struct Node*)malloc(sizeof(struct Node));
poly = poly->next;
poly->next = NULL;
}
while (poly1->next || poly2->next)
{ if (poly1->next) {
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
if (poly2->next) {
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
poly->next
= (struct Node*)malloc(sizeof(struct Node));
poly = poly->next;
poly->next = NULL;
}
}

// Display Linked list


void show(struct Node* node)
{
while (node->next != NULL) {
printf("%dx^%d", node->coeff, node->pow);
node = node->next;
if (node->coeff >= 0) {
if (node->next !=
NULL)
printf("+");
}
}
}

// Driver code
int main()
{
struct Node *poly1 = NULL, *poly2 = NULL, *poly = NULL;

// Create first list of 5x^2 + 4x^1 +


2x^0 create_node(5, 2, &poly1);
create_node(4, 1, &poly1);
create_node(2, 0, &poly1);

// Create second list of -5x^1 -


5x^0 create_node(-5, 1, &poly2);
create_node(-5, 0, &poly2);

printf("1st Number: ");


show(poly1);

printf("\n2nd Number: ");


show(poly2);

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

// Function add two polynomial numbers


polyadd(poly1, poly2, poly);

// Display resultant List


printf("\nAdded polynomial: ");
show(poly);

return 0;
}

Output:
1st Number: 5x^2+4x^1+2x^0
2nd Number: -5x^1-5x^0
Added polynomial: 5x^2-1x^1-3x^0

2. Refer LL.c to work with struct Node{int Coeff;


int exp; struct Node * next}; Perform Subtraction
of two Polynomials using two Linked Lists

Program :
/*
Write a program in C for polynomial subtraction using linked list
*/

#include<stdio.h
>
#include<conio.h
>
//#define NULL 0

void main()
{

struct poly
{
int coff;
int expo;
struct poly *link;
}*temp1,*start1,*temp2,*start2,*start3,*temp3;

int n,i,z=1,num;
char c;

start1=NULL;
printf("\tPolynomial Creation\n\n");
while(1)

{
if(start1==NULL)
{

temp1=(struct poly*)malloc(sizeof(struct poly));


printf("Enter Coefficient and Exponent for Node %d\n",z);

scanf("%d %d",&temp1->coff,&temp1->expo);

start1=temp1;
temp1->link=NULL;

}
else
{

temp1->link=(struct poly*)malloc(sizeof(struct poly));


temp1=temp1->link;
printf("Enter Coefficient and exponent for Node %d\n",z);
scanf("%d %d",&temp1->coff,&temp1->expo);

} z+

+;
printf("Do you want to create another node\n");
fflush(stdin);

scanf("%c",&c);
if(c!='y')

{
temp1->link=NULL;
break;
}
}

start2=NULL;
temp2=NULL;
z=1;
while(1)
{

if(start2==NULL)
{
temp2=(struct poly*)malloc(sizeof(struct poly));
printf("Enter Coefficient and exponent for Node %d\n",z);
scanf("%d %d",&temp2->coff,&temp2->expo);
start2=temp2;
temp2->link=NULL;
}
else
{

temp2->link=(struct poly*)malloc(sizeof(struct poly));


temp2=temp2->link;
printf("Enter Coefficient and exponent for Node %d\n",z);
scanf("%d %d",&temp2->coff,&temp2->expo);

} z+

+;
printf("Do you want to create another node\n");
fflush(stdin);
scanf("%c",&c);

if(c!='y')
{
temp2->link=NULL;
break;
}

//Traversing
temp1=NULL;
temp2=NULL;
temp1=start1;
temp2=start2;

printf("Traversal of Polynomial Linked List 1\n");

while(temp1!=NULL)

printf("%2dx^%d-",temp1->coff,temp1->expo);
temp1=temp1->link;

printf("\b \b");
printf("\nTraversal of Polynomial Linked List 2\n");

while(temp2!=NULL)

printf("%2dx^%d -",temp2->coff,temp2->expo);
temp2=temp2->link;

printf("\b \b");

// subtraction
temp1=NULL;
temp2=NULL;
temp1=start1;
temp2=start2;
temp3=NULL;
start3=NULL;

while(1)

if((temp1!=NULL)||(temp2!=NULL))

if(start3==NULL)
{

if((temp1->expo)==(temp2->expo))

temp3=(struct poly*)malloc(sizeof(struct poly));


temp3->coff= (temp1->coff) - (temp2->coff);
temp3->expo=temp1->expo;
start3=temp3;
temp1=temp1->link;
temp2=temp2->link;
temp3->link=NULL;

else if(temp1->expo>temp2->expo)

temp3=(struct poly*)malloc(sizeof(struct poly));


temp3->coff=temp1->coff;
temp3->expo=temp1->expo;
start3=temp3;
temp1=temp1->link;
temp3->link=NULL;

else

temp3=(struct poly*)malloc(sizeof(struct poly));


temp3->coff=temp2->coff;
temp3->expo=temp2->expo;
start3=temp3;
temp2=temp2->link;
temp3->link=NULL;

else

{
if(temp1->expo==temp2->expo)

temp3->link=(struct poly*)malloc(sizeof(struct poly));


temp3=temp3->link;
temp3->coff= (temp1->coff) - (temp2->coff);
temp3->expo=temp1->expo;
temp1=temp1->link;
temp2=temp2->link;

else if(temp1->expo>temp2->expo)

temp3->link=(struct poly*)malloc(sizeof(struct poly));


temp3=temp3->link;
temp3->coff=temp1->coff;
temp3->expo=temp1->expo;
temp1=temp1->link;

else

temp3->link=(struct poly*)malloc(sizeof(struct poly));


temp3=temp3->link;
temp3->coff=temp2->coff;
temp3->expo=temp2->expo;
temp2=temp2->link;

else

break;

temp3->link=NULL;
//traversing temp3
temp3=NULL;
temp3=start3;
printf("\nTraversal of Polynomial Linked List after subtracting Temp1 &
Temp2\n");

while(temp3!=NULL)

printf("%2dx^%d - ",temp3->coff,temp3->expo);
temp3=temp3->link;

printf("\b\b");
printf("\n the end\n");
getch();

}'

Output:

Polynomial Creation

Enter Coefficient and Exponent for Node 1


2.00*X^2+4.00*X^3
Do you want to create another node
Enter Coefficient and exponent for Node 1
Do you want to create another node
Traversal of Polynomial Linked List 1
2x^0-
Traversal of Polynomial Linked List 2
0x^0 -
Traversal of Polynomial Linked List after subtracting Temp1 & Temp2
2x^0 -
the end

3. Refer LL.c to increment the value of Odd


numbered nodes by 5 and Even numbered
nodes by 10

Program:
// CPP program to segregate even and odd nodes in a
// Linked List
#include <stdio.h>
#include <stdlib.h>

/* a node of the singly linked list */


struct Node
{
int data;
struct Node *next;
};

// Function to segregate even and odd nodes.


void segregateEvenOdd(struct Node **head_ref)
{
// Starting node of list having
// even values.
Node *evenStart = NULL;

// Ending node of even values list.


Node *evenEnd = NULL;

// Starting node of odd values list.


Node *oddStart = NULL;

// Ending node of odd values list.


Node *oddEnd = NULL;

// Node to traverse the


list. Node *currNode =
*head_ref;
while(currNode != NULL){
int val = currNode -> data;

// If current value is even, add


// it to even values list.
if(val % 2 == 0) {
if(evenStart == NULL)
{ evenStart = currNode;
evenEnd = evenStart;
}

else{
evenEnd -> next = currNode;
evenEnd = evenEnd -> next;
}
}

// If current value is odd, add


// it to odd values list.
else{
if(oddStart == NULL)
{ oddStart = currNode;
oddEnd = oddStart;
}
else{
oddEnd -> next = currNode;
oddEnd = oddEnd -> next;
}
}

// Move head pointer one step in


// forward direction
currNode = currNode -> next;
}

// If either odd list or even list is empty,


// no change is required as all elements
// are either even or odd.
if(oddStart == NULL || evenStart == NULL){
return;
}

// Add odd list after even list.


evenEnd -> next = oddStart;
oddEnd -> next = NULL;

// Modify head pointer to


// starting of even list.
*head_ref = evenStart;
}

/* UTILITY FUNCTIONS */
/* Function to insert a node at the beginning */
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node
=
(struct Node*) malloc(sizeof(struct Node));

/* put in the data */


new_node->data = new_data;

/* link the old list off the new node */


new_node->next = (*head_ref);

/* move the head to point to the new node */


(*head_ref) = new_node;
}

/* Function to print nodes in a given linked list */


void printList(struct Node *node)
{
while (node!=NULL)
{
printf("%d ", node->data);
node = node->next;
}
}

/* Driver program to test above functions*/


int main()
{
/* Start with the empty list */
struct Node* head = NULL;

/* Let us create a sample linked list as following 0-


>1->4->6->9->10->11 */

push(&head, 16);
push(&head, 20);
push(&head, 14);
push(&head, 16);
push(&head, 14);
push(&head, 6);
push(&head, 10);
printf("\nOriginal Linked list \n");
printList(head);

segregateEvenOdd(&head);

printf("\nModified Linked list \n");


printList(head);

return 0;
}

Output:

Original Linked list


10 6 14 16 14 20 16
Modified Linked
list 10 6 14 16 14
20 16
4. Refer LL.c to work with struct Node{int RNo, char
name[10], int marks} and create a Linked list of
nodes arranged in decreasing order of marks.

Program :
#include<stdio.h>
struct student
{
int rno;
char name[20]; int marks[3]; int total; float avg;
}stud[2]; int main()
{
int i,j;
struct student s; for(i=0;i<2;i++)
{
printf("Enter Record for Student-%d \n",i+1); printf("printf("Enter Roll-No. : ");
scanf("%d",&stud[i].rno);

\n");

er Name: ");
stud[i].name); stud[i].total=0; for(j=0;j<3;j++)

er Marks of Subject %d : ",j+1); scanf("%d",&stud[i].marks[j]); stud[i].total=stud[i].total+stud[i].marks[j] ; stud[i].avg=stud[i].total/3.0;

n");

i++)

2;j++)

tal<stud[j].total)
{
s=stud[i]; stud[i]=stud[j]; stud[j]=s;
}
}
}
printf("Records in Descending Order.\n (According to Total-Marks)"); printf("\n

\n");
printf("\n ROLLNO NAMETOTAL-MARKSAVG\n");
for(i=0;i<2;i++)
{
printf("\n %d\t %s\t %d\t
%.2f",stud[i].rno,stud[i].name,stud[i].total,stud[i].avg);
}
return 0;
}

Output:

Enter Record for Student-1

Enter Roll-No. : 1
Enter Name: Sanchit
Enter Marks of Subject 1 : 5 87
Enter Marks of Subject 2 : 90
Enter Marks of Subject 3 : 87

Enter Record for Student-2

Enter Roll-No. :
2
Enter Name :yash
Enter Marks of Subject 1 : 76
Enter Marks of Subject 2 : 90
Enter Marks of Subject 3 : 95

Records in Descending Order.


(According to Total-Marks)

ROLLNO NAME TOTAL-MARKS AVG

1 Sanchit 254 84.67


2 yash 261 87.00

You might also like