DSPD Unit - III Notes PDF
DSPD Unit - III Notes PDF
- 4th /CT
A linked list is a way to store a collection of elements. Like an array these can be character
or integers. Each element in a linked list is stored in the form of a node.
Node:
A node is a collection of two sub-elements or parts. A data part that stores the element and
a next part that stores the link to the next node.
Linked List:
A linked list is formed when many such nodes are linked together to form a chain. Each
node points to the next node present in the order. The first node is always used as a
reference to traverse the list and is called HEAD. The last node points to NULL.
The above definition is used to create every node in the list. The data field stores the
element and the next is a pointer to store the address of the next node.
In place of a data type, struct LinkedList is written before next. That's because its a self-
referencing pointer. It means a pointer that points to whatever it is a part of. Here next is a
part of a node and it will point to the next node.
Creating a Node:
node createNode(){
node temp; // declare a node
sizeof() is used to determine size in bytes of an element in C. Here it is used to determine size
of each node and sent as a parameter to malloc.
Note:- The above code will create a node with data as value and next pointing to NULL.
Here the new node will always be added after the last node. This is known as inserting a
node at the rear end.
Here -> is used to access next sub element of node p. NULL denotes no node exists after
the current node , i.e. its the end of the list.
node p;
p = head;
While (p != NULL)
{
p = p->next;
}
int i;
}
}
newNode->next = temp->next;
temp->next = newNode;
}
Add to end
void insertAtEnd(struct node* head, int value)
{ Allocate memory for new
struct node *newNode; node
newNode = malloc(sizeof(struct node)); Store data
newNode->data = value;
Traverse to last node
newNode->next = NULL;
Change next of last node
struct node *temp = head; to recently created node
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newNode;
}
int main() {
/* Initialize nodes */
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
/* Allocate memory */
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
/* Connect nodes */
one->next = two;
two->next = three;
three->next = NULL;
insertAtFront(&head, 4);
display(head); // 4 --->1 --->2 --->3 --->
deleteFromFront(&head);
display(head); // 1 --->2 --->3 --->
insertAtEnd(head, 5);
display(head); // 1 --->2 --->3 --->5 --->
deleteFromEnd(head);
display(head); // 1 --->2 --->3 --->
int position = 3;
insertAtMiddle(head, position, 10);
display(head); // 1 --->2 --->10 --->3 --->
deleteFromMiddle(head, position);
display(head); // 1 --->2 --->3 --->
When temp is NULL, we know that we have reached the end of linked list so we get out of the
while loop.
A polynomial may be represented using array or structure. A structure may be defined such that
it contains two parts – one is the coefficient and second is the corresponding exponent. The
structure definition may be given as shown below:
Struct polynomial{
int coefficient;
int exponent;
};
The basic idea of polynomial addition is to add coefficient parts of the polynomials having same
exponent.
Algorithm:
AddPoly(Struct Poly p1[10],Struct Poly p2[10],int t1,int t2,Struct Poly
p3[10])
1.) [Initialize segment variables] [Initialize Counter] Set i=0,j=0,k=0
2.) Repeat step 3 while i<t1 and j<t2 Algorithm to add two polynomials using linked list is as
follows:-
3.) If p1[i].expo=p2[j].expo, then Let p and q be the two polynomials represented by
p3[i].coeff=p1[i].coeff+p2[i].coeff linked lists
p3[k].expo=p1[i].expo 1 . while p and q are not null, repeat step 2.
[Increase counter] Set i=i+1,j=j+1,k=k+1 2. If powers of the two terms ate equal then
else if p1[i].expo > p2[j].expo, then if the terms do not cancel then
p3[k].coeff=p1[i].coeff insert the sum of the terms into the sum
p3[k].expo=p1[i].expo Polynomial
[Increase counter] Set i=i+1,k=k+1 Advance p
else Advance q
p3[k].coeff=p2[j].coeff Else if the power of the first polynomial> power of
p3[k].expo=p2[j].expo second Then
Set j=j+1,k=k+1 insert the term from first polynomial into sum
[End of If] polynomial
[End of loop] Advance p
Else insert the term from second polynomial into sum
4.) Repeat while i<t1 polynomial
p3[k].coeff=p1[i].coeff Advance q
p3[k].expo=p1[i].expo 3. copy the remaining terms from the non empty
Set i=i+1,k=k+1 polynomial into the sum polynomial.
[End of loop] The third step of the algorithm is to be processed till the
end of the polynomials has not been reached.
5.) Repeat while j<t2
p3[k].coeff=p2[j].coeff
p3[k].expo=p2[j].expo
Set j=j+1,k=k+1
[End of loop]
6.) Return k
7.) Exit
/* declare three arrays p1, p2, p3 of type structure poly. each polynomial can have maximum of ten
terms
/ * addition result of p1 and p2 is stored in p3 */
/* function prototypes */
int readPoly(struct poly []);
int addPoly(struct poly [],struct poly [],int ,int ,struct poly []);
void displayPoly( struct poly [],int terms);
int main()
{
int t1,t2,t3;
return 0;
}
int addPoly(struct poly p1[10],struct poly p2[10],int t1,int t2,struct poly p3[10])
{
int i,j,k;
i=0;
j=0;
k=0;
i++;
j++;
k++;
}
else if(p1[i].expo>p2[j].expo)
{
p3[k].coeff=p1[i].coeff;
p3[k].expo=p1[i].expo;
i++;
k++;
}
else
{
p3[k].coeff=p2[j].coeff;
p3[k].expo=p2[j].expo;
j++;
k++;
}
}
p3[k].coeff=p1[i].coeff;
p3[k].expo=p1[i].expo;
i++;
k++;
}
/* for rest over terms of polynomial 2 */
while(j<t2)
{
p3[k].coeff=p2[j].coeff;
p3[k].expo=p2[j].expo;
j++;
k++;
}
for(k=0;k<term-1;k++)
printf("%d(x^%d)+",p[k].coeff,p[k].expo);
printf("%d(x^%d)",p[term-1].coeff,p[term-1].expo);
}
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
};
printf("\n[head] <=>");
//start from the beginning
while(ptr != NULL) {
printf(" %d <=>",ptr->data);
ptr = ptr->next;
}
printf(" [last]\n");
}
printf("\n[head] <=>");
//start from the beginning
while(ptr != NULL) {
printf(" %d <=>",ptr->data);
ptr = ptr->prev;
}
printf(" [last]\n");
}
link->data = data;
link->prev = NULL;
link->next = NULL;
current = head;
last = link;
link->prev = current;
}
int main() {
insert(10);
insert(20);
insert(30);
insert(1);
insert(40);
insert(56);
printList();
print_backward();
return 0;
}