linklist3 - Linked Lists III Stacks and Queues Representing...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Linked Lists – III Stacks and Queues Representing Polynomials Circularly linked lists
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Implementation of stacks and queues using linked lists. Stacks and Queues are easier to implement using linked lists. There is no need to set a limit on the size of the stack or the queue. A linked list can serve as a stack, if the pointer to the first node is referred to as stacktop . To implement a push function, do malloc and put the new data in node pNew . Now add this node at the front of the list, and rename it as stacktop. Let us develop this function using a double pointer. (You can also do it using a single pointer and returning stacktop ). In the main program you would first need to define a stack variable of the type struct node. struct node* stack1; int num; stack1 = (struct node*) (malloc(sizeof(struct node))); stack1 = NULL; . . . . . . . . . . . . push( &stack1, num); The Push function places the data num in a new node, attaches that node to the front of stacktop. void push(struct node* *stacktop, int d ) { struct node* pNew = (struct node*) (malloc(sizeof(struct node))); pNew-> data = d ; pNew->next = *stacktop; *stacktop = pNew ; } Similarly you can develop the pop function. Remember that you need to print an error message if the stack is empty. You should also free the first node of the stack and return it to memory.
Background image of page 2
Queues For Queue operations, we need to maintain two pointers – qfront and qRear as we had done for the case of array implementation of queues. For the enqueue operation, a new node is requested using malloc and the given data is stored in its data field. If the queue is empty and a node is being added to it, both qfront and qRear are made to point to this node. If the queue is not empty, the new node is simply appended to the end of the list and qRear updated. void enqueue(struct node**qfront,struct node**qRear ,int d) { struct node* pNew; pNew = (struct node*)(malloc(sizeof(struct node))); pNew-> data = d ; pNew->next = NULL; if (*qfront ==NULL && *qRear == NULL) { *qfront = pNew; *qRear = pNew; } else { (*qRear)->next = pNew; *qRear = pNew; } } For the dequeue function, first of all check if at all there is any element in the queue. If there is none, we would have *qfront as NULL, and so report the queue to be empty, otherwise return the data element, update the *qfront pointer and free the node. Special care has to be taken if it was the only node in the queue. Both pointers would have to be set to NULL.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Application of Linked Lists: Representing a polynomial A polynomial can be represented in an array or in a linked list by simply storing the coefficient and exponent of each term. However, for any polynomial operation , such as addition or multiplication of
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 10

linklist3 - Linked Lists III Stacks and Queues Representing...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online