Darshan Institute of Engineering & Technology for Diploma studies Unit 4

Similar documents
The time and space are the two measure for efficiency of an algorithm.

Insert. SEMCOM Page 1 of 11

Ashish Gupta, Data JUET, Guna

First of all, it is a variable, just like other variables you studied

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

UNIT-I Fundamental Notations

MODULE 3: LINKED LIST

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Linked List. April 2, 2007 Programming and Data Structure 1

Pointers. Introduction

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

MYcsvtu Notes LECTURE 34. POINTERS

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

UNIT IV 4 LINKED LIST

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

BBM 201 DATA STRUCTURES

[0569] p 0318 garbage

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

DC54 DATA STRUCTURES DEC 2014

CSCI 171 Chapter Outlines

Data Structure Series

Subject: Fundamental of Computer Programming 2068

Cpt S 122 Data Structures. Data Structures

1 P age DS & OOPS / UNIT II

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

IV Unit Second Part STRUCTURES

C-LANGUAGE CURRICULAM

Pointers, Dynamic Data, and Reference Types

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Variation of Pointers

Linked Lists. .. and other linked structures. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

CA341 - Comparative Programming Languages

CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar

Introduction to Data Structure

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

In Java we have the keyword null, which is the value of an uninitialized reference type

Chapter 19 Data Structures -struct -dynamic memory allocation

Advanced C Programming and Introduction to Data Structures

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Write a C program using arrays and structure

Pointers and File Handling

Algorithms & Data Structures

Fundamental of Programming (C)

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

Introduction to Data Structures and Algorithms

Introduction to Programming Using Java (98-388)

Dynamic memory allocation

Self-referential Structures and Linked List. Programming and Data Structure 1

Unit 7. Functions. Need of User Defined Functions

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Introduction to Data Structures. Systems Programming

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

Arrays, Pointers and Memory Management

Algorithms, Data Structures, and Problem Solving

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

CP2 Revision. theme: dynamic datatypes & data structures

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Programming and Data Structure Solved. MCQs- Part 2

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

Chapter 19 Data Structures

ECE 15B COMPUTER ORGANIZATION

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Reg. No. : Question Paper Code : 27157

NCS 301 DATA STRUCTURE USING C

ARRAYS(II Unit Part II)

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

Language comparison. C has pointers. Java has references. C++ has pointers and references

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Fundamentals of Programming

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Data Structure with C. List

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

Database Systems II. Record Organization

MODULE V: POINTERS & PREPROCESSORS

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

UNIT-1. Chapter 1(Introduction and overview) 1. Asymptotic Notations 2. One Dimensional array 3. Multi Dimensional array 4. Pointer arrays.

INTRODUCTION 1 AND REVIEW

Memory Allocation in C

High Performance Programming Programming in C part 1

DECLARAING AND INITIALIZING POINTERS

C Refresher, Advance C, Coding Standard, Misra C Compliance & Real-time Programming

Introduction to Linked Lists

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

Array Initialization

Run-time Environments - 3

CS61, Fall 2012 Section 2 Notes

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

M.CS201 Programming language

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Transcription:

Pointer A pointer is a variable that contains address or location of another variable. Pointer is a derived data type in C. Pointers contain memory address as their values, so they can also be used to access and manipulate data stored in memory. void main() { int a=0, *p; p = &a; // Assign memory address of a to pointer variable p printf( %d %d %d, a, *p, p); } Output: 0 0 5000 p is integer pointer variable & is address of or referencing operator which returns memory address of variable * is indirection or dereferencing operator which returns value stored at that memory address & operator is the inverse of * operator ( x = a is same as x = *(&a)) Variable Value Address 0 a 5000 5000 P 5048 Declaration of pointer, Syntax: data_type *pt_name; Example: int *p, float *q, char *c; ) The asterisk (*) tells that the variable pt_name is a pointer variable 2) pt_name needs a memory location to store address of another variable 3) pt_name points to a variable of type data_type Initialization of the pointer, Structure int a=5, x, *p; // Declares pointer variable p and regular variable a and x p = &a // Initializes p with address of a x = *p; // p contains address of a and *p gives value stored at that address. Structure is a collection of logically related data items of different data types grouped together under a single name. Structure is a user defined data type. Structure helps to organize complex data in a more meaningful way. Dept: CE DS (3330704) Vishal Makwana

Syntax of Structure: struct structure_name { data_type member; data_type member2; }; struct is a keyword. structure_name is a tag name of a structure. member, member2 are members of structure. Below Program reads and prints employee information: #include <stdio.h> struct employee { int emp_no; char name[20]; int age; }; void main() { struct employee e; printf( Enter employee details\n ); scanf( %d %s %d, &e.emp_no, e.name, &e.age); printf( Employee data is::\n ); printf( %d \n %s\n %d\n, e.emp_no, e.name, e.age); printf( It occupies %d bytes in memory \n, sizeof (struct employee)); } There are two ways to create variables of structure type as discussed below: ) The structure is declared as below: struct employee { int emp_no; char name[20]; int age; }; After making above declaration variable of employee type are created are follows: struct employee e,e2,e3; In above declaration of structure variables e, e & e3, each variable occupies separate memory. Total memory occupy by each variable is 24 bytes as 2 + 20 + 2 for each data member respectively. 2 Dept: CE DS (3330704) Vishal Makwana

2) The structure prototype and variables are defined together as follow: struct employee { int emp_no; char name[20]; int age; }e,e2,e3; Initialization of structure For initialization consider structure of employee. struct employee e = {0, xyz, 8}; OR e.emp_no = 0; strcpy (e.name, xyz ); e.age = 8; Structure using Pointers Structures and pointers are used together to design complex data structure used in larger and complex application using pointer of any type as member of structure, pointer to point to structure variable or array of structure, to create structure dynamically at run time etc. Example: Struct point { Int x; Int y; }; struct point *p; struct point p= { 6,8}; p = &p; Now, p points to p and can be used to access x and y coordinates of p. When pointer is used to access structure members we have to use arrow ( -> ) operator, following syntax is used: Structure_pointer -> member_name Similarlly we can use, Scanf ( %d %d, &p->x, &p->y); Printf ( %d\n, p->x); It is also possible that once pointer to structure is declared, structure can be created dynamically as follows: p = (struct point *) malloc (sizeof (stuct point)); Now p points to a structure of type point. 3 Dept: CE DS (3330704) Vishal Makwana

Dynamic Memory Allocation Using array, we can represent a linear data structure as a sequential allocation (static implementation) method of storage. But this method of allocation is suitable for certain application only. There are so many application where sequential allocation method is not unacceptable such as, ) Unpredictable storage requirement 2) Extensive manipulation of storage data Example: In a program operations such as insertion and deletion are performed frequently on the data. So Linked list (Dynamic memory allocation) method of storage is efficient for computer storage. The dynamic allocation does not allocate the memory at compile time but allocates memory at run time. Hence, there is no wastage of memory. The dynamic allocation increases the execution time of the program because of run time allocation, but it gives total control of memory in hands of programmer. C provides memory management function in library using <malloc.h> header file. The function which is widely used to allocate memory at run time is malloc. Void *malloc(int); Where int argument is number of bytes to be allocated. It returns an address of starting of the block of memory which is allocated. ptr = (target_type *)malloc (no_of_bytes); Above statement returns void pointer to block of memory of size no_of_bytes which must be casted to target data type which is the type of pointer. Then the pointer ptr can be used to access memory block allocated by malloc function. Consider following examples, ) int *p; p = (int *) malloc (sizeof (int) ) ; The above statement allocates memory for one integer. To store the value, we can write *p = 0; 2) float *f; f = (float *) malloc (sizeof (float)); This creates a floating point variable pointed by pointer variable f dynamically. 3) char *str; str = (char *) malloc (0 * sizeof(char)); This creates a string of maximum 0 character pointed by str. 4) Int *p; P = (int *) malloc (5 * sizeof(char)); This creates an array of 5 integers pointed by p. Once an array is created using above statement either subscripted variable or pointer notation can be used to access the individual elements of the array. 4 Dept: CE DS (3330704) Vishal Makwana

Linked List Presentation A simple way to represent a linear list is to expand each node to contain a link or pointer to the next node. This representation is called a one way chain or singly Linked List. It can be displayed as below: NULL (Single Linked Linear List) The variable contain the address of first node of the list. A linked list is a collection of nodes and each node has two pair: ) Information (INFO) - It contains the actual element of the list. 2) Address or pointer to next node (LINK) - It contain the address of the next node in the list INFO Infomation LINK Address (Node) In a linear list each node contains a link or pointer to the next node. The pointer contains the address of location where next information is stored. LINK of the last node contain a special value known as a NULL, which indicate end of the list. The list with no node is called empty list. The empty list has = NULL. For any operation, first step is to check whether list is empty or not. If list is empty and if we try to delete a node List underflow error occurs. Linked List Example A 200 B 2002 C 202 D Null (2000) 200 2002 202 Types of Linked List ) Singly Linked List 2) Singly Circular Linked List 3) Doubly Linked List 4) Doubly Circular Linked List 5) Ordered Linked List 5 Dept: CE DS (3330704) Vishal Makwana

Basic Operation on Singly Linked Linear List ) To create a linked list 2) Traversing a linked list 3) Insert new node at beginning of linked list 4) Insert new node at the end of linked list 5) Insert a node at any location or in between the list 6) Inserting a node into an ordered linear list 7) Delete a first node(at beginning ) of linked list 8) Delete a last node(at end) of linked list 9) Delete a node on basis of node number 0) Searching element in linked list ) Count the number of nodes in linked list To perform above operation following fundamental things are to be considered: Linked list has a pointer variable which stores address of the first node of the list. Likewise, we have a pointer variable AVAIL which stores the address of the first free space of the free pool (Which is linked list of all the free memory cells). This free pool is called availability list. Whenever a node is to be inserted in a list, the memory address pointed by AVAIL pointer will be taken from the availability list and used to store the information. After the insertion, the next available free space s address will be stored in AVAIL pointer. When we delete a particular node from an existing list, the space occupied by it must be given back to the free pool. So that the memory can be reused by some other program. The advantage of this scheme is memory management. Avail New Avail Null (Before) [Availabiitility List] (After) [To free a Node Form avail stack] 6 Dept: CE DS (3330704) Vishal Makwana

Linked List Creation Algorithm: We consider some variable for writing a algorithm New_Node -> It is temporary variable of node type. Avail -> It is a top pointer of availability of stack of nodes. First -> It is the pointer which will points to front (first) node having INFO & LINK portion X -> It is the variable which will store the value/item at information part ALGORITHM: Create a Linked List Step : [Initially list is empty or check whether any node exist or not] If(First=null) Write( Linked list is empty ); First = NULL; Step 2: [To create new node] [Remove free node from availability list] If(First = NULL) New_Node <- Avail; Avail <- LINK(Avail); Step 3: [Assign a value to information part of node] INFO (New_Node) <- X Step 4: [Assign null to the address part for node to indicate end of list] LINK(New_Node) <- NULL Step 5: [Assign address of first node to first variable] First <- New_Node Step 6: [Return the created node] Return(First) Inserting a node at the beginning of the list: 7 5 3 9 New_Node to be inserted at the beginning of the list Allocates memory for the new node and initialize its data part to 9. 9 7 5 3 Add the new node as the first node of the list. Now the LINK part of the new node contains address of. 7 Dept: CE DS (3330704) Vishal Makwana

9 7 5 3 Now is made to point to the first node of the list. ALGORITHM: Insert (x, First) Step : [Check for availability list underflow] if(avail = NULL) Write( Availability stack underflow ) Step 2: [Check whether any nodes exist or not in a list] (A)[If there is not any node in the list remove freed node from availability list] () if(first = NULL) New_Node <- Avail Avail <- LINK(Avail) (2)[Assign data to node] INFO(New_Node) <- x (3)[Set link of node to null] LINK(New_Node) <- NULL (B)[If there is any node available in list and insert a new node] ()[Assign data to node] New_Node <- Avail Avail <- LINK(Avail) (2)[Assign data to node] INFO(New_Node) <- x (3)[Set link of node] LINK(New_Node) <- First Step 3: [Assign the address of temporary pointer to first pointer] First <- New_Node Step 4: [finished] Inserting a node at the end of the list: 7 5 3 We take a which will initially point to. 7 5 3, 8 Dept: CE DS (3330704) Vishal Makwana

7 5 3 Move so that it points to the last node of the list. 7 5 3 9 Add the new node after the node pointed by. This is done by storing the address of the new node in the LINK part of. Inserting an element at the end of the Linked List ALGORITHM: InsEnd (x, First) Step : [Check for availability list underflow] If(Avail = NULL) Write( Availability stack is underflow ) Step 2: [Create node and set the data and link portion of node] ()[Remove free node from availability stack] New_Node <- Avail Avail <- LINK(Avail) (2)[Assign data to information part of node] INFO(New_Node) <- x (3)[Set the link portion to null because it is last node at end] LINK(New_Node) <- NULL Step 3: [If there is no node in list] If(First ==NULL) First <- New_Node Step 4: [If there is any node in the list and put new node at the end of list] ()[Assign address of first node to ] <- First (2)[Traverse the list till last node is reached] Repeat while( LINK() <> NULL) <- LINK() (3)[Set link of last node to new node] LINK() <- New_Node Step 5: [Finished] 9 Dept: CE DS (3330704) Vishal Makwana

Inserting a node after given node in the list: 7 5 3 Take two pointer variable and PRE and initialize them with so that, and PRE point to the first node of the list. 9 7 5 3,,PRE Move and PRE until the data part of the PRE = Value of the node after which insertion has to be done. PRE will always point to the node just before. 7 5 3 PRE Add new node in between the nodes pointed by PRE and. 7 5 3 9 New_Node 7 9 5 3 Inserting a node after a given node in the linked list ALGORITHM: InAfterLoc (x, First) Step : [Check for availability list underflow] If(Avail = NULL) Write( Availability stack is underflow ) Step 2: [Create node and set the data and link portion of node] ()[Remove free node from availability stack] New_Node <- Avail Avail <- LINK(Avail) (2)[Assign data to information part of node] INFO(New_Node) <- x 0 Dept: CE DS (3330704) Vishal Makwana

Step 3: [Read location] Read N Step 4: [Set two pointer and PRE to First] = First PRE = Step 5: [Reach to specific location] Repeat while (INFO(PRE) <> N) PRE <- <- LINK() Step 6: [Insert New_Node after given location] LINK(PRE) <- New_Node LINK(New_Node) <- Step 7: [Finished] Inserting a node before given node in the list: 7 5 3 Take two pointer variable and PRE and initialize them with so that, and PRE point to the first node of the list. 9 7 5 3,,PRE Move and PRE until the data part of the = Value of the node before which insertion has to be done. PRE will always point to the node just before. 7 5 3 PRE Add new node in between the nodes pointed by PRE and. 7 5 3 9 New_Node 7 9 5 3 Dept: CE DS (3330704) Vishal Makwana

ALGORITHM: InBeforeLoc (x, First) Step : [Check for availability list underflow] If(Avail = NULL) Write( Availability stack is underflow ) Step 2: [Create node and set the data and link portion of node] ()[Remove free node from availability stack] New_Node <- Avail Avail <- LINK(Avail) (2)[Assign data to information part of node] INFO(New_Node) <- x Step 3: [Read location] Read N Step 4: [Set two pointer and PRE to First] = First PRE = Step 5: [Reach to specific location] Repeat while(info() <> N) PRE <- LINK() <- Step 6:[Insert New_Node before given Location] LINK(PRE) <- New_Node LINK(New_Node) <- Step 7: [Finished] Inserting a new node into sorted linked list 5 7 8 Take two pointer variable and PRE and initialize them with so that, and PRE point to the first node of the list. 6 5 7 8,,PRE Move and PRE until you find the data part of a node whose value is greater than NUM. PRE will always point to the node just before. 2 Dept: CE DS (3330704) Vishal Makwana

5 7 8 PRE Add new node in between the nodes pointed by PRE and. 5 7 8 6 New_Node 5 6 7 8 Inserting node into sorted linked list ALGORITHM: InsOrder (x, First) Step : [Check for availability list overflow] If(Avail = NULL) Write( Availability stack is overflow ) Step 2: [Create node and set the data and link portion of node] ()[Remove free node from availability stack] New_Node <- Avail Avail <- LINK(Avail) (2)[Assign data to information part of node] INFO(New_Node) <- x Step 3: [Read location] Read N Step 4: [Set two pointer and PRE to First] = First PRE = Step 5: [Reach to specific location in ordered list] Repeat while (INFO()< N) PRE <- <- LINK() Step 6: [Insert New_Node at given location] LINK(PRE) <- New_Node LINK(New_Node) <- Step 7: [Finished] 3 Dept: CE DS (3330704) Vishal Makwana

Deleting a first node of the linked list 7 5 3 9, 7 5 3 9 Deleting a first node from given linked list ALGORITHM: DelFirst (x, First) Step : [Check for empty list] If (First = NULL) Write ( Linked list is empty and inderflow ) Step 2: [Delete first node] If (LINK(First) = NULL) Free (First) [Now list is empty] First <- NULL Step 3: [If more than one node exist] () [Assign address of first node to ] <- First (2) [Move First pointer to second node] First <- LINK() (3) [Delete node & free space] Free () Step 4: [Finished] Deleting last node of the given list: 7 5 3 9 Take two pointer & PRE variables which will initially point to.,,pre 7 5 3 9 4 Dept: CE DS (3330704) Vishal Makwana

7 5 3 9 PRE The pointer variables will be moved to point to the last node of the linked list. So that the memory occupied by it can be freed. We also store NULL in the last node which was second to the last node initially. Deleting a last node from given linked list ALGORITHM: DelFirst (x, First) Step : [Check for empty list] If (First = NULL) Write ( Linked list is empty ) Step 2: [Assign address of first node to ] <- First Step 3: [Traverse the list till last node is reached] Repeat while (LINK() <> NULL) PRE <- <- LINK() Step 4: [Assign link of PRE to NULL which will terminates the list] LINK(PRE) <- NULL Free () Step 5: [Finished] Deleting a given node from linked list 7 5 3 9 Take two pointer & PRE variables which will initially point to. 7 5 3 9,,PRE 7 5 3 9 PRE The pointer variables will be moved to point to the nodes that contain NUM. 7 5 3 9 PRE 5 Dept: CE DS (3330704) Vishal Makwana

7 3 9 Deleting a given node from the linked list ALGORITHM: DelLoc (x, First) Step : [Check for empty list] If (First = NULL) Write ( Linked list is empty ) Step 2: [Read position] Read N Step 3:[Assign first node address to & PRE] <- First PRE <- Step 4: [Reached at specific location] Repeat while( INFO() <> N) PRE <- <- LINK(PRE) Step 5:[Delete node & free space] LINK(PRE) <- LINK() Free () Step 6: [Finished] Searching node from given linked list, 7 5 3 9 Here - > INFO =. Since -> INFO!= 5, we move to the next node. 7 5 3 9 Here - > INFO = 7. Since -> INFO!= 5, we move to the next node. 7 5 3 9 Here - > INFO = 5. Since -> INFO!= 5, POS =. POS is now stores the address of the node that contains the NUM. 6 Dept: CE DS (3330704) Vishal Makwana

ALGORITHM: Search (x, First) Step : [Check for empty list] If(First = NULL) Then Write( List is empty & node not found ) Step 2:[Initialize] Flag <- 0 <- First Step 3: [Traverse entire list for search X] Repeat while (LINK() <> NULL) If(INFO () == X) Then Flag <- <- LINK() Else <- LINK() Step 4: [Check for node found or not] If(Flag =) Then Write( Node found ) Else Write( Node not found ) Step 5: [Finished] Count number of node in the linked list ALGORITHM: Count(First) Step : [Initialize] count <- 0 <- First Step 2: [Traverse the list until end of the list] Repeat while (LINK() <> NULL) count <- count + <- LINK() Step 3: [Display Count] Write(count) Step 4: [Finished] 7 Dept: CE DS (3330704) Vishal Makwana

Circular Linked List The pointer of the last node contains the address of the first node this type of linked list is called Circular Linked List. Circular Linked List From above diagram, we can see that the address of last node (LINK) doesn t contain NULL value. Difference between Circular Linked List and Singly Linked List Every node is accessible from a given node that is from this given node, all nodes can be reached by chaining through the list. To delete a node from a singly linked list. It is require to have the first node s address. Such a requirement does not exist for a circular linked list. Certain operation such as splitting, concatenation becomes more efficient in the circular list. The disadvantage of a circular list is, it is possible to get into an infinite loop. In circular linked list, the detection of the end by placing a special node which can be easily identified in the circular list is called a list head. (Circular Linked List) In above figure the variable HEAD indicates the address of the list head. The INFO field of list head is not used which is shown by shading the field. This list can never be empty, so there is no need to test for empty list in circular list algorithm. An empty list is represented by having link (Head) = Head. Basic Concepts on Doubly Linked Linear Lists In Linear (Singly & Circular Linked) list structure, traversing is possible in one direction only. Sometimes, it is required to traverse the list in either direction, forward or backward. This property of linked list has two links field in a node instead of one. The links are denoted as predecessor and successor of a node. The link denoting the predecessor (Previous) of node is called left link and successor (Next) is called right link. A list containing this type of node is called doubly linked list or two way chain. L (Prev) Doubly Linked List R (Next) 8 Dept: CE DS (3330704) Vishal Makwana

In above fig. L and R as pointers variable which represent left most node and right most node in the list. A doubly linked list as a collection of nodes, each node having three fields, ) Pointer to previous node (Pointer to Predecessor(prev)) 2) Information Field (INFO) 3) Pointer to next node (Pointer to Successor (Next)) Next INFO Previous (Node of double Linked List) Consider following variables to write an algorithm of the doubly linked list: -> It is pointer which will points to front (first) node having INFO & LINKS portion. Prev -> It is pointer which will points to previous node having INFO, Next & Prev portion. Next -> It is pointer which will points to next node having INFo, Next & Prev portion. New_Node -> It is temporary variable of node type. X -> It is the variable which will store the value/item at information part. Inserting a new node at the beginning of the Doubly Linked List 5 7 Add new node before the node. Now New_Node becomes the first node of the list. 9 5 7 Inserting new node at the beginning of the Doubly linked List Algorithm: Doubly_FInsert(, X, Next, Prev) Step : [Check for availability list under flow] if(avail = NULL) Write( Availability stack underflow ) 9 Dept: CE DS (3330704) Vishal Makwana

Step 2: [Remove freed node availability list] New_Node <- Avail Avail <- LINK(Avail) Step 3: [Check whether any nodes exist or not in a list] (A)[If there is not any node in the list] if( = NULL) ) [Assign data to node] INFO(New_Node) <- X 2) [Set Prev & Next of node to null] Prev(New_Node) <- NULL Next(New_Node) <- NULL (B)[If there is any node available in list] ) [Assign data to node] INFO(New_Node) <- X 2) [Set Prev & Next of the new node] Prev() <- New_Node Prev(New_Node) <- NULL Next(New_Node) <- First Step 4: [Assign the address of temporary pointer to first pointer] <- New_Node Step 5: [finished] Inserting a new node at the end of the Doubly Linked List 5 7 Take a pointer variable and make it point to the first node of the list., 5 7 Move so that it points to the last node of the list. Insert new node after the node pointed by the. 5 7 9 Inserting a new node at the end of the Doubly Linked List 20 Dept: CE DS (3330704) Vishal Makwana

Algorithm: Doubly_EInsert(, X, Next, Prev) Step : [Check for availability list underflow] if(avail = NULL) Write( Availability stack underflow ) Step 2: [Remove freed node availability list] New_Node <- Avail Avail <- LINK(Avail) Step 2: [Check whether any nodes exist or not in a list] (A)[If there is not any node in the list] if( = NULL) ) [Assign data to node] INFO(New_Node) <- X 2) [Set Prev & Next of node to null] Prev(New_Node) <- NULL Next(New_Node) <- NULL (B)[If there is any node available in list] ) [Remove freed node from availability list] New_Node <- Avail Avail <- LINK(Avail) 2) [Take temporary pointer which is point to ] <- 3) [To reach at the end of the list] Repeat while (Next()!= NULL) <- Next() 4) [Assign data to node] INFO(New_Node) <- X 5) [Set Prev & Next of the new node] Prev(New_Node) <- Next() <- New_Node Next(New_Node) <- NULL Step 3: [finished] Inserting a new node after a given node in the Doubly Linked List 5 7 Take a pointer variable and make it point to the first node of the list. 5 7, 2 Dept: CE DS (3330704) Vishal Makwana

5 7 5 7 9 Inserting a New_Node after. 5 9 7 Inserting a new node after a given node in the Doubly Linked List Algorithm: Doubly_AfterLocInsert(, X, Prev, Next) Step : [Check for availability list underflow] if(avail = NULL) Write( Availability stack underflow ) Step 2: [Read location] Read NUM Step 3: [Remove freed node availability list] New_Node <- Avail Avail <- LINK(Avail) Step 4: [Set INFO part of the New_Node & Take temporary pointer which is point to ] INFO(New_Node) <- X <- Step 5: [To reach at the specific location in the list] Repeat while (INFO()!= NUM) <- Next() Step 6: [Insert New_Node after & set Prev and Next of the New_Node] Next(New_Node) <- Next () Prev(New_Node) <- Next() <- New_Node Step 7: [Finished] 22 Dept: CE DS (3330704) Vishal Makwana

Inserting a new node before a given node in the Doubly Linked List 5 7 Take a pointer variable and make it point to the first node of the list., 5 7 5 7 Move until the INFO part of = NUM before which the node has to be Inserted. 5 7 9 Inserting the new node in between the node pointed by and the node preceding it. 9 5 7 Inserting a new node before given node in the Doubly Linked List Algorithm: Doubly_BeforeLocInsert(, X, Prev, Next) Step : [Check for availability list underflow] if(avail = NULL) Write( Availability stack underflow ) Step 2: [Read location] Read NUM Step 3: [Remove freed node availability list] New_Node <- Avail Avail <- LINK(Avail) 23 Dept: CE DS (3330704) Vishal Makwana

Step 4: [Set INFO part of the New_Node & Take temporary pointer which is point to ] INFO(New_Node) <- X <- Step 5: [To reach at the specific location in the list] Repeat while (INFO()!= NUM) <- Next() Step 6: [Insert New_Node before & set Prev and Next of the New_Node] Prev(New_Node) <- Prev () Next(New_Node) <- Step 7: [Assign Prev portion of the node whose address is next portion of the New_Node] if(prev() = NULL) <- New_Node else Next(Prev()) <- New_Node Step 7: [Finished] Deleting the first node of the Doubly Linked List 9 5 7 Free the memory occupied by the first node of the list and the second node of the list becomes first node. 5 7 Deleting first node of the Doubly Linked List Algorithm: Doubly_FDelete(, Prev, Next) Step : [Check for empty list] if( = NULL) Write( Linked list is underflow ) Step 2: [Take temporary pointer which is point to ] <- Step 3: [Assign links so that second node becomes first node of the list] First <- Next() Prev() <- NULL Step 4: [Freed memory allocated to ] Free () 24 Dept: CE DS (3330704) Vishal Makwana

Step 5: [Finished] Deleting the last node of the Doubly Linked List 5 7 9 Take a pointer variable and make it point to the first node of the list. 5 7 9, Move so that it points to the last node of the list. 5 7 9 Free the memory occupied by the node pointed by the and store NULL in it preceding node. 5 7 Deleting the last node of the Doubly Linked List Algorithm: Doubly_FDelete(, Prev, Next) Step : [Check for empty list] if( = NULL) Write( Linked list is underflow ) Step 2: [Take temporary pointer which is point to ] <- Step 3: [To reach at the end of the list] Repeat while (Next()!= NULL) <- Next() Step 3: [Assign Prev & Next of to the second last node of the list] Next(Prev()) <- NULL Step 4: [Freed memory allocated to ] Free () 25 Dept: CE DS (3330704) Vishal Makwana

Step 5: [Finished] Deleting the node after given node in the Doubly Linked List 5 9 7, Take a pointer variable and make it point to the first node of the list. 5 9 7 Move until the INFO part of = NUM after which the node has to be Deleted. 5 9 7 5 7 Deleting the node after a given node in the Doubly Linked List Algorithm: Doubly_AfterLocDelete(, Prev, Next) Step : [Check for empty list] if( = NULL) Write( Linked list is underflow ) Step 2: [Take temporary pointer which is point to ] <- Step 3: [To reach at the specific location in the list] Repeat while (INFO()!= NUM) <- Next() Step 4: [Delete node after a node pointed by ] Temp <- Next() Next() <- Next(Temp) Prev(Next(Temp)) <- Step 4: [Freed memory allocated to Temp] Free (Temp) 26 Dept: CE DS (3330704) Vishal Makwana

Step 5: [Finished] Advantages and Disadvantages of Linked List Advantages: A linked list use pointer & dynamic memory allocation technique, so it allocates memory at run time and save the memory space. In static memory allocation, insertion and deletion operation is very slow and complicated where using linked list it is very easy and no any shifting of memory location during insertion & deletion operations. In Circular linked list every node is accessible from the given node. Concatenation operation and splitting operation become fast in Circular linked list. We can traverse in both directions in a list using doubly linked list. Disadvantages: The disadvantage of Circular linked list is, without some care in processing, it is possible to get into infinite loop. In case of Doubly linked list, it consume more memory space compare to Singly and Circularly linked list because it has two pointer Next and Previous to hold the address of next & previous node. In linked list it is very complicated to calculate the address of any node. The searching and sorting operation become very complicated in linked list. Application of Linked List For Polynomial representation, It means in addition/subtraction /multiplication.. of two polynomials. Creation of Symbol table. In Dynamic Memory Management, it means allocation and releasing memory at runtime Representation of Sparse Matrix. Explanation of Polynomial manipulation and representation The polynomial equations are algebraic expression. The form of this expression is as below, A n x n + A n- x n- + A n-2 x n-2 + + A 2 x 2 + A x + A 0 x 0 For example, 30x 3 + 20x 2 + 5x + where a0=, a=5, a2=20, a3=30 Where A i is Co-efficient. For above expression, four nodes are required to store the value of a0,a,a2 & a3. Each node contains three part, ) Co- efficient 2) Exponent 3) Address of next node. 30 3 2000 20 2 3000 5 4000 0 NULL Address: 000 2000 3000 4000 27 Dept: CE DS (3330704) Vishal Makwana