Showing posts with label linked list. Show all posts
Showing posts with label linked list. Show all posts

Tuesday, April 21, 2009

Linear Data Structures :: Creating Queues

The concept of a queue is what every small child learns when they go to school. Wait your turn. The queue is used to create a FIFO list also known as first in first out. A queue is similar to a stack in that the top of the stack is like the front of the queue. However, the stack is created using Push at the Top while the queue is created by inserting the new node at the back of the queue.

STACK
The following image is an example of a stack. It shows the TOP of the stack equal to 3. The image demonstrates how the stack would push new nodes. Each new node would push down on the next creating what would look like a tower of blocks. The problem with a stack is that if data at the bottom of the stack needs to be popped off the stack it has to wait for everything else to go first. This is a problem in programs that create stacks of processes. Starvation is the term used for a process that does not get a chance to execute.


QUEUE
The image of the queue can be viewed as a line of people waiting at a bank. The first person in line would get to go first. After that person completes their transaction they can leave the queue and the next person can be in front of the line. A queue is a good way to ensure that everyone gets treated equally and prevents starvation from occuring.

Monday, April 20, 2009

Linear Data Structures :: Deleting from a Linked List

Since the previous post discussed inserting a node into a linked list it is necessary to also discuss the deletion of a node from the list. There are multiple cases that need to be taken into consideration. Removing a node from the beginning, end, and inbetween. The steps of deleting form the list consist of first finding the node, next removing the node and finally repairing the link between the list by linking the node previous to the deleted node and the node immediatly following the deleted node.
The following is an example of a linked list. Each square is a node with a structure of a data field and a next. In order to delete the second node we would traverse through the list until the specific data is found. The links must be changed in order to maintain the proper structure as was mentioned before. The first and third square would then be linked.
BEFORE DELETION

AFTER DELETION


Deleting from the beginning or end is similar to what was previously shown. Deleting from the beginning is nothing more than setting the initial pointer to another location. Removal from the end of the list is similar in that we change only one pointer. The previous node must be set to be the end of the list by setting next to NULL.
Deleting and Inserting are not the only functions of linked lists. Searching and Sorting can also be used, often within deletion and insertion algorithims.

Sunday, April 19, 2009

Linear Data Structures :: Linked List

Usually the first data structure that is taught is the linked list. This is because it is simple and easy to implement. In order to create a linked list it is necessary to understand pointers. (The Pointers lesson is located in the archive) A linked list can be as large or small as the programmer wants it to be; of course memory size has to be taken into consideration. A linked list is individual nodes chained together by pointers. Each node can contain any number of fields of information depending on the structure. The following is an example of defining the structure of a linked list.



Adding a node to a list is easiest to do at the end of the list. Creating the new node can be done by using the following example.


This creates a new node and sets the values of each field in the node equal to a value that would be input from the terminal or from file. Traversing the list and deleting a node from the linked list is more difficult and will be discussed in later posts.