CHP-5 LinkedList.

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Chapter 22 Implementing lists: linked implementations.
Pointers.
Singly Linked Lists What is a singly-linked list? Why linked lists?
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
Linked Lists Linked Lists Representation Traversing a Linked List
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Data Structures Using C++
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
Review Pseudo Code Basic elements of Pseudo code
Foundation of Computing Systems Lecture 2 Linked Lists.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Lecture - 1 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Data Type and Data Structure Data type Set of possible values for variables.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures Using C++ 2E
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
CHP - 9 File Structures. INTRODUCTION In some of the previous chapters, we have discussed representations of and operations on data structures. These.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Data Strcutures.
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.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Linked List by Chapter 5 Linked List by
CHP-4 QUEUE. 1.INTRODUCTION  A queue is a linear list in which elements can be added at one end and elements can be removed only at other end.  That.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
 Array is a data structure were elements are stored in consecutive memory location.in the array once the memory is allocated.it cannot be extend any more.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
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:
Linked List. LINKED LIST Link is collection of similar type of elements. There are two ways of maintaining a list in memory. The first way is store the.
ARRAYS IN C/C++ (1-Dimensional & 2-Dimensional) Introduction 1-D 2-D Applications Operations Limitations Conclusion Bibliography.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
UNIT-II Topics to be covered Singly linked list Circular linked list
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
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.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Data Structure By Amee Trivedi.
CHP - 9 File Structures.
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
Data Structure Interview Question and Answers
Data structures and algorithms
Review Deleting an Element from a Linked List Deletion involves:
Data Structure and Algorithms
UNIT-3 LINKED LIST.
Data Structures Interview / VIVA Questions and Answers
CSCE 210 Data Structures and Algorithms
LINKED LISTS CSCD Linked Lists.
Arrays and Linked Lists
Linked Lists.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
LINKED LIST Dr. T. Kokilavani Assistant Professor
LINEAR DATA STRUCTURES
Presentation transcript:

CHP-5 LinkedList

1.INTRODUCTION In simplest terms, a list refers to a collection of data items of similar type arranged in sequence (that is, one after another). For example, list of students' names, list of addresses.. etc. One way to store such lists in memory is to use an array. However, arrays have certain problems associated with them. As array elements are stored in adjacent memory locations. a sufficient block of memory is allocated to array at compile-time. Once the memory is allocated to array, it cannot be expanded. That is why array is called a static data structure. If the number of elements to be stored in an array increases or decreases significantly at run-time, it may require more memory space or may result in wastage of memory, both of which are unacceptable. Another problem is that the insertion and deletion of an element into array are expensive operations, since they may require a number of elements to be shifted. Because of these problems, arrays are not generally used to implement linear lists instead another data structure known as linked list is used. A linked list is a linear collection of homogeneous elements called nodes. The successive nodes of a linked list need not occupy adjacent memory locations and the linear order between nodes is maintained by means of pointers.

SINGLY LINKED LISTS a singly linked list (also called linear linked list), each node consists of two fields: inf o and next (see Figure 5.1). The info field contains the data and the next field contains the address of memory location where the next node is stored. The last node of the singly linked list contains NULL in its next field that indicates the end of list. A linked list contains a list pointer variable Start that stores the address of the first Node of the list. In case, the Start contains NULL,the list is called an empty list or a null list. Figure 5.2 shows a singly linked list with four nodes.

Memory Representation To maintain a linked list in memory, two parallel arrays of equal size are used. One array(say INFO) is used for the info field and another array (say, NEXT) for the next field of the nodes of the list. Figure 5.3 shows the memory representation of a linked list where each node contains an integer. In this figure, the pointer variable Start contains 25, that is, the address of first node of the list, which stores the value 37 in array INFO and its corresponding element in array NEXT stores 49, that is, the address of next node in the list and so on. Finally, the node at address 24 stores value 69 in array INFO and NULL in array NEXT, thus, it is the last node of the list.

Memory Allocation As memory is allocated dynamically to the linked list, a new node can be inserted anytime in the list. For this, the memory manager maintains a special linked list known as free storage list or memory bank or free pool that consists of unused memory cells. This list keeps track of the free space available in the memory and a pointer to this list is stored in a pointer variable Avail (see Figure 5.4). Note that the end of free-storage list is also denoted by storing NULL in the last available block of memory. In this figure, Avail contains 22, hence, INFO [ 2 2] is the starting point of the free storage list. Since NEXT [22] contains 26, INFO [26] is the next free memory location. Similarly, other free spaces can be accessed and the NULL in NEXT [23] indicates the end of free-storage list.

Operations These operations include traversing, searching, inserting and deleting nodes, reversing, sorting and merging linked lists. Creating a node means defining its structure, allocating memory to it and its initialization. As discussed earlier, the node of a linked list consists of data and a pointer to next node. To define a node containing an integer data and a pointer to next node in C language, we can use a self-referential structure whose definition is shown here.

Traversing Traversing a list means accessing its elements one by one to process all or some of the elements. For example, if we need to display values of the nodes, count the number of nodes or search a particular item in the list, then traversing is required. We can traverse the list by using a temporary pointer variable (say, temp),which points to the node currently being processed. Initially, we make temp to point to the first node, process that element, then move temp to point to the next node using statement temp=temp- >next, process that element and move so on as long as the last node is not reached, that is, until temp becomes NULL

Insertion Insertion in Beginning: To insert a node in the beginning oflist, the next field of new node (pointed to by nptr) is made to point to the existing first node and the Start pointer is modified to point to the new node (see Figure 5.5).

Insertion at End To insert a node at the end of a linked list, the list is traversed up to the last node and the next field of this node is modified to point to the new node. However, if the linked list is initially empty, then the new node becomes the first node and Start points to it. Figure 5.6(a) shows a linked list with a pointer variable temp pointing to its first node and Figure 5.6(b) shows temp pointing to the last node and the next field of last node pointing to the new node.

Insertion at a Specified Position

Deletion Deletion from Beginning To delete a node from the beginning of a linked list, the address of the first node is stored in a temporary pointer variable temp and Start is modified to point to the second node in the linked list. After that the memory occupied by the node pointed to by temp is deallocated. Figure 5.8 shows the deletion of node from the beginning of a linked list.

Deletion from End To delete a node from the end of a linked list, the list is traversed up to the last node. Two pointer variables save and temp are used to traverse the list, where save points to the node previously pointed by temp. At the end of traversing, temp points to the last node and save points to the second last node. Then the next field of the node pointed by save is made to point to NULL and the memory occupied by the node pointed to by temp is deallocated. Figure 5.9 shows the deletion of node from the end of a linked list.

Deletion from a Specified Position

Searching in an Unsorted List

Searching in a Sorted List

Reversing

circular linked list. A linear linked list, in which the next field of the last node points back to the first node instead of containing NULL, is termed as a circular linked list.

Insertion in Beginning

Insertion at End

Deletion from Beginning

Deletion from End

DOUBLY LINKED LISTS a singly linked list, each node contains a pointer to the next node and it has no information about its-previous node. Thus, we can traverse only in one direction, that is, from beginning to end. However, sometimes it is required to traverse in the backward direc that is, from end to beginning. This can be implemented by maintaining an additional pointer in each node of the list that points to the previous node. Such type of linked list is called doubly linked list. Each node ofa doubly linked list consists of three fields: prev, info and next (see 5.17). The info field contains the data, the prev field contains the address of the previous node and the next field contains the address of the next node.

DOUBLY LINKED LISTS The structure of a node of doubly linked list is shown here.

Insertion in Beginning

Insertion at End

Insertion at specified position

Deletion from Beginning

Deletion from a specified position

LINKED IMPLEMENTATION OF STACK

INKED IMPLEMENTATIQN-OF QUEUE

APPLICATIONS OF LINKED LIST Polynomial Addition Equivalence Relation Sparse Matrices