Appl of linked list
Appl of linked list
To represent polynomials and the different operations that can be performed on them
Linked representation of stacks
Linked representation of stacks
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.
- 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.
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.
if(start == NULL)
{ start = new_node; }
else
{
ptr = start;
while(ptr -> next != NULL)
ptr = ptr -> next;
ptr -> next = new_node;
}
}
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;
}
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;
}