Unit - Ii: (An ISO 9001:2000 Certified Institution)
Unit - Ii: (An ISO 9001:2000 Certified Institution)
CS1151-DS
JAYAM COLLEGE OF ENGINEERING AND TECHNOLOGY (An ISO 9001:2000 Certified Institution) DHARMAPURI-636813. COURSE CODE:CS1151 COURSE NAME:DATA STRUCTURES DEPT :ECE SEMESTER/YEAR:II/2
ACADEMIC YEAR:2005-2006
UNIT II
LISTS, STACKS AND QUEUES TABLE OF CONTENTS
o o o o
Abstract Data Type (ADT) The List ADT The Stack ADT The Queue ADT
CS1151-DS
v ABSTRACT DATA TYPES o A useful tool for specifying the logical properties of a data type is the abstract data type or ADT Fundamentally, a data type is a collection of values and a set of operations on those values. o That collection and those operations form a mathematical construct that may be; implemented using a particular hardware or software data structure. o The term abstract data type refers to the basic mathematical concept that defines the data type. o In defining an abstract data type as a mathematical concept, we are not concerned with space or time efficiency. The definition of an ADT is not concerned with implementation details at all. It may not even be possible to implement a particular ADT on a particular piece of hardware or using a particular software system. v Implementation of lists o The list can be implemented using static and dynamic memory allocation as shown in following figure.
Concept of Lists o 1st means collection of elements in sequential order. In memory we can store the two ways, one way is we can store the elements in sequential memory o This is known as arrays. And the other way is-we can use pointers or links associate the elements sequentially. This known as Linked Lists. Following Fig Sends this idea of List.
CS1151-DS
Concept of a Linked List o A linked list is a set of Where data field stores the to next node. o Basically link Nodes actual field is where each node has two fields data and a link . Piece of information and link field is used to point nothing but the address only .
Explanation of the program C representation of linked list: Step 1 : We have declared the nodes n1, n2, n3, n4, n5 of structure type. The structure is for singly linked list. So every node will look like this.
Step 2 : We start filling the data in each node at data field and assigning the next pointer to the next node.
Here & is address of symbol. So the above figure can be interpreted as the next pointer of ni is pointing to the node n2. Then we will start filling the data in each node and will assign the next pointer to next node.
To terminate the link list we will set n5 -> next = NULL Step 3 (a) : while (temp! = NULL) set one = &rtl, temp = one
CS1151-DS
Step 3 (b)
1. In linked organization, insertion and deletion of elements can be efficiently done. 2. There is no wastage of memory The memory can be allocated and deal located as per requirement. Disadvantages of linked organization 1. Linked organization does not support random or direct access. 2. Each data field should be supported by a link field to point to next node. Various Functions for Dynamic Storage Management o In fact C supports variety of functions for dynamic storage management. These functions are malloc, calloc, realloc, for memory allocation o function free deallocates the memory. The function malloc we have already discussed. The function calloc is similar to malloc. But there are two differences in these.
CS1151-DS
Types of Linked List There are various types of linked lists such as Singly linear linked list o o Singly circular linked list o Doubly linear linked list o Doubly circular linked list. Let us see each of the above one by one.
Singly linear linked list. o It is called singly because this list consists of only one link, to point to next node or element. This is also called linear list because the last element points to nothing it is linear is nature. o The last field of last node is NULL which means that there is no further list. The very first node is called head or first.
CS1151-DS
Operations on Singly Linked List o In the given C program we have discussed basic operations on a linked list. The create is a function which will create a singly linked list as follows o The head node is a starting node of the linked list. The list has to be scanned from this node only.
CS1151-DS
CS1151-DS
CS1151-DS
Circular Lists o As we have already discussed the concept of circular list, it is a list in which last node s next pointer always points to the first node
CS1151-DS
10
CS1151-DS
o As we have seen already the doubly linked list has two link fields. One link field previous o pointer and the other link field is that next pointer. The typical structure of each node in doubly linked list is like this.
11
CS1151-DS
Logic for doubly linked list: In doubly linked list there are following operations. 1) add 2) display 3) delete
12
CS1151-DS
13
CS1151-DS
The start node s address will be stored in temp variable and while temp > next is not NULL the value at each node will be printed, and each time new temp will be temp > next 3) For deletion operation Step 1 : We assume the linked list as
14
CS1151-DS
15
CS1151-DS
Representation of Stack Using Linked List o Stack is a special case of list and therefore we can represent stack using arrays as well as using linked list. o The advantage of implementing stack using linked lust is that we need not have to worry about the size of the stack. o Since we are using linked list as many elements we want to inset those many nodes can be created. o The nodes are dynamically getting created so there wont be any stack full condition. The typical C structure for linked stack can be struct stack{ int data; struct stack * next; }node;
1. Push operation: o Let us now discuss, the C program given above. In the main function, o when the tern to be inserted is entered, a call to push function is given.
16
CS1151-DS
o In push function another function get_node is invoked to allocate the memory for a node as follows
If item = 10, then by get node function node will be allocated. then *top = New This is our new top node. Hence stack will look like this
17
CS1151-DS
Representation of Queue Using Linked List o As we have seen that the queue can be implemented using arrays, o It is possible implement queues using linked list also. o The main advantage in linked representation is that we need not have to worry about size of the queue. o As in linked organization we can create as many nodes as we want so there will not be a queue full condition at all. o The queue using linked list will be very much similar to a linked list. o The difference between the two is in queue the left most node is called front node and right most node is called rear node. o We can not remove any arbitrary node I queue. We have to remove front node always:
Circular Stack Using Linked List o A linked stack can be made circular by pointing next pointer of very first node which is pushed onto the stack to the top node. o For example : We want to push elements 10, 20, 30, 40, 50 onto the stack. The push operation is illustrated by following figures.
18
CS1151-DS
Circular Queue Using Linked List o In a circular queue using linked list the next pointer of rear node always points the front node. o For example : We want to insert elements 10, 20. 30, 40, 50 in the queue. The insert operation is illustrated by fol1ow figures.
19
CS1151-DS
v Stacks o The stack is an ordered list but the access, insertions and deletions of elements are restricted by following certain rules o For example, in a stack, the operations are carried out in a way such that the last element which is inserted will be the first one to come out Such a order is called Last In First Out Or LIFO o The ordered list does not have such specific ordering and elements can be inserted and deleted in any random order. Thus the stacks may be treated as special cases of ordered lists. Concept of Stack Definition o A stack is an ordered list in which all insertions and deletions are made at one called the top. If we have to make stack of elements 10, 20, 30, 40, 50, 60 then 10 o It be the bottommost element and 60 will be the topmost element in the stack. A
20
CS1151-DS
Primitive Operations on the Stack The operations are as follows o To create a stack o To insert an element on to the stack o To delete an element from the stack o To check which element is at the top of the stack o To check whether a stack is empty or not. o The create function creates a stack in the memory. Creation of stack can be either arrays or linked list. o The insert function inserts a new element at the top of the stack Representation of Stack Using Arrays o A stack is a special case of an ordered list, i.e. it is a ordered list with some restrictions on the way in which we perform various operations on a list. o It is therefore quite natural to use sequential representation for implementing a stack. To do this, we need to define an array of some maximum size. o Moreover, we need an integer variable top which will keep track of the top of the stack as more and more elements are inserted into and deleted from the stack. o The declarations in C are as follows. Declaration 1 #define size 100 int stack top = -1; In the above declaration stack is nothing but an array of integers. And most recent index of that array will act as a top.
21
CS1151-DS
o The stack is of the size 100. As we insert the numbers, the top will get incremented. o The elements will be placed from 0 positions in the stack. At the most we can store 100 elements in the stack, so at the most last element can be at (size-I) position, i.e., at index 99. Declaration 2 #define size 10 struct stack int s int top; st; In the above declaration stack is declared as a structure. o Now compare declaration I and 2. Both are for stack declaration only. But the second declaration will always prefer. o The second declaration we have used a structure for stack elements and top. By this we are binding or co-relating top variable with stack elements.
o The stack can be passed to the function by simply passing the structure variable. o The stack can also be used in the databases. For example if we want to store marks of all the students of forth semester we can declare a structure of stack as follows # define size 60 typedef struct student { int rollno;
22
CS1151-DS
char name ; float marks; } stud; stud sl int top = -1; The above stack will look like this
Thus we can store the data about whole class in our stack. The above declaration means creation of stack. Hence we will write only push and pop function to implement the stack. And before pushing or popping we should check whether stack is empty or full. Stack Empty Operation o Initially stack is empty. At that time the top should be initialized to 1 or 0. o If we set top to -1. Initially then the stack will contain the elements from O position and if we set top to 0 initially, o the elements will be stored from 1 position, in the stack. Elements may be pushed onto the stack and there may be a case that all the elements are removed from the stack. Then the stack becomes empty. Thus whenever top reaches to -1 we can say the stack is empty. int stempty() if(st.top = =-1) return 1; else return 0;
23
CS1151-DS
Stack Full Operation o In the representation of stack using arrays, size of array means size of stack. o As we go on inserting the elements the stack gets filled with the elements. So it is necessary before inserting the elements to check whether the stack is full or not. o Stack full condition is achieved when stack reaches to maximum size of array. int stfull() if(st.top> return 1; else return 0;
o Thus stfull is a boolean function if stack is full it returns 1 otherwise it returns 0. o The two important functions which are carried out on a stack. o push is a function which inserts new element at the top of the stack. The function is as follows. void push(int item) st.top++; st.s =itern; o Note that the push function takes the parameter item which is actually the element which we want to insert into the stack means we are pushing the element onto the stack. In the function
24
CS1151-DS
o we have checked whether the stack is full or not, if the stack is not full then only the insertion of the element can be achieved by means of push operation. o The operation pop , which deletes the element at the top of the stack. The function pop is as given below o Note that always top element can be deleted. Stack pop operation int pop() int item; item = st.s st.top--; return (item);
Applications of Stack Various applications of stack are o Expression conversion. o Expression Evaluation. o parsing well formed parenthesis. o Decimal to binary conversion. o Reversing a string. o Storing function calls. Expressions o Infix Expression o Postfix Expression
25
CS1151-DS
o Prefix Expression Infix Expression In this type of expressions the arrangement of operands and operator is as
For example 1. (a+b) 2 (a+b) * (c- d) 3. (a+b/e) * (d+f) parenthesis can be used in these expressions infix expression are the most natural of representing the expressions. Postfix Expression This type of expressions the arrangement of operands and operator is as
v Queue o The queue can be formally defined as ordered collection of elements that has it ends named as front and rear. o From the front end one can delete the elements a from the rear end one can insert the elements. For Example: o The typical example can be a queue of people who are waiting for a city but the bus stop. o Any new person is joining at one end of the queue; it can call it as rear end. When the bus arrives the person at the other end first enters in the bus. It can call it as the front end of the queue. Following Fig. 5.1 represents the queue of few elements
Representation of Queue Using Arrays o Queue is nothing but the collection of items. Both thc ends of the queue are having their own functionality. o The Queue is also called as LIFO i.e. a Last In First Out data structure. All the elements in the queue are stored sequentially. The various operations on the queue are
26
CS1151-DS
1.Queue Overflow. 2. Insertion of the element into the queue. 3. Queue Underflow. 4. Deletion of the element from the queue. 5. Display of the queue. Insertion of element into the queue The insertion of any element in the queue will always take place from the rear end.
Before performing insert operation you must check whether the queue is full or not. If the rear pointer is going beyond the maximum size of the queue then the queue overflow occurs.
Deletion of the element from the queue The deletion of any element in the queue takes place by the front end always.
If the Queue is empty, you can not perform the deletion .The result to delete an element from the empty queue is called the Queue condition.
Circular Queue o We have deleted the elements 10, 20 and 30 means simply the front pointer shifted ahead. o We will consider a queue from front to rear always. And now if we insert any more element then it won t be possible as it is going to queue full ! message.
27
CS1151-DS
o Although there is a space of elements 10, 20 and 30 (these deleted elements), we can not utilize them because queue is nothing but a array Hence there is a concept called circular queue. o The main advantage of queue is we can utilize the space of the queue fully. The circular queue is shown following Fig.
rear = (rear 1) % size = (4 + 1) % 5 rear = 0 So we can store the element 60 at 0 locations similarly while deleting the element. front = (front + 1) % size =(3+1)%5 front = 4 So delete the element at 4 location i.e. element 50 Applications of Queue There are various applications of queues such as
28
CS1151-DS
DEPARTMENT: ELECTRICAL AND ELECTRONICS ENGINEERING SEMESTER: III COURSE CODE: CS1151 ACADEMIC YEAR: 2005-2006 COURSE NAME: DATA STRUCTURES
QUESTION PAPER
UNIT-II PART A 1. What are the types of Lists available? 2. What is the difference between art array and a list? 3. Define a doubly linked list. 4. What is a priority queue? 5. What is a linked stack? 6. What is a linked stack? 7. Write any three applications of queue. 8. What are the different types of queue available? 9. What is meant by Dequeue ? Why it is used? 10. Write any three applications of stack PART B 1. Explain operations of stack. 2. Explain operations of queue and applications. 3. Explain circular queue and priority queue? 4. Explain types of list 5. Explain doubly link list 6. Explain stack implementation using list 7. Explain queue implementation using list
29