Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Objective
To understand the concept of data structures
The explain various data structures like linked list, stack and queue with their
operations
To discuss about various applications of stacks and queue
Data structures
Data structure is defines as the way of organizing the data that considers not only
the items stored and also the relationships to each other.
Types
Linear data structures
Non linear data structures
A set of data values and associated operations that are precisely specified independent
of any particular implementation. It is also known as ADT.
2. List ADT
A list is a sequential data structure, ie. a collection of items accessible one after
another beginning at the head and ending at the tail.
It is a widely used data structure for applications which do not need random access
Addition and removals can be made at any position in the list
lists are normally in the form of a1,a2,a3.....an. The size of this list is n.The first
element of the list is a1,and the last element is an.The position of element ai in a list
is i.
Implementation of List:
A list can be implemented in two ways
1. Array list
2. Linked list
The element with the index can be accessed in constant time (ie) the time to
access does not depend on the size of the list.
The time taken to add an element at the end of the list does not depend on the
size of the list. But the time taken to add an element at any other point in the list
depends on the size of the list because the subsequent elements must be shifted
to next index value.So the additions near the start of the list take longer time than
the additions near the middle or end.
Simple arrays are generally not used to implement lists. Because the running time
for insertion and deletion is so slow and the list size must be known in advance
A Linked list is a chain of structs or records called Nodes. Each node has at least
two members, one of which points to the next Node in the list and the other holds the
data. These are defined as Single Linked Lists because they can only point to the next
Node in the list but not to the previous.
The list can be made just as long as required. It does not waste memory space
because successive elements are connected by pointers. The position of each element is
given by an index from 0 to n-1, where n is the number of elements. The time taken to
access an element with an index depends on the index because each element of the list
must be traversed until the required index is found. The time taken to add an element at
any point in the list does not depend on the size of the list, as no shifts are required
Additions and deletion near the end of the list take longer than additions near the middle
or start of the list. because the list must be traversed until the required index is found.
1.is_last()
int is_last( position p, LIST L )
{
return( p->next == NULL );
This procedure is used to check whether the position p is an end of the linked list L
2. Isempty()
This is used to check whether the linked list is empty or not. If the header of linked list is
NULL then return TRUE otherwise return FALSE.
3.Insert()
This procedure is used to insert an element x at the position p in the linked list L. This is
done by
4. Delete()
5.Find()
This is used to check whether the element x is present in the linked list L or not. This is
done by searching an element x from the beginning of the linked list. If it is matched
then return TRUE, else move the next node and continue this process until the end of the
linked list. If an element is matched with any of the node then return FALSE.
Many languages, such as BASIC and FORTRAN, do not support pointers. If linked lists are
required and pointers are not available, then an alternate implementation must be used.
The alternate method we will describe is known as a cursor implementation.
The two important items present in a pointer implementation of linked lists are
1. The data is stored in a collection of structures. Each structure contains the data and a
pointer to the next structure.
2. A new structure can be obtained from the system's global memory by a call to malloc
and released by a call to free.
1.is_last()
This procedure is used to check whether the position p is an end of the linked list L
2. Isempty()
This is used to check whether the linked list is empty or not. If the header of linked list is
NULL then return TRUE otherwise return FALSE.
3.Insert()
This procedure is used to insert an element x at the position p in the linked list L. This is
done by
4. Delete()
This is used to delete an element x from the linked list L. This is done by
5.Find()
A doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node
contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes.
1. Insert()
Create a new node and assign the value x into the data field of new node.
Rearrange the pointers by assigning the address of x to the pointer field of p and
vice versa
Assigning the address of next cell of p to the address filed of x and vice versa
2.Delete()
A linked list have a beginning node and an ending node: the beginning node was a
node pointed to by a special pointer called head and the end of the list was denoted by a
Data Structures-P.Vasantha Kumari,L/IT 11
special node which had a NULL pointer in its next pointer field. If a problem requires that
operations need to be performed on nodes in a linked list and it is not important that the
list have special nodes indicating the front and rear of the list, then this problem should
be solved using a circular linked list. A singly linked circular list is a linked list
where the last node in the list points to the first node in the list. A circular list does not
contain NULL pointers.
8. Applications of List
Multilists
The polynomial can be represented by using linked list. Here, each node that consists of
2 data field (coefficient and exponent) and one pointer field that points the address of
Radix sort is one of the linear sorting algorithms for integers. It functions by sorting the
input numbers on each digit, for each of the digits in the numbers. However, the process
adopted by this sort method is somewhat counterintuitive, in the sense that the numbers
are sorted on the least-significant digit first, followed by the second-least significant digit
and so on till the most significant digit.
To appreciate Radix Sort, consider the following analogy: Suppose that we wish to sort a
deck of 52 playing cards (the different suits can be given suitable values, for example 1
for Diamonds, 2 for Clubs, 3 for Hearts and 4 for Spades). The 'natural' thing to do would
be to first sort the cards according to suits, then sort each of the four seperate piles, and
finally combine the four in order. This approach, however, has an inherent disadvantage.
When each of the piles is being sorted, the other piles have to be kept aside and kept
track of. If, instead, we follow the 'counterintuitive' aproach of first sorting the cards by
value, this problem is eliminated. After the first step, the four seperate piles are
combined in order and then sorted by suit. If a stable sorting algorithm (i.e. one which
resolves a tie by keeping the number obtained first in the input as the first in the output)
it can be easily seen that correct final results are obtained.
An example
The following example shows the action of radix sort on 10 numbers. The input is 64, 8,
216, 512, 27, 729, 0, 1, 343, 125 (the first ten cubes arranged randomly). The first step
bucket sorts by the least significant digit. In this case the math is in base 10 (to make
3.Multilists
As the figure shows, we have combined two lists into one. All lists use a header and are
circular. To list all of the students in class C3, we start at C3 and traverse its list (by going
right). The first cell belongs to student S1. Although there is no explicit information to
this effect, this can be determined by following the student's linked list until the header
is reached. Once this is done, we return to C3's list (we stored the position we were at in
the course list before we traversed the student's list) and find another cell, which can be
determined to belong to S3. We can continue and find that S4 and S5 are also in this
class. In a similar manner, we can determine, for any student, all of the classes in which
the student is registered.
9. Stack ADT
A stack is a list with the restriction that inserts and deletes can be performed in only one
position, namely the end of the list called the top. The fundamental operations on a stack
are push, which is equivalent to an insert, and pop, which deletes the most recently
inserted element. It is also called Last In First Out (LIFO)
Implementation of Stacks
Stack data structure can be implemented using linked list. All thye insertion(push) and
deletion operation(pop) can be performed in LIFO(Last In First Out) manner. Here, the
elements can be inserted up to the size of storage space. The operations can be
performed under stack is push(), pop() and top().
(i) push():
(ii) pop()
This is the function which is for deletion (popping up) of an element from the stack.
It is similar to the deletion of an element at the beginning of a single linked list. Before
deleting the element, check whether the stack(linked list) is empty or not. If it is empty
then return error otherwise delete the first element from the linked list by rearranging
the pointer.
(iii) top()
This function is used to return he top element in the stack which means that the last
inserted element.
element_type top( STACK S )
{
if( is_empty( S ) )
error("Empty stack");
else
return S->next->element;
}
2. Array Implementation of Stacks
Stack data structure can be implemented using arrays. All thye insertion(push) and
deletion operation(pop) can be performed in LIFO(Last In First Out) manner. Here, the
elements can be inserted up to the size of the array. The operations can be performed
under stack is push(), pop() and top().
This is used to deleet or pop an element from th stack. Initially check wtheter the
stack is empty or not. If it is empty then return FALSE. Otherwise decrement the top
pointer by 1.
This function is used to return he top element in the stack which means that the
last inserted element.
element_type top( STACK S )
{
Data Structures-P.Vasantha Kumari,L/IT 19
if( is_empty( S ) )
error("Empty stack");
else
return S->stack_array[ S->top_of_stack ];
}
9. Queue ADT
Queue is an linear data structure which insert an element into the rear(enqueue) and
dete an element from the front(dequeue). It is also called First In First Out(FIFO). That is
the first inserted element will be deleted first.
Queue is implemented using an array. It can insert elements upto the size of an array. All
the insertion(enqueue) and deletion(dequeue) operation can be performed in FIFO (First
In First Out) manner. Here there are two poitera are used. Rear is ued for inserion and
front pointer is use for deletion.
(i) enqueue()
{
Data Structures-P.Vasantha Kumari,L/IT 20
if( is_full( Q ) )
error("Full queue");
else
Q->q_size++;
Q->q_array[ Q->q_rear ] = x;
}
Thi s procedure is used to insert an element into the queue using an array. Before
inserting any element first check whether the queue is full or not. If it is full, then display
an error. Otherwise identify the successor rear position using succ() function and insert
an element into the rear position of queue.
Dequeue()
This is used to delete an element from the queue. This is done by identify the successor
of front. Then the first inserted element is deleted first.
There are three different ways in which an expression like a+b can be represented.
Prefix (Polish)
+ab
Postfix (Suffix or reverse polish)
ab+
Infix
a+b
Note than an infix expression can have parathesis, but postfix and prefix expressions are
Data Structures-P.Vasantha Kumari,L/IT 22
paranthesis free expressions.
Conversion from infix to postfix
Suppose this is the infix expresssion
((A + (B - C) * D) ^ E + F)
To convert it to postfix, we add an extra special value ] at the end of the infix string and
push [ onto the stack.
((A + (B - C) * D) ^ E + F)]
--------->
We move from left to right in the infix expression. We keep on pushing elements onto the
stack till we reach a operand. Once we reach an operand, we add it to the output. If we
reach a ) symbol, we pop elements off the stack till we reach a corresponding { symbol.
If we reach an operator, we pop off any operators on the stack which are of higher
precedence than this one and push this operator onto the stack.
As an example
Example:
is evaluated as follows: The first four symbols are placed on the stack. The resulting
stack is
Next 8 is pushed.
Now, 3 is pushed.
Finally, a '*' is seen and 48 and 6 are popped, the result 6 * 48 = 288 is pushed.
The algorithm to check balanced symbols suggests a way to implement function calls.
The problem here is that when a call is made to a new function, all the variables local to
the calling routine need to be saved by the system, since otherwise the new function will
overwrite the calling routine's variables. Furthermore, the current location in the routine
must be saved so that the new function knows where to go after it is done. The variables
have generally been assigned by the compiler to machine registers, and there are
certain to be conflicts (usually all procedures get some variables assigned to register
#1), especially if recursion is involved.
Applications of Queue
Printers
Job Scheduling
Ticket Reservation ….
Review Questions
Two Mark Questions
1. Define data structure
2. Define ADT
3. What is linear and non linear data structure?
4. Define array and linked list
5. What is the difference between arrays and linked list
6. What is the drawback of array?
7. What are the types of linked list
8. Define doubly linked list
Data Structures-P.Vasantha Kumari,L/IT 26
9. Define circular linked list
10. What are the applications of linked list
11. Define cursor based linked list
12. Define stack
13. Define queue
14. What are the applications of stack and queue?
15. How to convert an infix to postfix expression?
16. Problems in
converting infix to postfix
evaluating postfix expression
Big Questions
1. Write a program to implement a singly linked list
2. Write an procedure to perform insert and delete operation in doubly linked list
3. Explain in detail about the cursor implementation of linked list
4. Explain stack ADT
5. Explain queue ADT
6. Explain in detail about the applications of stack
7. Write a program to reverse a linked list
8. Write an procedure for polynomial addition using linked list
--------- END OF FIRST UNIT ---------