Upcoming SlideShare
×

Data structures

2,997 views

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
2,997
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
106
0
Likes
1
Embeds 0
No embeds

No notes for slide

Data structures

1. 1. Data Structures
2. 2. What are Data Structures?• Data may be organized in many different ways.• A Data Structure is an arrangement of data in a computer’s memory or on a disk.• The logical or mathematical model of a particular organization of a data is called Data Structures.• The choice of a data model depends on two consideration:1) It must be rich enough in structure to mirror the actual relationships of the data in the real world.2) The structure should be simple enough that one can effectively process the data when necessary.
3. 3. Types Of Data Structures• The more commonly used data structures include lists, arrays, stacks, queues, heaps, trees, and graphs.1) Arrays: The simplest type of data structure is linear array. A linear array is list of a finite number n of similar data elements referenced respt by a set of n consecutive numbers.2) Linked List: A list is an ordered set of data. It is often used to store objects that are to be processed sequentially. A list can be used to create a queue.3) Trees: Data frequently contain a hierarchical relationships between various elements. The data structure which reflects this relationships is called a rooted tree graph or a tree.
4. 4. 4) Stack: A stack also called a last-in first-out (LIFO) system, is a linear list in which insertions and deletions can take place only at one end, called top.5) Queue: A queue also called first-in first-out (FIFO) system, is a linear list in which deletion can take place only at one end of the list, the “front” of the list, and insertion can take place only at the other end of the list, the “rear” of the list.6) Graph: Graphs are data structures rather like trees. Graphs often have a shape dictated by a physical or abstract problem. For example, nodes in a graph may represent cities, while edges may represent airline flight routes between the cities. Nodes are traditionally called vertices
5. 5. Data Structure Operations• The data appearing in data structures are processed by means of operations. The following are operations are major operations: a) Traversing: Accessing each record exactly once so that certain items in the record may be processed. b) Searching: Finding the location of the record with a given key value, or finding the locations of all records which satisfy one or more conditions. c) Inserting: Adding a new record to the structure. d) Deleting: Removing a record from the structure.Following two are special operations: a) Sorting: Arranging the records in some logical order. b) Merging: Combining the records in two different sorted files into a single sorted file.
6. 6. What are Algorithms?• An algorithm is a well-defined list of steps for solving a particular problem.• Algorithms manipulate the data present in data structures in various ways, such as searching for a particular data item and sorting the data.• Flowcharts are pictorial representations of the algorithms.• Properties/ characteristics of the algorithm: a) Finiteness: Total number of steps used in an algorithm should be finite. b) Definiteness: Each step of algorithm must be clear and unambiguous. c) Effectiveness: Every step must be basic and essential. d) Input/Output: The algorithm must accept zero or more input and must produce at least on output.
7. 7. Algorithmic Problem• There are mainly two types of algorithm: a) Simple Flow Problem: these are very simple problems and the procedure flow is sequential manner ie step-by-step. b) Branching Flow Problem: These sentences are not simple. It consists of condition, it tests the condition and accordingly transfers the flow.Eg: simpleFlowProblem.txt branchingFlowPrblm.txt
8. 8. Complexity of Algorithm• The analysis of algorithm is a major task. In order to compare algorithms, we must have some criteria to measure the efficiency of an algorithm. The efficiency of an algorithm depends on time and space factors.• Suppose M is an algorithm, and suppose n is the size of the input data. The time is measured by counting the number of key operations ,eg in sorting and searching algo, key operation is number of comparisons. The space is measured by counting the maximum of memory needed by the algorithm.• The complexity of an algo M is the function f(n) which gives the running time and/or storage space requirement of the algorithm in terms of size n of the input data.
9. 9. Example• Suppose we are given an English short story TEXT, and suppose we want to search through TEXT for the first occurrence of a gvn 3-letter word W. If W is the 3-letter word “the” then it is likely that W occurs near the beginning of TEXT. So f(n) will be small. On the other hand, if W is the 3- letter word “zoo”, then W may not appear in TEXT at all, so f(n) will be large.• From the above example complexity can be investigated as follows: a) Worst Case: When any searching algo finds required element in last attempt. b) Average Case: When any searching algo finds required element in some avg number of attempts. c) Best Case: When any searching algo finds required element in first attempt.
10. 10. ARRAYS
11. 11. • An array is a group of related data items that share a common name.• An array should be of a single type, comprising of integers or strings and so on.• Array elements are accessed by using index which is mentioned inside the subscript operator ([ ]).
12. 12. Types of Array1) One-Dimensional Array: Creating array involves three steps: a) Declaration: type[ ] arrayname; or type arrayname[ ]; b) Creating new object of array: arrayname= new type[size]; c) Initialization of array: arrayname[index] = value; or type arrayname[ ]={list of values};*****Give one example
13. 13. 2) Two-Dimensional Array: Eg: int table[][]; //declaration table=new int[2][3]; //creating new object of 2D //array with 2 rows and 3 cols. int table[][]={{0,0,0},{1,1,1}}; //initialization****Give one example. Duplicating Array: this is one of the properties of an array by which we can make duplication of array. Object.clone() method is used for duplication. Eg: Duplication.java
14. 14. Searching Techniques• Searching is used to locate the position of specific item in the array. Search can be performed by using following techniques: a) Sequential Search. b) Binary Search.1) Sequential Search: It is also called linear search since we can search element on after the other in the array. First we compare the search key value with first index variable, then second and so on till we get the search key. Algorithm: sequentialSearchAlgo.txt Eg: SequentialSearch.java
15. 15. 2) Binary Search: Binary search relies on a divide and conquer strategy to find a value within an already-sorted collection. Algorithm: binarySearch.txt Eg: Find 6 in {-1, 5, 6, 18, 19, 25, 46, 78, 102, 114}.• Step 1 (middle element is 19 > 6): -1 5 6 18 19 25 46 78 102 114• Step 2 (middle element is 5 < 6): -1 5 6 18 19 25 46 78 102 114• Step 3 (middle element is 6 == 6): -1 5 6 18 19 25 46 78 102 114
16. 16. STACK
17. 17. • Stack is the collection of ordered elements, which can be accessed through LIFO manner. One end is used for insertion as well as deletion purpose. Stack Interface: Stack interface contains various operations on the stack which are as follows:1) public void push (E element): inserting element on the stack is called as a PUSH operation.2) public E pop(): deleting element from stack is called as POP operation.3) public boolean isEmpty(): function checks if stack is empty or not. This function basically required when we remove elements from stack.4) public E peek(): Access the topmost element.5) public int size(): To check size of the stack.
18. 18. Algorithms for Handling Stacka) Create a stack:1) Start.2) Assign the initial capacity to the elements through its object.3) Stop.b) To check stack is empty or not?1) Start.2) Check that size=0. If yes Then return true. else return false.3) Stop.
19. 19. c) To insert element in the stack:1) If stack is full? if yes resize the stack.2) size=size+1;3) Stack[size]= element4) Return stack/top.d) To delete element from stack:1) Check that stack is empty if yes then print stack underflow & throw exception for empty stack otherwise follow next step.2) item=stack[top].3) top=top-1.4) Return item.5) Stop.
20. 20. e) To access the topmost element from stack:1) Check that stack is empty. if yes then print stack overflow & throw exception for empty stack. otherwise follow next step.2) Return item of the top.3) Stop.Eg: usestack.java
21. 21. Queue
22. 22. • A queue is a linear list of elements in which deletions can take place only at one end, called the front, and insertions can take place only at the other end called rear.• Queues access elements in FIFO manner.• Real world example: queues in hospital, college office’s, bus stop etc.• Logically size of queue is infinite but practically it should be finite.• Operations provided for queue are present in Queue interface:1) public void add(E element): inserts element in the queue.2) public E remove(): deletes element from the queue.3) public boolean isEmpty(): function which checks queue is empty or not.4) public E element(): access the front side element.5) public int size(): to check the size of queue.
23. 23. • Types of the Queue:1) Simple or Linear Queue.2) Circular Queue.3) Priority Queue.4) Dequeue.
24. 24. Algorithm of Indexed Implementation of Queue1) To create a queue: a) Start. b) Declare space for queue that means assign capacity for the queue. c) Stop.2) To check Queue is Empty or Not? a) Start b) Call size() method & check its value=1 If yes return true else return false c) Stop
25. 25. 3) To insert element in the queue: a) Start b) Check queue is full or not by its size if yes then call resize() function for extra size c) Add element by rear size. d) Check that rear < total element-1 if yes increase rear size by 1. else rear=0 e) Stop.
26. 26. 4) To delete element from queue: a) Start b) check that queue is empty or not by checking its size. if yes then throw exception c) Item=queue[Front] d) check that Rear=Front if yes then Rear=0. Front=0. e) Check that front==total elements length if yes then front=0 f) stop.
27. 27. 5) To count size of the Queue: a) Start. b) Check that front <=rear If yes Return rear-front else return rear-front + element.length c) Stop.
28. 28. Applications of Queue1) Queues are more useful in OS: a) Processor management creates ready queue of the process by the CPU Scheduler Algorithm. b) Batch Processing. c) File Manager. d) Job scheduling & Device scheduling. e) Every IO devices has their own queue to collect requests from different applications or processing.2) Queues are used to traversing all nodes of the graph.3) Queues are used to traversing all nodes of the tree.4) It is also used in different Artificial Intelligent programs.