Data Structure and Algorithms

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

CHP-5 LinkedList.
Data Structures: A Pseudocode Approach with C
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
C++ for Engineers and Scientists Third Edition
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Data structures Abstract data types Java classes for Data structures and ADTs.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Data Structure Introduction.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Algorithms and Data Structures
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
UNIT-II Topics to be covered Singly linked list Circular linked list
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Generic Trees—Trie, Compressed Trie, Suffix Trie (with Analysi
Object Lifetime and Pointers
STACKS & QUEUES for CLASS XII ( C++).
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
Data Structures Using C, 2e
Data Structure By Amee Trivedi.
5.13 Recursion Recursive functions Functions that call themselves
CHP - 9 File Structures.
Top 50 Data Structures Interview Questions
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Data Structure Interview Question and Answers
Data structures and algorithms
Review Deleting an Element from a Linked List Deletion involves:
More Linking Up with Linked Lists
List ADT & Linked Lists.
UNIT-3 LINKED LIST.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Linked lists.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Data Structures Interview / VIVA Questions and Answers
Numeric Arrays Numeric Arrays Chapter 4.
CSCE 210 Data Structures and Algorithms
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Introduction to Linked Lists
Arrays and Linked Lists
Linked Lists.
Introduction to Data Structures
Chapter 16 Linked Structures
Introduction to Data Structure
Linked Lists.
Lists.
Data Structures & Algorithms
LINKED LIST Dr. T. Kokilavani Assistant Professor
Linked lists.
Sequences 08/30/17 08/30/17 Unit III. Linked Lists 1.
Dynamic Data Structures
Presentation transcript:

Data Structure and Algorithms Linked Lists

Data Structure Types arrays linked lists stacks queues trees graphs

How you choose a data Structure? Data size Type and speed of using these data Dynamic nature of data Memory size needed Time for retrieve the data Type of programming

Arrays It is one of the simplest data structures which is consists of group of homogeneous elements. An array is similar to, but different from, a vector or list (for one-dimensional arrays) or a matrix (for two-dimensional arrays). Arrays hold a sequence of data elements, usually of the same size and data type. Individual elements are accessed by their position in the array. The position is given by an index, which is also called a subscript. The index usually uses a consecutive range of integers, (as opposed to an associative array) but the index can have any ordinal set of values. Some arrays are multi-dimensional, meaning they are indexed by a fixed number of integers, for example by a tuple of four integers. Generally, one- and two-dimensional arrays are the most common.

Advantages of Arrays Arrays permit efficient random access, but not efficient insertion and deletion of elements Consequently, arrays are most appropriate for storing a fixed amount of data which will be accessed in an unpredictable fashion. Another advantage of arrays that has become very important on modern architectures is that iterating through an array has good locality of reference Arrays also are among the most compact data structures; storing 100 integers in an array takes only 100 times the space required to store an integer, plus perhaps a few bytes of overhead for the pointer to the array.

Disadvantages of Arrays it has a single fixed size, and although its size can be altered in many environments, this is an expensive operation. Dynamic arrays or growable arrays are arrays which automatically perform this resizing as late as possible, when the programmer attempts to add an element to the end of the array and there is no more space. To average the high cost of resizing over a long period of time (we say it is an amortized cost), they expand by a large amount, and when the programmer attempts to expand the array again, it just uses more of this reserved space.

Linked List 1. Definition of a linked list A linked list is a structure of data each item called node. Every node contains at least two fields: Data field to store the relevant data and pointer field, called link, to store the address of the next node in the list. The node can be sketched as follows: Link to the next node The pointer pointed to the first node in the list is stored in a separate location, called the head, while the last node, called last pointed to NULL. There are two ways of representing linked list in memory: Static representation using array. Dynamic representation using free storage

The static representation can be considered as two arrays: one array for data and other for the links. This can be shown by the following figure: Memory location 14 23 35 38 40 50 88 . . . 39 40 41 42 X . . . Array of position

This representation is two parallel arrays of equal size to store the entire linked list. On the other hand, the efficient way of representing a linked list is the dynamic method. In this method there is a memory bank ( a collection of free memory spaces), and a memory leader (a program). During the creation of the linked list, the node allocated and the leader simply searches for its address. The leader is known as dynamic memory management.

To deal with the single linked list, the following tools should be determined: The head of the list Number of items Data for each item The last node pointed to NULL The following figure is an example of a linked list:

The head is simply the address of the first node The head is simply the address of the first node. The arrow in each node represents the pointing to the next node. Since each node of a linked list has two components, so we need to declare each node as a struct, hence the definition of the struct will be as follows: head last Data *head *last

struct nod { int Data ; nod *link ; } ; To implement this linked list, we have to define the following three pointers: head which is pointed to the beginning of the linked list cur to determine the pointer of each node last to determine the pointer of the last node The corresponding variables can be declared as follows: nod *head, *cur, *last ;

2. Creating a single linked List The main idea for creating a single linked list is to determine the pointers values of head, cur and last. This can be done by means of the new operator which offers dynamic storage allocation. Therefore to create the linked list sketched above, we can use the following code:

for (int i = 1 ; i <= 4;i++ ) cur -> link = new nod; // Program Chr1_01.cpp # include<iostream.h> struct nod { int Data ; nod *link ; } ; nod *head, *cur, *last ; void create ( ) { head = new nod; cur = head; for (int i = 1 ; i <= 4;i++ ) cur -> link = new nod; cur = cur -> link; } last = cur; last -> link = NULL; main( ) { create ( );

3. Basic Operations on single Linked List I. Read Data This section discuses how to read the data for each node. The structure of the function is as follows: void read ( ) { cout<< "Enter the value for the head : " ; cin>> head -> Data ; cur = head ; while( cur->link) cout<< " Enter the values of the items : " ; cur = cur -> link ; cin>> cur -> Data ; }

Add this function to program Chr1_01 Add this function to program Chr1_01. then the main program will be as follows: main( ) { create ( ) ; read ( ) ; } Executing the program required to insert specific integer values for the: head four cur nodes, the last current node will be called last

for example, we can give the following data: 1 2, 5, 6, 15 one value for each node. The figure of the linked list will be as follows: head last 15 2 5 6 1 *head *last

II. Traversing a single linked list To traverse a single list we mean to visit every node in the list starting from the head node to the last node. The structure of the function is as follows: void write( ) { int l = 0; cur = head ; while (cur) cout<< "Data of node [ " <<l++<<" ] = " <<cur->Data<<endl; cur = cur -> link; }

Again we can add this function to the previous modified program and execute the following: main ( ) { create ( ) ; read ( ) ; clrscr ( ) ; write ( ) ; }

The main program contains the function clrscr which can be used to clear the text-mode window and places the cursor in the upper left corner. Certainly, we have to include our program the header file called conio, which contains this function. The output will be: Data of node [ 0 ] = 1 Data of node [ 1 ] = 2 Data of node [ 2 ] = 5 Data of node [ 3 ] = 6 Data of node [ 4 ] = 15

III. Insertion This section discusses how to perform functions: for inserting a new item: at the beginning of the linked list at the end of the linked list at the middle of the linked list

1. Inserting a new node at the beginning of the single linked list: The following code will be used: void adfirst(int m) { if(!head) head = new nod; head->Data = m; last = head; last -> link = NULL; } else nod *temp = new nod; temp -> Data = m; temp -> link =head; head = temp; last 1 4 9 16 head m = 0

2. Inserting a new node at the end of the single linked list: The following code will be used: void adend(int n) { if( !head) head = new nod; head->Data = n; head -> link = NULL; last = head; } else nod *temp = new nod; temp -> Data = n; last -> link = temp; last = temp; last -> link = NULL ; head 1 4 9 1 6 n = 25 last 25 last

Inserting a new node at the middle of the single linked list: The following code will be used: void adany(int me) { if( !head ) head = new nod; head->Data =me; head -> link = NULL; last = head; } else nod *temp, *ptr ; temp = new nod ; temp -> Data = me ; cur = head ; while (cur -> Data < me ) ptr = cur ; cur = cur -> link ; ptr ->link=temp; temp->link = cur ; ptr cur last head 1 4 9 16 temp 7 me =7

IV. Deletion This section discusses how to perform functions: for Deleting an item: at the beginning of the linked list at the end of the linked list at a required place

Deletion Item: The deletion of a node can be considered similarly like the insertion, there are also various cases of deletion: at the beginning of the linked list at a required place at the end of the linked list 1. To delete a node at the beginning of a single linked list we can use the following function: void defirst( ) { cur = head; head = head -> link; delete cur; }

is to give the first node (head) the name cur, as follows: Apply this function to delete the first node (head) from the following single linked list. The first statement cur = head ; is to give the first node (head) the name cur, as follows: 15 2 5 6 1 head last cur

is simply to give the node, next to the head, the name head. the second statement : head = head -> link; is simply to give the node, next to the head, the name head. The required figure is as follows: And finally the last statement delete cur; is to delete the node from the list, and the figure will be as follows: cur head last 15 2 5 6 1 last head 2 5 6 15

to get the computer result, we can execute the following main program: { create( ) ; read( ); defirst( ); clrscr( ); write( ); } and the output will be as follows: Data of node [ 0 ] = 2 Data of node [ 1 ] = 5 Data of node [ 2 ] = 6 Data of node [ 3 ] = 15

therefore, this function delete the first node from the single linked list and deallocate the memory occupied by this node. 2. To delete an existing node at the end of a single linked list we can use the following function: void delast( ) { cur = head ; while (cur -> link != last ) cur = cur -> link; delete last; last = cur ; last -> link = NULL; }

is to give the first node (head) the name cur, as follows: Apply this function to delete the last node (last) from the linked list. The first statement cur = head ; is to give the first node (head) the name cur, as follows: the second statement : while (cur -> link !=last ) cur = cur -> link; is simply to reach the last node, and to allocate the node before the last, The required figure is as follows: 15 2 5 6 1 head last cur 15 2 5 6 1 head last cur

is to delete the node last, and the figure will be as follows: the statement delete last; is to delete the node last, and the figure will be as follows: 2 5 6 1 head cur and the statement: last = cur ; is to give the name last to the node labeled cur, and the figure will be as follows: 2 5 6 1 head last and finally, the last node(last) should pointed to the NULL, this can be observed by the following statement: last ->link = NULL; the figure is shown below: 2 5 6 1 head last

to get the computer result, we can execute the following main program: { create( ) ; read( ); delast( ); clrscr( ); write( ); } and the output will be as follows: Data of node [ 0 ] = 1 Data of node [ 1 ] = 2 Data of node [ 2 ] = 5 Data of node [ 3 ] = 6

therefore, this function delete the last node from the single linked list and deallocate the memory occupied by this node. 3. To delete an existing node at any position of a single linked list we can use the following function: void deany (int me ) { nod *ptr; cur = head ; while (cur -> Data != me) ptr = cur ; cur = cur -> link ; } ptr -> link = cur -> link ; delete cur ;

is a declaration for the nodes ptr. The second statement: cur = head ; Apply this function to delete the node having the data value equal to 5 from the linked list. The first statement nod *ptr ; is a declaration for the nodes ptr. The second statement: cur = head ; is to give the first node (head) the name cur, as follows: 15 2 5 6 1 head last cur

while (cur -> Data < me) { ptr = cur ; cur = cur -> link ; } the while block: while (cur -> Data < me) { ptr = cur ; cur = cur -> link ; } is simply to reach the position of the required node, and to allocate the node before it. The corresponding figure is as follows: head ptr cur last 15 2 5 6 1

ptr -> link = cur -> link ; the statement: ptr -> link = cur -> link ; is to join the node before with the node after the indicated node (the node holding the value 5). The figure below represents these statements. head ptr cur last 15 2 5 6 1

And finally the statement: delete cur ; is to delete the indicated node from the single linked list and de allocate the memory occupied by this node. To see this we can draw the linked list as follows: Executing the program with the aid of this function, we will get the following result: last head 2 1 15 6 Data of node [ 0 ] = 1 Data of node [ 1 ] = 2 Data of node [ 2 ] = 6 Data of node [ 3 ] = 15

V. Destroy This section discusses how to Destroy linked list or Deleting all nodes in the linked list: A) void destroy() { cur = head ; while(cur) head = cur ->link ; delete cur ; } cout<<" No linked List exist \n" ; B) void destroy() { cur = head ; head = head -> link; while(cur->link) { delete cur ; head = head->link ; } cout<<" No linked List exist \n" ; }

Apply this function to destroy the single linked list. The first statement cur = head ; is to give the first node (head) the name cur, as follows: 15 2 5 6 1 head last cur

is simply to give the node, next to the head, the name head. the second statement : while(cur) { head = head -> link; is simply to give the node, next to the head, the name head. The required figure is as follows: The third statement: delete cur; is to delete the first node from the list. And finally the last statement: cur = head; is to give the new (head) node the name cur, to delete it in next loop Until reach to last node and destroy all linked list. 15 2 5 6 1 head last cur

to get the computer result, we can execute the following main program: { create( ) ; clrscr( ); Destroy( ); write( ); } and the output will be as follows: No linked List exist