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

Appl of linked list

The document discusses the applications of linked lists, particularly in representing stacks and queues, as well as polynomial expressions. It details operations such as push and pop for linked stacks, and insert and delete for linked queues, along with examples of polynomial addition and subtraction using linked lists. The linked representation allows for dynamic memory allocation, overcoming the limitations of fixed-size arrays.

Uploaded by

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

Appl of linked list

The document discusses the applications of linked lists, particularly in representing stacks and queues, as well as polynomial expressions. It details operations such as push and pop for linked stacks, and insert and delete for linked queues, along with examples of polynomial addition and subtraction using linked lists. The linked representation allows for dynamic memory allocation, overcoming the limitations of fixed-size arrays.

Uploaded by

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

APPLICATIONS OF LINKED LISTS

 To represent polynomials and the different operations that can be performed on them
 Linked representation of stacks
 Linked representation of stacks

Linked representation of stacks

- stack can be created using an array.


- creating a stack using array is easy but the drawback is that the array must be declared to
have some fixed size.
- In case the stack is a very small one or its maximum size is known in advance, then the
array implementation of the stack gives an efficient implementation.
- But if the array size cannot be determined in advance, then the other alternative, i.e.,
linked representation, is used.
- In a linked stack, every node has two parts—one that stores data and another that stores
the address of the next node.
- The START pointer of the linked list is used as TOP.
- All insertions and deletions are done at the node pointed by TOP.
- If TOP = NULL, then it indicates that the stack is empty.

OPERATIONS ON A LINKED STACK


- A linked stack supports all the three stack operations, that is, push, pop, and peek.

Push Operation
- The push operation is used to insert an element into the stack.
- The new element is added at the topmost position of the stack.
- memory is allocated for the new node.
- the DATA part of the new node is initialized with the value to be stored in the node.
- check if the new node is the first node of the linked list, done by checking if TOP =
NULL.
- IF true, then NULL is stored in the NEXT part of the node and the new node is called
TOP.
- Else , it is added before the first node of the list (that is, the TOP node) and termed as
TOP
Step 1: Allocate memory for the new struct stack *push(struct stack *top, int val)
node and name it as NEW_NODE {
Step 2: SET NEW_NODE ->DATA = VAL struct stack *ptr;
SET NEW_NODE ->NEXT = NULL ptr = (struct stack*)malloc(sizeof(struct
stack));
Step 3: IF TOP = NULL ptr -> data = val;
SET TOP = NEW_NODE ptr -> next = null;
ELSE if(top == NULL)
SET NEW_NODE ->NEXT = TOP {
SET TOP = NEW_NODE ptr -> next = NULL;
[END OF IF] top = ptr;
Step 4: END. }
else
{
ptr -> next = top;
top = ptr;
}
return top;
}

Pop Operation
- delete the topmost element from a stack.
- before deleting the value, check if TOP=NULL, it means that the stack is empty and no
more deletions can be done.
- If an attempt is made to delete a value from a stack that is already empty, an
UNDERFLOW message is printed.
- pointer PTR that points to TOP.
- TOP is made to point to the next node in sequence.
- The memory occupied by PTR is given back to the free pool.

Step 1: IF TOP = NULL struct stack *pop(struct stack *top)


PRINT UNDERFLOW {
[END OF IF] struct stack *ptr;
Step 2: SET PTR = TOP ptr = top;
Step 3: SET TOP=TOP->NEXT if(top == NULL)
Step 4: FREE PTR printf("\n STACK UNDERFLOW");
Step 5: END else
{
top = top -> next;
printf("\n The value being deleted is: %d", ptr -
> data);
free(ptr);
}
return top;
}

Linked representation of queues

- every element has two parts, one that stores the data and another that stores the address of
the next element.
- The START pointer of the linked list is used as FRONT.
- another pointer called REAR, which will store the address of the last element in the
queue.
- All insertions will be done at the rear end and all the deletions will be done at the front
end.
- If FRONT = REAR = NULL, then it indicates that the queue is empty.

Operations on Linked Queues


Insert Operation
- insert an element into a queue.
- The new element is added as the last element of the queue.
- In Step 1, the memory is allocated for the new node.
- In Step2, the DATA part of the new node is initialized with the value to be stored in the
node.
- In Step 3, check if the new node is the first node of the linked queue. if FRONT = NULL.
then the new node is tagged as FRONT as well as REAR.
- Also NULL is stored in the NEXT part of the node (which is also the FRONT and the
REAR node).
- However, if the new node is not the first node in the list, then it is added at the REAR end
of the linked queue
struct queue *insert(struct queue *start, int val)
{
struct node *newnode,*front,*rear;
newnode = (struct node*)malloc(sizeof(struct
node));
newnode -> data = val;
front=rear=start;
if(front == NULL)
{
front = newnode;
rear=front;
}
else
{
While(rear->next!=NULL)
rear=rear->next;
rear->next=newnode;
}
}
Delete Operation
- delete the element that is first inserted in a queue,
- i.e., the element whose address is stored in FRONT.
- In Step 1, first check for the underflow condition.
- If the condition is true, then an appropriate message is displayed, otherwise in Step 2, we
use a pointer PTR that points to FRONT.
- In Step 3, FRONT is made to point to the next node in sequence.
- In Step 4, the memory occupied by PTR is given back to the free pool.
struct queue *delete_element(struct queue *start)
{
struct node *front;
if(front == NULL)
printf("\n UNDERFLOW");
else
{
front = front -> next;
start=front;
printf("\n The value being deleted is : %d",
front -> data);
free(front);
}
}

Polynomial Representation
- Consider a polynomial 6x3 + 9x2 + 7x + 1.
- Every individual term in a polynomial consists of two parts, a coefficient and a power.
- Here, 6, 9, 7, and 1 are the coefficients of the terms that have 3, 2, 1, and 0 as their
powers respectively.
- Every term of a polynomial can be represented as a node of the linked list.

struct node *add_node(struct node *start, int n, int c)


{
struct node *ptr, *new_node;
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> num = n;
new_node -> coeff = c;
new_node -> next = NULL;

if(start == NULL)
{ start = new_node; }
else
{
ptr = start;
while(ptr -> next != NULL)
ptr = ptr -> next;
ptr -> next = new_node;
}
}

Addition of two polynomials:

struct node *add_poly(struct node *start1, struct node *start2, struct node *start3)
{
struct node *ptr1, *ptr2;
int sum_num, c;
ptr1 = start1, ptr2 = start2;
while(ptr1 != NULL && ptr2 != NULL)
{
if(ptr1 -> coeff == ptr2 -> coeff)
{
sum_num = ptr1 -> num + ptr2 -> num;
start3 = add_node(start3, sum_num, ptr1 -> coeff);
ptr1 = ptr1 -> next;
ptr2 = ptr2 -> next;
}
else if(ptr1 -> coeff > ptr2 -> coeff)
{
start3 = add_node(start3, ptr1 -> num, ptr1 -> coeff);
ptr1 = ptr1 -> next;
}
else if(ptr1 -> coeff < ptr2 -> coeff)
{
start3 = add_node(start3, ptr2 -> num, ptr2 -> coeff);
ptr2 = ptr2 -> next;
}
}
while(ptr2 != NULL)
{
start3 = add_node(start3, ptr2 -> num, ptr2 -> coeff);
ptr2 = ptr2 -> next;
}
while(ptr1 != NULL)
{
start3 = add_node(start3, ptr1 -> num, ptr1 -> coeff);
ptr1 = ptr1 -> next;
}
return start3;
}

Subtraction of two polynomials:

struct node *sub_poly(struct node *start1, struct node *start2, struct node *start4)
{
struct node *ptr1, *ptr2;
int sub_num, c;
ptr1 = start1, ptr2 = start2;
do
{
if(ptr1 -> coeff == ptr2 -> coeff)
{
sub_num = ptr1 -> num – ptr2 -> num;
start4 = add_node(start4, sub_num, ptr1 -> coeff);
ptr1 = ptr1 -> next;
ptr2 = ptr2 -> next;
}
else if(ptr1 -> coeff > ptr2 -> coeff)
{
start4 = add_node(start4, ptr1 -> num, ptr1 -> coeff);
ptr1 = ptr1 -> next;
}
else if(ptr1 -> coeff < ptr2 -> coeff)
{
start4 = add_node(start4, ptr2 -> num, ptr2 -> coeff);
ptr2 = ptr2 -> next;
}
}while(ptr1 != NULL || ptr2 != NULL);
while(ptr2 != NULL)
{
start4 = add_node(start4, ptr2 -> num, ptr2 -> coeff);
ptr2 = ptr2 -> next;
}
while(ptr1 != NULL)
{
start4 = add_node(start4, ptr1 -> num, ptr1 -> coeff);
ptr1 = ptr1 -> next;
}
return start4;
}

You might also like