Sei sulla pagina 1di 10

Data Structures

UNIT V DATA STRUCTURES Introduction: A data may consists of several items, each data item may be stored internal in different ways is called Data structures. Data structures are mainly divided into two types. They are Primitive Data structures Non-Primitive Data structures Primitive Data structures: These are the data structures which are stored directly by a system and we can perform any operations on this data items. For example, the data types int, float, char, double etc are belongs to the primitive data structures Non-Primitive Data structures: These are the data structures which are not stored directly by a system, they are classified into two types. They are Linear Data structures Non Linear Data structures Linear Data structures: This type of data structure involves arranging the elements in a sequential order. For example, Arrays , Linked list , Stack , Queues etc. Non Linear Data structures: This type of Data structures involves arranging the elements in a Hierarchical order. For example Trees and Graphs. LINKED LIST Linked list refers to a linear collection of data items, which are inserted and deleted dynamically. Linked list is a collection of nodes. Each node contains two fields. Namely, data field and link field. Node Data Field The data field contains information of the node and the link field contains address of next node or null. The representation of a node is as follows: class Node { int data; Node link; } Types of linked lists: There are four types of linked lists. They are 1. Single linked list. 2. Doubly linked list. 3. Circular linked list. 4. Header linked list. 1. Single linked list: A single linked list is also called as one way list. A single linked list is a collection of nodes. Each node contains two parts. The first part contains the data of the node and the second part contains address of the next node in the list.

Data Structures

Start 10 20 30 40 null 2. Doubly linked list: A doubly linked list can move in both directions, since each node contains two link fields. null 10 20 30 40 null

The representation of node is as follows: class Node { int data; Node left,right; } The left link contains the address of the previous node and right link contains the address of the next node. 3. Circular linked list: In a linked list the last node link field contains the address of the first node is called a circular linked list. To change the linked list into the circular linked list, the statement is q.link=first; first q 10 20 30 40

4. Header linked list: This type of list contains a special node called header node, it contains address of the first node and no information. The header node is placed at the beginning of the list. The list pointer start is positioning to the header node. Start Header node 10 20 30 40 null

Advantages: Linked list provides a flexibility to arrange the items efficiently and also quick accessing of items in the list. Using linked lists we can allocates the memory dynamically and also releases the unused memory dynamically. This effective memory utilization is possible. In linked lists the data elements may be inserted (or) deleted in simple and in less time. Disadvantages: For fixed length list, it will be better to use an array than linked list. Linked list will occupy more space than an array for same number of items, because each item has an additional link field.

Data Structures

STACKS A stack is linear collection of data items, which are inserted and deleted at same end. Such end is called top and other end is called bottom. In stack, we delete the item what we inserted at last. Such process is referred as Last-In-FirstOut(LIFO). In stacks insertion and deletion operations are performed as PUSH and POP. These two operations are performed at the top end of the stack. A stack may be represented in the memory mainly in two ways. They are (1) Arrays (2) Linked list For example, an array based stack with five elements will be represented as follows. POP PUSH top

30 20 10 The stack pointer top is pointing to the top element of the stack. In the array representation of a stack, when the stack is empty, then the top position is -1.

In several applications the size of the stack may modify during execution of a program. For this problem we represented stack using linked list. For example, following figure shows a linked list representation a stack. 3 30 top

20

10
null

In the linked list representation of a stack, when the stack is empty, then the top position is 0. Stack Operations PUSH operation: This operation inserts an element to the top of the stack. To PUSH an element, we have to check weather the stack is full or not. a) If it is full, we display an error message Stack Overflow. b) If it is not full, we increasing top by one and insert an element to the top of the stack. The algorithm for PUSH is as follows. 1. Start 2. if top==(size-1) then 2.1: Print Stack Overflow 2.2: return 3. top top+1 4. s[top] x 5. Stop.

Data Structures

POP operation: This operation deletes an element from the top of the stack. To POP an element, we have to check weather the stack is empty or not. c) If it is empty, we display an error message Stack Underflow. d) If it is not empty, we delete the top most element and then decreasing top by one. The algorithm for POP is as follows. 1. Start 2. if (top==-1) then 2.1: Print Stack Underflow 2.2: return 3. x s[top] 4. top top-1 5. Stop. QUEUES A queue is linear collection of data items, which are inserted and deleted in different ends. Insertion end is called as REAR, and deletion end is called as FRONT. In queue is to be process is referred as First-In-First-Out(FIFO). A queue may be represented in the memory mainly in two ways. They are (3) Arrays (4) Linked list For example, an array based queue with five elements will be represented as follows. REAR

30 20 10 FRONT

In the array representation of a queue, when the queue is empty, then the rear position is -1. In several applications the size of the queue may modify during execution of a program. For this problem we represented queue using linked list. For example, following figure shows a linked list representation a queue. 10 20 30 40 null

FRONT REAR In the linked list representation of a queue, when the queue is empty, then the rear position is 0. Queue Operations REAR operation: This operation inserts an element to the rear of the queue. To insert an element, we have to check weather the queue is full or not. e) If it is full, we display an error message Queue Overflow. f) If it is not full, we increasing rear by one and insert an element to the rear of the queue. The algorithm for REAR is as follows. 1. Start 2. if rear==(size-1) then 2.1: Print Queue Overflow 2.2: return 3. rear rear+1 4. q[rear] x 5. Stop.

Data Structures

FRONT operation: This operation deletes an element from the front of the queue. To delete an element, we have to check weather the queue is empty or not. g) If it is empty, we display an error message Queue Underflow. h) If it is not empty, we delete the first element and then increasing front by one. The algorithm for FRONT is as follows. 1. Start 2. if (rear==-1) then 2.1: Print Queue Underflow 2.2: return 3. x q[front] 4. front front+1 5. Stop. Types of Queues: 1) Circular Queue: In the normal queue, when the REAR pointer reaches to the end, then insertions will not possible even its space available at front. To overcome this problem we can implement the queue as circular queue. front rear 50 Deletion Insertion

A circular queue is a queue in which the locations are treated as circular, such that the first location follows the last location. In a linked representation circular queue the last node is pointing to the first node. rear front
10 20 40 30

70 60 50

2) Dequeue: In a queue both insertions and deletions are may done in both ends, than it is called a Double Ended queue (Dequeue). Insertion Insertion 10 20 30 Deletion Deletion There are two types of dequeues. They are Input Restricted Dequeue Output Restricted Dequeue An Input Restricted Dequeue allows insertions at only one end but deletions on both ends. front rear Insertion 10 Deletion 20 50 Deletion

An Output Restricted Dequeue allows deletions at only one end but insertions on both ends. front rear Insertion Insertion

Data Structures

6
50

10 Deletion

20

3) Priority Queue: A priority queue is a collection of elements, which are stored according to their priority level. Following rules applied to priority queue. a) The element which higher priority is processed before the lower priority element. b) If the elements are same priority, then the element added first in the queue will be processed. Priority queues are implementing job-scheduling by the operating system. The jobs with higher priority are to be processed first. TREES Trees A tree is a finite non empty set of elements. When drawing a tree each element is represented as a node and a link between the node and its subtrees is called as edge. One of these elements must be a root node.
A

In the above example A, B, C, D, E, G, H, I, J are called as nodes. A is the root node. B, C, D are the subtrees of root node A. The number of subtrees of a node is called degree of node. Eg: Degree of A = 3 Degree of B = 2 Degree of C = 1 Degree of D = 3 The node with degree zero will be called as leaf node or terminal node. Eg: E, F, G, H, I, J The node with degree greater than zero will be called as non-terminal node. Eg: A, B, C, D The depth or height of the tree refers to the maximum level of the tree. The height of above tree is 3. A tree with no root node will be called as forest. Binary Tree A binary tree is a finite empty set of elements. When binary tree is not empty, it has a root node, and the remaining elements are partition into two binary trees, which are called as left subtree and right subtree.
A A

The difference between binary tree and tree as follows: A binary tree can be empty, while a tree cannot be empty. In a binary tree each element have exact two subtrees, but in a tree each element have any number of subtrees.

Data Structures

Properties of Binary trees: A binary with n nodes have exactly (n-1) edges.
A

Nodes = n = 7 Edges = (n-1) = 6

A binary tree of height h have exactly 2h-1 elements in it. This type of binary tree is called as full binary tree.
A

Height = h= 3 Elements = 2h-1 = 23-1 = 7

If we delete k elements from any full binary tree without decreasing the height, then it will be called as complete binary tree.
A A A A

Binary Tree Representation: A binary tree can be represented in two ways. They are Arrays Linked lists Array Representation: This is the simplest way to represent the binary trees in memory by using arrays. The root node of binary tree is stored in first location, the left child is stored in second location and right child is stored in third location and so on.
A

0
A
C

1
B

2
C

4
E

6
G

In a tree, missing elements are represented by doted lines with connecting to empty circle. Array representation will be useful, when the number of missing elements are small. This representation makes wasteful space many elements are missed. Linked representation:

Data Structures

In linked representation each element is represented as a node. A node contains two fields. Namely, left child and right child. If a node does not have any child its field is null. A class definition for creating a node is as follows: class Node { int data; Node left, right; } Consider the following example.
A

A
B C

B D E F

C G

GRAPHS Graph: A graph is a collection of nodes or vertices which are joins by edges. A graph G is a collection of two sets. Namely, V and E. G=(V,E) Where V=Collection of vertices E=Collection of edges. An edge with direction is called as directed edge, an edge with no direction is called as undirected edge. The directed edge (i, j) is different from directed edge (j,i), but the undirected edge (i, j) and (j, i) are same. Undirected Graph: In a graph all the edges are undirected, it is called as undirected graph. Undirected graph has an unordered pair of vertices.

G=(V,E) V={1,2,3,4} E={(1,2)(1,4)(1,3)(2,3)(3,4)}

Directed Graph: In a graph all the edges are directed, it is called as directed graph (or) digraph. Directed graph has an ordered pair of vertices.
1

G=(V,E) V={1,2,3,4} E={(1,2)(2,3)(3,1)(3,4)(4,1)}

Data Structures

Loop: If any node contains a self edge in a graph is called as a loop.


1

Weighted Graph: Adjacent Graph:

Cyclic Graph: A graph that has cycles is called a cyclic graph.


1

Path 1 2

(or) 1

Degree: In an undirected graph the number of edges connected to a node is called the degree of that node.
1

The degree of node, d1=3 d2=2 d3=3 d4=2

In-Degree and Out-Degree: In a directed graph, the number of edges coming to that node is called as In-Degree of that node. In a directed graph, the number of edges going from that node is called as Out-Degree of that node.
1

In-Degree of node, d1in=2 d2in=1 d3in=1 d4in=1

Out-Degree of node, d1out=1 d2out=1 d3out=2 d4out=1

Data Structures

10

Source Node: A node when has no incoming edges, but has out going edges is called as a source node. Connected Graph: If there is an edge from one node to all other nodes in an undirected graph is called as connected graph.
1

Strongly Connected Graph: If there is an edge from one node to all other nodes in a directed graph is called as connected graph.
1

Complete Graph: An undirected graph with n nodes contains n(n-1)/2 edges is called as complete graph. In the case of directed graph with n nodes contains n(n-1) edges is called as complete graph. Graph Representation Graphs can be represented in two ways. They are Adjacency matrix representation Linked lists Adjacency matrix: Adjacency matrix is a square matrix, which keeps the information of adjacent nodes. A graph G=(V,E) for adjacent matrix A is as follows A[i][j] = 1, if there is edge from i to j 0, if there is no edge from i to j For undirected graph 1 2 3 4
1

A[i][j] =
2 3

1 2 3 4

0 1 1 1

1 0 1 0

1 1 0 1

1 0 1 0

Potrebbero piacerti anche