Professional Documents
Culture Documents
Roll No. : 29
Div : E
Experiment No. 3
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. .
// Driver code
int main()
{
struct Node *poly1 = NULL, *poly2 = NULL, *poly = NULL;
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
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)
{
scanf("%d %d",&temp1->coff,&temp1->expo);
start1=temp1;
temp1->link=NULL;
}
else
{
} 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
{
} 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;
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))
else if(temp1->expo>temp2->expo)
else
else
{
if(temp1->expo==temp2->expo)
else if(temp1->expo>temp2->expo)
else
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
Program:
// CPP program to segregate even and odd nodes in a
// Linked List
#include <stdio.h>
#include <stdlib.h>
else{
evenEnd -> next = currNode;
evenEnd = evenEnd -> next;
}
}
/* 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));
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);
return 0;
}
Output:
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++)
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 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 Roll-No. :
2
Enter Name :yash
Enter Marks of Subject 1 : 76
Enter Marks of Subject 2 : 90
Enter Marks of Subject 3 : 95