341 linked list chapter 8
Each node contains two fields
1. information 2. next address
operations
1. insertion of a node 2. deletion of a node
Linked list
a dynamic structure defined as a sequence of nodes linked or connected to the node following it. This solves the problem of initializing memory space that is used whether there is anything stored in it or not (as with static data structures)
3 kinds of structures
a) linear doubly-linked list b) circular doubly-linked list w/out header c) circular doubly-linked list w/ header
next(p)
address field of the node whose address is p, and hence, it is a pointer to the next node.
Stack as a circular linked list
assume stack points to the last node and POP/delete and PUSH/add always happens at the first node.
Linked list can
be used as a data structure
deleteminpos: (ascending)
can be done using POP(list) operator, which always deletes the first element
Addpq (descending)
can be done using Place(list,x) where list is now built in decreasing sequence.
Addpq (ascending)
can be done using place(list, x), where place (list, x) builds a list in increasing order.
disadvantages of linear linked list
given a point p to a node, we cannot search any node preceding node (p) Solution: use a circular linked list structure
Notation for basic algorithms
if p is a pointer/address to a node then. Node(p) info(p) next(p) getnode(p) freenode(p)
info(p)
information field of the node pointed to by p
implementing priority queue as an ordered list
insertion requires searching deletion requires only one step
deletemaxpos (descending)
is done using deleteq or POP
A list can be initialized to empty
list <- new
node(p)
noted pointed to by p
Complexity on ordered vs unordered list implementation of pq
ordered pq -insertion takes about n/2 nodes -delete needs 1 node -more efficient unordered pq -insertion needs to look at one node -deletion needs n nodes -less efficient
Problem: not being able to traverse backwards and we cannot delete a node from a circular linked-list given a pointer to that node.
solution: doubly-linked list Advantages: it is possible to delete a given node that is not possible in linear list or circular list
Place
this operation builds an ordered (ascending) linked list and we want to add a node into the ordered list, preserving the order. 1. Traverse the list, if not empty 2. insert at a current location
Queue as a circular linked list
we need only one pointer - q points to the rear and the following node is the front Note: deleteq is the same as POP operation