830 likes | 1.04k Views
Course Name: DATA STRUCTURES Course Code: 10B11CI211 Course Credits: 4 (3-1-2) Session: Jan – Jun 2013 Data Structures Team: Course Coordinator: Deepak, Nitin and Hemraj. WHAT THE COURSE IS ABOUT. Data structures is concerned with the representation and manipulation of data.
E N D
Course Name: DATA STRUCTURESCourse Code: 10B11CI211Course Credits: 4 (3-1-2)Session: Jan – Jun 2013Data Structures Team:Course Coordinator: Deepak, Nitin and Hemraj
WHAT THE COURSE IS ABOUT • Data structures is concerned with the representation and manipulation of data. • All programs manipulate data. • So, all programs represent data in some way. • Data manipulation requires an algorithm. • We shall study ways to represent data and algorithms to manipulate these representations. • The study of data structures is fundamental to Computer Science & Engineering.
PREREQUISITE • C • Enumerated data type, void data type • typedef statement • Control statements • Use of memory by a program • Specification of pointers • Memory management functions • Problems with pointers • Various aspects of user defined functions
WHAT IS DATA STRUCTURE “A Conceptual and concrete way toorganize data for efficient storage and manipulation”
WHAT IS DATA STRUCTURE A data structure is a logical and mathematical model of a particular organization of data. The choice of particular data structure depends upon following consideration: 1.It must be able to represent the inherent relationship of data in the real world. 2. It must be simple enough so that it can process efficiently as and when necessary
OVERVIEW OF DATA STRUCTURE • Basic Terms related to data organization • Data type • Meaning of data structure • Factor that influence the choice of data structure • Different data structure • Various operation performed on data structure
BASIC TERMS RELATED TO DATA ORGANIZATION • Data: Values or set of values. Eg. Observation of experiment, marks obtained by student. • Data item: A data item refers to a single unit of values. Eg. Roll no. name etc. • Entity: That has certain attribute or properties which may be assigned values. Eg. Student is an entity
BASIC TERMS RELATED TO DATA ORGANIZATION • Entity set: Collection of similar entity. Eg. Student of a class • Record: Collection of related data items. Eg. Rollno, Dob, gender, class of a particular student. • File: Collection of related record. Eg. A file containing records of all students in a class
BASIC TERMS RELATED TO DATA ORGANIZATION • Key: A key is a data item in a record that takes unique values and can be used to distinguish a record from other records. • Information: Meaningful data, coveys some meaning and hence can be used for decision making
DATA TYPE A data type is a collection of values and a set of operation that act on those values Classification of data type: 1. Primitive data type • Abstract data type • Polymorphic data type
PRIMITIVE DATA TYPE • That is predefined. It is also known as built in data type. Eg. C have built in data type int, long int, float, double, char.
ABSTRACT DATA TYPE • In computing, an abstract data type (ADT) is a specification of a set of data and the set of operations that can be performed on the data. Such a data type is abstract in the sense that it is independent of various concrete implementations. • The main contribution of the abstract data type theory is that it • (1) formalizes a definition of type (which was only intuitively hinted on procedural programming) • (2) on the basis of the information hiding principle and • (3) in a way that such formalization can be explicitly represented in programming language notations and semantics. This important advance in computer science theory (motivated by software engineering challenges in procedural programming) led to the emergence of languages and methodological principles of object-oriented programming.
POLYMORPHIC DATA TYPE • Very often in programs, a generic operation must be performed on data of different types. For example, in our bubble sort algorithm for the payroll records, when elements were found out of order in the id[] array, we needed to swap the integer elements in that array as well as the float elements in the hrs[] and rate[] arrays. If we decided to implement this swapping operation as a function, we would need to write two functions: one to swap integers, and another to swap floating point values; even though the algorithm for swapping is the same in both cases. • The C language provides a mechanism which allows us to write a single swapping function which can be used on any data type. This mechanism is called a polymorphic data type, i.e. a data type which can be transformed to any distinct data type as required..
THE STUDY OF DATA STRUCTURE INCLUDE: • Logical description of data structure • Implementation of data structure • Quantative analysis of data structure, this include amount of memory, processing time
TYPES OF DATA STRUCTURES • Linear data structure • Non linear data structure
LINEAR DATA STRUCTURE A data structure whose elements form a sequence, and every element in the structures has a unique predecessor and unique successor. Eg. Array, linked list, stack and queues.
NON LINEAR DATA STRUCTURE A data structure whose elements do not form a sequence, and there is no predecessor and unique successor. Eg. Trees, graphs
ARRAYS Collection of homogenous data elements. Arrays can be: 1. One dimensional • Two dimensional • Multi dimensional
LINKED LIST • Linked list • Linear collection of self-referential class objects, called nodes • Connected by pointer links • Accessed via a pointer to the first node of the list • Subsequent nodes are accessed via the link-pointer member of the current node • Link pointer in the last node is set to null to mark the list’s end • Use a linked list instead of an array when • You have an unpredictable number of data elements • Your list needs to be sorted quickly
[0] [1] [2] array Array A B C node Linked list linked A B C The Linked List data structure Linked lists are unbounded (maximum number of items limited only by memory)
LINKED LIST • Types of linked lists: • Singly linked list • Begins with a pointer to the first node • Terminates with a null pointer • Only traversed in one direction • Circular, singly linked • Pointer in the last node points back to the first node • Doubly linked list • Two “start pointers” – first element and last element • Each node has a forward pointer and a backward pointer • Allows traversals both forwards and backwards • Circular, doubly linked list • Forward pointer of the last node points to the first node and backward pointer of the first node points to the last node
LINKED LISTS (VARIATIONS) Basic elements: Head Node Simplest form: Linear-Singly-linked node data pointer head A A C B Head
LINKED LISTS (VARIATIONS) Circular-linked Lists The last node points to the first node of the list Strengths Able to traverse the list starting from any point Allow quick access to first and last records through a single pointer Weakness A bit complicated during insertion, needs careful setting of pointer for empty or one-node list Head A C B
LINKED LISTS (VARIATIONS) Doubly-linked Lists Each inner node points to BOTH successor and the predecessor Strengths Able to traverse the list in any direction Can insert or delete a node very quickly given only that node’s address Weakness Requires extra memory and handling for additional pointers A C B Head
LINKED LISTS (VARIATIONS) Putting together…Circular-doubly-linked lists! A C B Head
STACK • Stack • New nodes can be added and removed only at the top • Similar to a pile of dishes • Last-in, first-out (LIFO) • Bottom of stack indicated by a link member to NULL • Constrained version of a linked list • push • Adds a new node to the top of the stack • pop • Removes a node from the top • Stores the popped value • Returns true if pop was successful
DATA STRUCTURES -- STACKS A stack is a list in which insertion and deletion take place at the same end This end is called top The other end is called bottom
QUEUES • Queue • Similar to a supermarket checkout line • First-in, first-out (FIFO) • Nodes are removed only from the head • Nodes are inserted only at the tail • Insert and remove operations • Enqueue (insert) and dequeue (remove)
$ $ The Queue Operations • A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. Front Rear
TREES • Tree nodes contain two or more links • All other data structures we have discussed only contain one • Binary trees • All nodes contain two links • None, one, or both of which may be NULL • The root node is the first node in a tree. • Each link in the root node refers to a child • A node with no children is called a leaf node
TREE TERMINOLOGY level 0 1 2 3 4 There is a unique path from the root to each node. Root is a level 0, child is at level(parent) + 1. Depth/height of a tree is the length of the longest path.
A binary tree is either - empty or - consists of a root and two binary trees, one called the left subtree and one called the right subtree BINARY TREE • Each node has two successors • one called the left child • one called the right child • left child and/or right child may be empty
GRAPHSG = (V, E) a vertex may have: 0 or more predecessors 0 or more successors
sequence/linear (1 to 1) first ith last hierarchical (1 to many) graph (many to many) set abstract containers
HEAPS Heap is a binary tree that satisfy the following property: • Shape property • Order property Shape property states that heap is complete or nearly complete binary tree. Order property states that: • Either the element at any node is smallest of all of its children, called min heap • Element at any node is largest of all of its children, called max heap.
HASH TABLES • There are many application that require a dynamic structure that support only insert, search and delete operations. These operations are commonly known as dictionary operations. A hash table is an effective data structure for implementing dictionaries.
COMMON OPERATIONS ON DATA STRUCTURE • Traversal:-Accessing each element exactly once in order to process it. • Searching:-Finding the location of a given element. • Insertion:-Adding the new element to the structure. • Deletion:-Removing a existing element from the structure. • Sorting:-Arranging the elements in logical order. • Merging:-Combining the elements of two similar sorted structures into a single structure.
ENUMERATED DATA TYPE • Variable of enumerated data type enhance the readability of the program. enum boolean {false, true}; Here boolean is called tag name for the user defined data type. Then we can declare variable of this type as follows: enum boolean flag; Then we can assign value false or true to variable flag, and also we can compare the value of flag with these values.
void DATA TYPE • This is also known as empty data type, is useful in many situation; • Void functionname(int x, int y) { } Functionname() does not return any value. • Int functionname(void) { } Functionname() does not take any rgument. • Void main() { void *ptr; int x=5; ptr=&x; printf(“value pointed to pointer is now %d”,*(int*)ptr); } Void pointer cannot be directly dereferenced without type casting. This is because the compiler cannot determie the size of the value the pointer points to.
REDEFINING DATA TYPES • Using typedef statement we can make it possible to declare variables of user defined data types as with built in data types by redefining the user defined data type and giving our own name. typedef enum{false, true} boolean; The word boolean becomes name of the new defined data type. boolean flag;
CONTROL STATEMENTS • Decision making statements • if statement • if-else statement • switch statement • Looping Statements • for statement • while statement • do while statement • Jumping statements • brake statement • continue statement • goto statement
MEMORY USE IN C STACK Initialized and un initialized local variable High Memory Memory allocated with malloc(). Calloc(), and realloc() functions HEAP BSS Un initialized static variable CONST Read only variable DATA Initialized & un initialized global variables & initialized static variables TEXT Low Memory Program code
POINTER • Pointer is a variable which contains reference of another variable Address of x value of x px x Pointer px=&x
DECLARATION OF POINTER • “*” is used to declare and dereference the pointers. data type *ptvar; int *ip; /*declare ip to be pointer to an integer*/ *ip=5; /* assign 5 to the integer pointer to which ip points*/ address 5 ip
POINTER OPERATOR • Two operators: &-----address of *------at address in X=8 let x be at 100 (x at 100) Ip=&x ip contains 100 (ip at 200) a=*ip contains 8 (a at 250) 8 100 8
ASSIGNMENT IN POINTER Given Int x; Double y; Int *a,*b; double *c; a=&x; /* a now points to x*/ b=a; /*b now points to the same variable as a points */ c=&y; /* c points to y */
POINTER TO A POINTER • Variable that hold an address of a another variable that in turn holds an address of another variable, this type of variable is know as pointer to pointer. • Pointer to pointer will be declared as **ptr;
DYNAMIC MEMORY MANAGEMENT Memory management functions
DYNAMIC MEMORY ALLOCATION • Dynamic memory allocation • Obtain and release memory during execution • malloc • Takes number of bytes to allocate • Use sizeof to determine the size of an object • Returns pointer of type void* • A void* pointer may be assigned to any pointer • If no memory available, returns NULL • Example newPtr = malloc( sizeof( struct node ) ); • free • Deallocates memory allocated by malloc • Takes a pointer as an argument • free( newPtr );
calloc() The calloc() function dynamically allocates memory and automatically initializes the memory to zeroes. Example newPtr = calloc( 5,sizeof( struct node ) );