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

Linked Lists – III Stacks and Queues Representing Polynomials Circularly linked lists

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

View Full Document
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.
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.

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

View Full Document
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
This is the end of the preview. Sign up to access the rest of the document.