0% found this document useful (0 votes)
11 views

Complete Module 3 Notes

Uploaded by

Kakarot
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Complete Module 3 Notes

Uploaded by

Kakarot
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Module 3

Linear Data Structures – Linked list


• Introduction to Linked List
• Representation of LL
• Linked List Vs Arrays
• Types of Linked List:
❖ Singly Linked List
❖ Doubly Linked List
❖ Circular Linked List
• Operations on Singly Linked List
❖ Create List
❖ Insert Node (Empty list, beginning, middle, end)
❖ Delete Node (First, general case)
❖ Search list
❖ Retrieve node
❖ Print list
• Operations on Doubly Linked List
❖ Create List
❖ Insert Node (Empty list, beginning, middle, end)
❖ Delete Node (First, general case)
❖ Search list
❖ Retrieve node
❖ Print list
• Stack using Singly Linked List
• Queue using Singly Linked List
• Singly Linked List Applications
❖ Polynomial representation and Addition

VASAVI. A
Introduction to Linked List

Array:

Array is a linear data structure where all elements are arranged sequentially.

• It is a collection of elements of same data type stored at contiguous


memory locations.

Limitations of Arrays:

Fixed size and Reallocation:

• Arrays have a fixed size, which can lead to memory waste if the allocated
size is larger than the actual data.
• Resizing an array often requires creating a new one and copying elements,
which can be inefficient.
• Since array is static data structure, we should give size at the time of
declaration.

Inefficient insertion and deletion:

Adding or removing elements in the middle of an array requires shifting the


remaining elements, resulting in a higher time complexity.

Less flexible:

Arrays can only store elements of same data type and their structure cannot be
easily adapted to different types (e.g. singly linked list, double, circular linked
list) like linked list.

VASAVI. A
Arrays Vs Linked List

Array Linked List

An array is a consistent set of fixed number A Linked list is an ordered set of a variable
of data items. no. of data items.

They are stored in contiguous memory They are not stored in contiguous memory
locations. locations.

The memory allocation is done at compile The memory allocation is done at run time.
time.

Arrays are fixed in size. Linked Lists are dynamic in size.

Requires less memory space. Require more memory space.

Accessing elements is easier. The whole LL is to be traversed to access


the elements.

The insertion and deletion operations The insertion and deletion operations take
require more time to execute. less time to execute.

VASAVI. A
Linked List:

A Linked List is a dynamic data structure that consists of elements called


nodes, which are connected in a linear sequence.

All the nodes are connected to each other by means of a pointer.

Each node contains 2 parts:

1. Data part
2. Link part (reference to next node) or next part

The data part of the node stores information.

The link part (next part) contains address of the next node of the list.

Linked List Representation:

VASAVI. A
• head always points to first node of the LL. It will have address of 1st node.
• Last node of LL is NULL.

Creating a node in Linked List:


Struct node // Creating node
{
int info; // data part of int type
Struct node *next; // to store address part we use
} pointer of type “struct node”

Operations on Linked List:


1. Traversal – Accessing elements of LL

2. Insertion – Adding elements to LL

3. Deletion – Removing existing elements from LL

Types of Linked Lists:


1. Singly Linked list

2. Doubly Linked list

3. Circular Linked list

VASAVI. A
Singly Linked List
• A singly linked list is a fundamental data structure, consisting
of nodes where each node contains a data field and a reference to the next
node in the node.
• The last node points to null, indicating the end of the list.
• This linear data structure allows for efficient insertion and deletion
operations, making it a popular choice for various applications.

Representation of a node in singly Linked list:

Struct node
{
int info;
Struct node *next;
}
Operations on Singly Linked List:

1. Create node

2. Traversal

3. Insertion

4. Deletion

5. Search

VASAVI. A
Creating a node in Singly Linked List:

Struct node
{
int info;

Struct node *next;


}

Single Linked List traversal:

Linked List requires access to its nodes through sequential traversal.

The process of visiting each node of the list once is called traversing.

• Firstly, we need one pointer (Extra).


• Even though head is a pointer, we cant move it because head always points
to 1st node.
• Take one pointer (ptr) and assign it to head.

VASAVI. A
• Now, move ptr forward

ptr = ptr → next

VASAVI. A
ptr = ptr → next
ptr != null

VASAVI. A
Implementing traversal:
traverse(struct node * head)

if(head = = NULL)

printf(“Linked List is empty”);

struct node * ptr; // Created ptr of struct type for traversing

ptr = head; // head & ptr points to same node initially

while (ptr != NULL)

printf(“%d”, ptr→ data); // prints data part of all nodes till

NULL

ptr = ptr → next; // ptr increments to next

VASAVI. A
Insertion into Singly Linked List:

A node can be added into the linked list in 3 ways:

1. At the beginning of linked list


2. At the end of linked list
3. After a given node(After, Before)

Inserting node into empty list:

Struct node * addToEmpty(Struct node * head, int data)


{
Struct node * new=malloc(sizeof(struct node));
if(top==NULL)
{
new→data=data; // 45
new→next=NULL;
head=new; // 1000
return head;
}
}

VASAVI. A
Inserting a node at the beginning of linked list:

Steps:

1. create new node

2. Fill the data part & address part of new node

3. which address should we add?

Since we are adding new node at the beginning, add head address in new
node’s address., i.e., 1000 in new node.

Because head always points to first node.

4. head will point to first node i.e., new node

So, add 5000 (new node’s address) in head.

VASAVI. A
insertbeginning(struct node * head, int info) // new node’s info

Struct node * new; // create new node

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

new→data = info; // 50 will be added in new node’s data

new→next = head; // 1000 will be added in address part of new node

head=new; // 5000 will be added in head

return head;

VASAVI. A
Inserting node at the end of the Single Linked List:

1. We need to insert a node at the end of the LL.


2. We should traverse through entire LL till last node and then add the new
node.
3. We cannot move head forward.
4. Head always points to first node. If we move head forward, the previous
nodes automatically get deleted from the LL.
5. So, take one pointer to traverse through the LL.

VASAVI. A
insert_end(struct node * head, int info) // data part of new node

{
struct node * ptr, * new; // ptr for traversing, new node to insert
new = (struct node *) malloc (sizeof(struct node));
new→data=info; // 25 will be added in new nodes data
new→next=null;
ptr=head; // 1000 will be added in ptr
if(ptr!=null)
{
while(ptr→next!=null)
{
ptr=ptr→next;
}
ptr→next=new;
}
else
head=new; // if ptr=null 1st node is null. Directly new
will be assigned to head
return head;

VASAVI. A
Inserting new node after a given node in SLL:

VASAVI. A
insertafter(struct node * head, int x, int info) // x is 23 info is 55

Struct node * ptr, *new; // ptr for traversing, new node to insert

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

new→data=info; // add 55 into data of new node

ptr=head; // add 1000 into ptr

while(ptr→data!=x && ptr!=null) // 16!=23 & ptr not null

ptr=ptr→next;

if(ptr→data==x) // 23 == 23

new→next=ptr→next; // add 3000 into address of new node

ptr→next=new; // add 5000

return head;

VASAVI. A
Deletion from Singly Linked List:

A node can be deleted from the LL in 3 ways:

1. From beginning of LL
2. From end of the LL
3. From middle of a LL (After, Before)

Deleting a node from beginning of a SLL:

• To delete first node of a linked list, (1000), directly point head to second
node (2000).
• Automatically, first node 1000 will be deleted.

VASAVI. A
delfirst(struct node * head)
{
struct node * ptr;
if(head==null)
printf(“List is empty”);
else
{
ptr=head;
head=head→next;
free(ptr);
}
return head;
}

VASAVI. A
Deleting a node from end of the SLL:

VASAVI. A
del_last(struct node * head)
{
struct node * ptr, *prev;
if(head==null)
{
printf(“Linked List is empty”);
}
elseif(head→next==null)
{
free(head);
head=null;
}
else
{
ptr=head;
while(ptr→next!=null)
{
prev=ptr;
ptr=ptr→next;
}
prev→next=null;
free(ptr);
}
return head;
}

VASAVI. A
Deleting a node after a given node in SLL:

VASAVI. A
delete_after(struct node * head, int key) // key is 23 300 should be deleted
{
struct node * ptr1, * ptr2;
ptr1=head;
while(ptr1→next!=null)
{
if(ptr1→data==key)
{
ptr2=ptr1→next;
ptr1→next=ptr2→next;
free(ptr2);
break;
}
ptr1=ptr1→next;
}
return head;
}

VASAVI. A
Search a node in SLL:
Searching in a Singly Linked List refers to the process of looking for a
specific element or value within the linked list.
Steps for searching:

1. Traverse the Linked List using ptr (Extra pointer).

2. Check if the current node’s data matches the target value.

• If a match is found, return true.

3. Otherwise, Move to the next node and repeat step 2.

4. If the end of the list is reached without finding a match, return false.

Code:

// Function to search for a node with a given key


int search(struct Node* head, int key)
{
struct Node* ptr;
ptr=head;
while (ptr != NULL)
{
if (ptr->data == key)
{
return 1; // Key found
}
ptr = ptr->next;
}
return 0; // Key not found
}

VASAVI. A
Display (print) elements of Single linked list:

We must traverse from first to last in the singly linked list to display it all.
In contrast to arrays, a linked list’s nodes cannot be accessed arbitrarily.
Therefore, to get to the nth element, we must first walk through all (n-1) elements.

display(struct node * head)


{
if(head = = NULL)
printf(“Linked List is empty”);
struct node * ptr; // Created ptr of struct type for traversing
ptr = head; // head & ptr points to same node initially
while (ptr != NULL)
{
printf(“%d”, ptr→ data); // prints data part of all nodes till
NULL
ptr = ptr → next; // ptr increments to next
}
}

VASAVI. A
Retrieve node of a Single linked list:

int retrieve(struct Node* head, int key)


{
struct Node* ptr;
ptr=head;
while (ptr != NULL)
{
if (ptr->data == key)
{
return 1; // element found
}
ptr = ptr->next;
}
return 0; // element not found
}

VASAVI. A
Module 3
Linear Data Structures – Linked list
• Introduction to Linked List
• Representation of LL
• Linked List Vs Arrays
• Types of Linked List:
❖ Singly Linked List
❖ Doubly Linked List
❖ Circular Linked List
• Operations on Singly Linked List
❖ Create List
❖ Insert Node (Empty list, beginning, middle, end)
❖ Delete Node (First, general case)
❖ Search list
❖ Retrieve node
❖ Print list
• Operations on Doubly Linked List
❖ Create List
❖ Insert Node (Empty list, beginning, middle, end)
❖ Delete Node (First, general case)
❖ Search list
❖ Retrieve node
❖ Print list
• Stack using Singly Linked List
• Queue using Singly Linked List
• Singly Linked List Applications
❖ Polynomial representation and Addition

VASAVI. A
Doubly Linked List
• In a singly linked list, we can traverse only in one direction(forward) i.e.,
each node stores the address of the next node in the linked list.
• It has no knowledge about where the previous node lies in the memory.
• If we are at the 12th node, and if we want to reach 11th node in the linked
list, we must traverse right from the first node.
• Some applications require us to traverse in both forward and backward
directions.
• Here, we can store in each node not only the address of the next node but
also the address of previous node in the linked list. This arrangement is
“Doubly Linked List”

VASAVI. A
Singly Linked List Doubly Linked List
Struct node Struct node
{ {
int data; Struct node * prev;
Struct node * next; int data;
} Struct node * next;
}

Operations on Doubly Linked List:

1. Create node

2. Traversal

3. Insertion

4. Deletion

5. Search

VASAVI. A
Creating a node of Doubly Linked List:

Struct node
{
Struct node * prev;
int data;
Struct node * next;
}
int main()
{
Struct node * head=malloc(sizeof(struct node));
head→prev=null;
head→data=10;
head→next=null;
}

VASAVI. A
Inserting a node in an empty doubly linked list:

#include<stdio.h>
#include<stdlib.h>
Struct node
{
struct node * prev;
int data;
struct node * next;
}
int main()
{
Struct node * head=NULL; // initially head is NULL
head=addToEmpty(head, 45); // data is 45
printf(“%d”, head→data);
return 0;
}
Struct node * addToEmpty(Struct node * head, int data)
{
Struct node * temp=malloc(sizeof(struct node));
temp→prev=NULL;
temp→data=data; // 45
temp→next=NULL;
head=temp; // 1000
return head;
}

VASAVI. A
Inserting node at beginning of doubly linked list:

temp→next=head;

head→prev=temp;

head=temp;

• Update the next part of temp so that it points to (1000) node.


temp→next=head;
• Update prev part of (1000) node so that it points to (2000) node because
2000 should be first node.
head→prev=temp;
• Finally, update head pointer so that it points to 2000 node.
head=temp;

Struct node * addAtBeg(struct node * head, int data)

Struct node * temp=malloc(sizeof(struct node));

temp→prev=NULL; // For creating new node

temp→data=data; // For creating new node

temp→next=NULL; // For creating new node

temp→next=head;

head→prev=temp;

head=temp;

return head;

VASAVI. A
Inserting node at end of doubly linked list:

Step 1:

Traversal (so that we can move till last node and then we can insert new node)

• Update next part of 1000 and update prev part of 3000.


• Take one pointer called tp. First it will point to 2000 and then it will point
to 1000.
while(tp→next!=NULL)
tp=tp→next;

Step 2:

Attach new node to the end node of the list.

• Update next part of 1000 so that it will point to 3000


tp→next=temp; // 3000
temp→prev=tp; // 1000

VASAVI. A
Struct node * addAtEnd(Struct node * head, int data)
{
Struct node * new, * ptr;
new=malloc(sizeof(struct node));
new→prev=NULL;
new→data=data;
new→next=NULL;
ptr=head;
while(ptr→next!=NULL)
ptr=ptr→next;
ptr→next=new;
new→prev=ptr;
return head;
}

Assume temp as new, ptr as tp

VASAVI. A
Inserting node at middle of doubly linked list:

• We want to add a new node after position 2.


• One pointer (newp) points to new node.
• We need 2nd pointer which points to the second node of the list.
• First we will consider one temp pointer pointing to 1st node with 1000
address initially.
• Later it will point to 2nd node (2000) in the list.

while(position!=1)

temp=temp→next;

position--;

• Position variable helps temp pointer move forward.


• temp pointer depends upon position variable.
• Now, we will keep one more pointer to point to 3rd node (3000) temp2.
• Always temp2 will point to the node after temp node.
while(position!=1)
{
temp=temp→next;
position--;
}
temp2=temp→next;
• Now, we should add new node in between 2000 and 3000.
• First, we will update next part of 2000.
temp→next=newp;
• Now, we need to update prev part of 3000 so that it will point to 4000.

VASAVI. A
temp2→prev=newp;
• Now, we should update prev part of new node and next part of new node.
• Prev part should contain address of previous i.e., 2000 and next part of new
node should be 3000.
newp→prev=temp;
newp→next=temp2;

Struct node * addAfterpos(struct node * head, int data, int position) position is 2
{
Struct node * newp=NULL;
Struct node * temp=head;
Struct node * temp2=NULL;
newp= addToEmpty(newp, data)
while(position!=1)
{
temp=temp→next;
position--; position is 2
}
temp2=temp→next;
temp→next=newp;
temp2→prev=newp;
newp→next=temp2;
newp→prev=temp;
return head;
}

VASAVI. A
Adding a node before a specific position (DLL):

• We want to add a new node before position 3 (3000).


• We use one temp pointer initially points to 1st node (1000)
• Later, we will move temp to right side…..
Struct node * temp=head;
While(position>2) // we should stop temp at 2000 so pos>2
condition
{
Temp=temp→next;
Position--;
}
• Temp pointer must point to the node after which we want to add a new
node.
i.e., temp must point to 2000 node.
• We take one pointer temp2 points to last node.
• Now, pos=2;
Temp2=temp→next;
Temp→next=newp;
Temp2→prev=newp;
Newp→next=temp2;
Newp→prev=temp;

VASAVI. A
Code:

Struct node *addBeforepos(struct node * head, int data, int position) position is 3
{
Struct node * newp=NULL;
Struct node * temp=head;
Struct node * temp2=NULL;
newp=addToEmpty(newp, data);
int pos=position; 3
while(pos>2) pos is 3
{
temp=temp→next;
pos--;
}
temp2=temp→next;
temp→next=newp;
temp2→prev=newp;
newp→next=temp2;
newp→prev=temp;
return head;
}

VASAVI. A
Deletion from Doubly Linked List

Deleting the first node of the DLL:

Method 1:

• Keep one pointer temp pointing to 1000. If head moves forward previous
data will be deleted.
• Now, we will move head forward:
head=head→next;
free(temp);
• Then call free function to delete the 1st node and then we need to assign
NULL to temp and then we should keep NULL to prev part since, 2000
is 1st node now.
head=head→next;
free(temp);
temp=NULL;
head→prev=NULL;

Method 2:

• We can directly move head pointer forward.


head=head→next;
• Now, we should remove prev part of 2000.
free(head→prev); 1st node will be removed
• Now, update the prev part of 2000
head→prev=NULL;

VASAVI. A
Code:

Struct node * delFirst(struct node * head)

Struct node * temp=head;

head=head→next;

free(temp);

temp=NULL;

head→prev=NULL;

return head;

VASAVI. A
Deleting the last node of the DLL:

• Keep one pointer pointing to 1st node and then move temp forward.
while(temp→next!=NULL)
{
temp=temp→next;
}
temp2=temp→prev;
temp2→next=NULL;
free(temp);
temp=NULL;
• Keep one more pointer temp2 points to 2000, the node which is before
the node we want to delete.
• Update next part of 2000 so that it will contain NULL because it will be
last node.
• Now free temp i.e., 3000
• 3000 will be deleted. Now place NULL in temp.

Struct node * delLast(struct node * head)


{
struct node * temp=head;
struct node * temp2;
while(temp→next!=NULL)
temp=temp→next;
temp2=temp→prev;
temp2→next=NULL;
free(temp);
return head;
}

VASAVI. A
VASAVI. A
Deleting the intermediate node of the DLL

• We need to delete the node at position 2 (2000).


• Take one temp pointer pointing to 1st node (1000).
• Move temp forward
while(position>1)
{
temp=temp→next;
position--;
}
temp2=temp→prev;
temp2→next=temp→next;
temp→next→prev=temp2;
free(temp);
temp=NULL;
• Always temp2 points to node before we want to delete.
• We want to delete 2000 so temp2 should point to 1000.
• Update next part of temp2.
• Update prev part of 3000
• Then free temp.

VASAVI. A
Code:
Struct node * delinter(Struct node * head, int position)
{
Struct node * temp=head;
Struct node * temp1=Null;
if(position==1)
{
head=delfirst(head);
return head;
}
while(position>1)
{
temp=temp→next;
position--;
}
if(temp→next==NULL)
{
head=dellast(head);
}
else
{
temp1=temp→prev;
temp1→next=temp→next;
temp→next→prev=temp1;
free(temp);
temp==null;
} return head;
}

VASAVI. A
Search for a node in doubly linked list
int search(struct node* head, int x)
{
struct node* temp = *head;
int pos = 0;
while (temp->data != x && temp->next != NULL)
{
pos++;
temp = temp->next;
}
if (temp->data != x)
return -1;
return (pos + 1);
}

VASAVI. A
Retrieve node of a Double linked list

int search(struct node* head, int x)


{
struct node* temp = *head;
int pos = 0;
while (temp->data != x && temp->next != NULL)
{
pos++;
temp = temp->next;
}
if (temp->data != x)
return -1;
return (pos + 1);
}

VASAVI. A
Display (print) elements of Double linked list
void printList(node* head)
{
node* temp = head;
printf("Linked List elements: ");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

VASAVI. A
Stack using Singly Linked List
• Since, Linked list is dynamic, we can add n no. of elements.
• Head will be top.
• Insertions and deletions are done from left side.
• If top is NULL, stack is empty (underflow)

Operations on Stack:

Push Operation:

VASAVI. A
push(struct stack * top, int info) // top – top of stack, info of new node
{
new=(struct stack *) malloc(sizeof(struct stack));
if(new==NULL)
Printf(“Stack Overflow”);
if(top==NULL) // if top is NULL, it should be last and first node
{
new→data=info;
new→next=NULL;
top=new;
}
else // if there are already some elements in the list
{
new→data=info;
new→next=top;
top=new;
}
return top;
}

VASAVI. A
Pop Operation:
pop(struct stack * top)
{
struct stack * ptr;
if(top==NULL) // No elements in stack
printf(“List is empty”);
else // If there are elements
{
ptr=top;
top=top→next;
free(ptr);
}
return top;
}

VASAVI. A
Display:
void display()
{
struct node * ptr
if(top==NULL)
{
printf(“Stack is empty”);
}
else
{
ptr=top;
while(ptr!=NULL)
{
printf(“%d\t”, ptr→data);
ptr=ptr→next;
}
}
}

VASAVI. A
Stack using Linked List program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*stack;
void push(int x)
{
struct node *new;
new=(struct node*)malloc(sizeof(struct node));
new->data=x;
new->next=stack;
stack=new;
return;

VASAVI. A
}
int pop()
{
struct node *ptr;
if(stack==NULL)
{
printf("stack is empty \n");
}
else
{
int x=stack->data;
ptr=stack;
stack=stack->next;
free(ptr);
return x;
}
}
void display()
{
struct node *temp=stack;
if(stack==NULL)
{
printf("stack is empty\n");
}
else
{

VASAVI. A
while(temp!=NULL)
{
printf("%d\t", temp->data);
temp=temp->next;
}
}
}
void main()
{
int ch,stack,x;
while(1)
{
printf("1.push \n");
printf("2.pop \n");
printf("3.display \n");
printf("4. exit \n");
printf("Enter your choice \n");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("enter the element \n");
scanf("%d", &x);
push(x);
break;
case 2: x=pop();
printf("element is popped from the stack is %d \n",x);

VASAVI. A
break;
case 3: display();
break;
case 4: exit(0);
}
}
}

VASAVI. A
Queue using Single Linked List
• Since Linked List is dynamic, we can add n no. of elements.
• Insertions and deletions are done from two ends.
• Data can only be inserted from one end called “rear” end.
• Data can only be deleted from one end called “front” end.

Operations on Queue:
Enqueue():
void insertq(int x)
{
struct node * new;
new=(struct node *)malloc(sizeof(struct node));
new→data=x;
new→next=NULL;
if(front==NULL) // Queue empty
{
front=rear=new;
}
else // There are some elements in queue
{
rear→next=new;
rear=new;
}
}

VASAVI. A
Dequeue():
int deleteq()
{
struct node * ptr;
if(front==NULL)
{
printf(“Queue is empty”);
}
else
{
int x=front→data;
ptr=front;
front=front→next;
free(ptr);
return x;
}
}

VASAVI. A
Display():
void display()
{
struct node * ptr=front;
if(front==NULL)
{
printf(“Queue is empty”);
}
else
{
printf(“The elements of Queue are”);
while(ptr!=NULL)
{
printf(“%d\t”, ptr→data);
ptr=ptr→next;
}
}
}

VASAVI. A
VASAVI. A
Queue using Linked List program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
} * front, * rear;
void insertq(int x)
{
struct node * new;
new=(struct node *)malloc(sizeof(struct node));
new→data=x;
new→next=NULL;
if(front==NULL)
{
front=rear=new;
}
else
{
rear→next=new;
rear=new;
}
}
int deleteq()
{
struct node * temp;
if(front==NULL)

VASAVI. A
{
printf(“Queue is empty”);
}
else
{
int x=front→data;
temp=front;
front=front→next;
free(temp);
return x;
}
}
void display()
{
struct node * temp=front;
if(front==NULL)
{
printf(“Queue is empty”);
}
else
{
printf(“The elements of queue are”);
while(ptr!=NULL)
{
printf(“%d\t”, temp→data);
ptr=ptr→next;
}
}
void main()

VASAVI. A
{
int ch, x;
while(1)
{
printf(“1. Insert \n”);
printf(“2. Delete \n”);
printf(“3. Display \n”);
printf(“4. Exit \n”);
printf(“Enter your choice \n”);
scanf(“%d”, &ch);
switch(ch)
{
case 1: printf(“Enter the element \n”);
scanf(“%d”, &x);
insertq(x);
break;
case 2: x=deleteq();
printf(“The deleted element is %d \n”, x);
break;
case 3: display();
break;
case 4: exit(0);
}
getch();
}
}

VASAVI. A
Circular Linked List

Circular Linked list:

The circular linked list is linked list where all nodes are connected to form a circle.

• In a circular linked list, the first node and the last node are connected to
each other which forms a circle.
• There is no NULL at the end.

Circular linked list types:

1. Circular singly linked list


2. Circular doubly linked list

VASAVI. A
Circular singly linked list:

The circular singly linked list is like the singly linked list except that the
last node of the circular singly linked list points to the 1st node.

Circular doubly linked list:

The circular doubly linked list is like the doubly linked list except that the
last node of the circular doubly linked list points to the first node and the first
node of the circular doubly linked list points to the last node.

VASAVI. A
Polynomial representation and Addition
• Polynomial addition in c programming is a fundamental operation used in
many applications such as signal processing, computer graphics, and
scientific simulations.
• We do polynomial addition using linked list in C programming.
• Linked lists provide an elegant solution for efficiently handling
polynomials due to their dynamic memory allocation.

Structure of Node:

In the linked list, each node has coefficient and power.

Therefore, each node represents a term of a polynomial.

For example, we can represent the polynomial with a linked list:

VASAVI. A
The linked list node contains three values:

• Coefficient: The numeric value.

• Degree: The power of the variable x.

• Address: The address of the next node of the linked list.

Polynomial Addition:

• We are given two polynomials represented by linked lists. We need to add


them.

Q) Add the polynomials

When we add them together, we can group the like terms and generate the result

VASAVI. A
Q) Add the polynomials

Q) Add the polynomials

Q) Add the polynomials

VASAVI. A
Q) Add the polynomials:

Q) Add the polynomials 5x^4 + 3x^2 + 1 and 4x^4 + 2x^2 + x

Polynomial 1 = 5x^4 + 3x^2 + 1

Polynomial 2 = 4x^4 + 2x^2 + x

Resultant list = 9 X^4 + 5 X ^ 2 + X + 1

VASAVI. A
Algorithm for Performing the Polynomial Addition using Linked List in C
[Initialize segment variables]
[Initialize Counter] Set i=0,j=0,k=0
Repeat while i<t1 and j<t2
IF p1[i].expo=p2[j].expo, THEN
p3[i].coeff=p1[i].coeff+p2[i].coeff
p3[k].expo=p1[i].expo
[Increase counter] Set i=i+1,j=j+1,k=k+1
ELSE IF p1[i].expo > p2[j].expo, THEN
p3[k].coeff=p1[i].coeff
p3[k].expo=p1[i].expo
[Increase counter] Set i=i+1,k=k+1
ELSE
p3[k].coeff=p2[j].coeff
p3[k].expo=p2[j].expo
Set j=j+1,k=k+1
[End of If]
[End of loop]
Repeat while i<t1
p3[k].coeff=p1[i].coeff
p3[k].expo=p1[i].expo
Set i=i+1,k=k+1
[End of loop]
Repeat while j<t2
p3[k].coeff=p2[j].coeff
p3[k].expo=p2[j].expo
Set j=j+1,k=k+1

VASAVI. A
[End of loop]
Return k
EXIT

VASAVI. A
Module 3 Important Questions
1. What is an Array? What are the limitations of an array?
2. Distinguish Array and Linked list.
3. What is a Linked list? Explain how a linked list is represented?
4. Explain how a node is created in a linked list with example code.
5. Explain about types of linked list with example notation.
6. Explain about Singly Linked list. How a node is represented using SLL.
7. Explain about singly linked list traversal with example and code.
8. Explain how a node is inserted in a singly linked list with example and code.
9. Explain how a node is deleted from a singly linked list with example and code.
10. Write a program to search for a given node in singly linked list.
11. Write a program to print elements of a singly linked list.
12. How a node is retrieved from a singly linked list? Explain with example and program.
13. Explain about doubly linked list. How a node is represented using DLL.
14. Explain about doubly linked list traversal with example and code.
15. Explain how a node is inserted in a doubly linked list with example and code.
16. Explain how a node is deleted from a doubly linked list with example and code.
17. Write a program to search for a given node in doubly linked list.
18. Write a program to print elements of a doubly linked list.
19. Distinguish Singly linked list and doubly linked list.
20. Write a program for implementing Stack using Linked list along with example.
21. Write a program for implementing Queue using Linked list along with example
22. Explain in brief about Circular linked list and its types.
23. Write an algorithm for polynomial representation and addition.
24. Numericals on Polynomial representation and addition.

VASAVI.A

You might also like