Data Structure Dr. Tauheed Ahmed Lecture 2: Introduction
2 C ontent s Overview of Non-Primitive Data Structure
Linked Lists: Introduction A lists (Linear linked list) can be defined as a collection of variable number of data items Ex. 2.1: Brokerage Firm Data Suppose a brokerage firm maintains a file where each record contains a customer’s name and his or her salesman S. No. Customer Salesperson 1 Adams Smith 2 Brown Ray 3 Clark Jones 4 Drew Ray 5 Evans Smith 6 Farmer Jones 7 Geller Ray 8 Hill Smith 9 Infeld Ray Is it the most effective way to represent the data? No
Linked Lists: Introduction Let us try to solve this, so as to have following representation Ex. 2.1: Brokerage Firm Data Suppose the firm wants to list the customers for the given salesperson S. No. Customer Salesperson Pointer 1 Adams 3 2 Brown 2 3 Clark 1 4 Drew 2 5 Evans 3 6 Farmer 1 7 Geller 2 8 Hill 3 9 Infeld 2 Is it the most effective way to represent the data? No Salesperson Pointer Salesperson 1 Jones 2 Ray 3 Smith
Linked Lists: Introduction Following representation will solve the problem S. No. Customer Link 1 Adams 5 2 Brown 4 3 Clark 6 4 Drew 7 5 Evans 8 6 Farmer 7 Geller 9 8 Hill 9 Infeld Is it the most effective way to represent the data? Yes Salesperson Pointer Salesperson Start Pointer 1 Jones 3 2 Ray 2 3 Smith 1
Linked List A linked list is a way to store a collection of elements. Like an array these can be character or integers. Each element in a linked list is stored in the form of a node. Definition Pointer Structure Node
Pointers If you have a variable in your program, will give you its address in the memory Pointers (pointer variables) are special variables that are used to store addresses rather than values. Pointer Syntax int* p; ( declared a pointer p of int type. ) int *p1; int * p2; ( declared two pointers p1 and p2 ) int* p1, p2; ( declared a pointer p1 and a normal variable p2 )
Accessing Pointers Assigning addresses to Pointers int * pc, c; c = 5; pc = &c; Output: 5 Output: 1 1 What is the output ? int* pc, c; c = 5; pc = &c; printf ("%d", *pc); What is the output int* pc, c; c = 5; pc = &c; c = 1; printf ("%d", c); printf ("%d", *pc);
Structure In C programming, a struct (or structure) is a collection of variables (can be of different types) under a single name. struct Person { char name[50]; int ID No ; float salary; }; struct Person { // code }; int main() { struct Person person1, person2, p[20]; return 0; } struct Person { // code } person1, person2, p[20]; Create Struct Variables person1 and person2 are struct Person variables p[] is a struct Person array of size 20.
Node of a Linked List Each node of a linked list consists of following component struct node { int data; struct node *next; }; Node Representation A data item An address of another node
Arrays Vs Linked List Linked List Arrays
Stack A stack is also an ordered collection of elements like arrays, but it has a special feature that deletion and insertion of elements can be done only from one end called the top of the stack (TOP) Definition Due to this property, it is also called as last in first out type of data structure (LIFO).
Stack When an element is inserted into a stack or removed from the stack, its base remains fixed where the top of stack changes Insertion of element into stack is called PUSH and deletion of element from stack is called POP.
Stack: Implementation
Queue A Queue is also an ordered collection of elements that follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. Definition Due to this property, it is also called as First in first out type of data structure (FIFO).
Queue In a queue new elements are added to the queue from one end called REAR end and the element are always removed from the other end called the FRONT end. Insertion of element into queue is called Enqueue and deletion of element from queue is called Dequeue .
Queue: Implementation
Tree Tree data structure is a hierarchical structure that is used to represent and organize data in the form of parent child relationship. Definition There is a special data item at the top of hierarchy called the Root of the tree. The remaining data items are partitioned into number of mutually exclusive subset, each of which is itself, a tree which is called the sub tree . The tree always grows in length towards bottom in data structures, unlike natural trees which grows upwards.
Graph A graph G(V,E) is a set of vertices V and a set of edges E. Definition A graph data structure is a collection of nodes that have data and are connected to other nodes.
Graph An edge connects a pair of vertices and many have weight such as length, cost and another measuring instrument for according the graph. Vertices on the graph are shown as point or circles and edges are drawn as arcs or line segment. Example of graph:
Question What is the correct syntax for declaring an integer array in C? 2. What is the major limitation of arrays? They can only store integers. They require predefined size. They can only store characters. They are immutable. C B
Question What does each node in a linked list consist of? Only data Only a pointer to the next node Data and a pointer to another node None of the above 4. In a graph, what does an edge represent? single node A connection between two vertices The weight of the graph A hierarchical structure C B