CCSB364 Data Structures & Algorithms Pointer & Linked List.

Slides:



Advertisements
Similar presentations
Chapter 17 Linked Lists.
Advertisements

Stacks, Queues, and Linked Lists
EENG212 Algorithms and Data Structures
Linked Lists.
CSEB324 Data Structures & Algorithms
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Computer Programming Link List (Insertion, Printing and Deletion functions) Lecture 23.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Linked List C and Data Structures Baojian Hua
Implementation of Linked List For more notes and topics visit: eITnotes.com.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
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.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Chapter 6 LISTS AND STRINGS 1. List Definition 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Doubly Linked 3. Linked.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
Array 10 GB Hard Disk 2 GB 4 GB2 GB 3 GB DATA 4 GB Free Store data in the form of Array (Continuous memory locations) Solution-1: No Solution. Memory Insufficient.
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: 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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Lists and Strings Manolis Koubarakis Data Structures and Programming Techniques 1.
Linked List :: Basic Concepts
Lectures linked lists Chapter 6 of textbook
UNIT-3 LINKED LIST.
Linked list.
Linked lists.
CSC172 Data Structures Linked Lists (C version)
Linked Lists.
Lists.
Chapter 4 Linked Lists
Lists.
Chapter 16-2 Linked Structures
Arrays and Linked Lists
Data Structures and Programming Techniques
Linked List.
Chapter 17: Linked Lists.
Linked lists.
Linked Lists.
Presentation transcript:

CCSB364 Data Structures & Algorithms Pointer & Linked List

Introduction  If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow Size can’t be extended Wasted if unused  To overcome this – we use linked list.  To understand linked list, we must first understand the fundamentals – the pointer.

Pointer ?  Variable concept Declaring a variable Memory allocation  Pointer ? A variable which give location of other variable.  Linked List? Put a pointer in a structure, giving the next location of the next structure.  Static variable vs. Dynamic variable Static variable – declared and named in program Dynamic – created during program execution.

Understanding Pointers  Declare a pointer int *aPtr; int *aPtr = null;  Assigning a pointer aPtr = &aVar;  Read the pointer value printf(“%d “, *aPtr);

Exercise 1. Declare an integer variable a and b 2. Declare pointer variables aPtr and bPtr 3. Assign a to has a value of 100, and b to has a value of Assign aPtr to points to a 5. Assign bPtr to points to aPtr 6. By using bPtr, change value of a to be the same as b.

Linked List 123 AC  a pointer in a structure, giving the next location of the next structure.

Linked List - Declaration  Linked List consist of structure  Llist – is a pointer, pointing to a linked list structure  Next is a pointer in the linked list structure Temp Item Next Llist NULL

Structure Declaration typedef char item; typedef struct node{ item data; struct node *next; } Node;

Declaring the linked list Item Next Llist NULL  To declare a linked list Node *Llist; //Llist –pointer pointing to a node type

Linked List Operation  Create a Nod  Verify for an empty list  Traversal along the linked nodes  Insert new nodes  Delete a node

Creating New Node Node *newnode (item c) { Node *n; n = (Node *) malloc (sizeof (Node)); if ( n != NULL) { n-> data = c; n->next = NULL; } return n; }

Insert New Node at the begining Item Next Temp NULL Item Next Llist NULL Item Next Temp X Item Next Llist NULL

Inserting node in the middle Item Next Temp NULL Item Next Llist NULL Item Next CurrPtr Item Next Temp Item Next Llist NULL Item Next CurrPtr

Insert New Node - implementation void InsertNode( Node *Llist, Node *temp, Node *CurrPtr){ { if (CurrPtr ==NULL) { temp->next = Llist; Llist = temp; } else { temp->next = CurrPtr->next; CurrPtr –>next = temp; } }

Traverse The List void Traversal ( Node *Llist) { Node *Temp; Temp = Llist; while ( Temp != NULL) { printf (“data = %c", Temp->data); Temp = Temp-> next; } } Item Next Llist NULL Item Next Llist NULL Temp

Deleting first node Temp Item Next Llist NULL Temp Item Next Llist NULL X

Deleting middle or last node Temp Item Next Llist NULL Item Next CurrPtr Temp Item Next Llist NULL Item Next CurrPtr

Deleting Node - implementation void DeleteNode( Node *Llist, Node *CurrPtr) { Node *temp; if (CurrPtr ==NULL) { temp = Llist; Llist = temp->next; } else { temp = CurrPtr->next; CurrPtr –>next = temp->next; } free(temp) }

Type of Linked List  Simple one-way linked list L x1 x2 x3 x4

Type of Linked List  Circular Linked List Formed by having the link in the l ast node of a one way linked list point back to the first node. L x1 x2 x3 x4

Type of Linked List  Two Way Linked List Formed from nodes that have pointers to both their left and right neighbours in the list L x3 x1 x2

Type of Linked List  Linked List with Header Node Header points to the first node  As a marker / stopping place  Ease the deletion process of a node L x1 x2 x3 x4 L Header Node

Linked List Using Array  Older and widely used computer language (COBOL, Fortran, BASIC) do not provide facilities for dynamic storage allocation (pointers)  Workspace (several arrays hold different part of a logical record) is used for programming languages which do not support records.

Linked List Using Array  Implementation of linked list using array is preferred if: Number of entries is known in advance Few insertions or deletions Data are sometimes best treated as a linked list and other times as a contiguous

Linked List Using Array typedef char ListEntry; typedef int ListIndex; typedef struct listnode{ ListEntry entry; ListIndex next; } ListNode; typedef int Position; typedef struct list{ ListIndex head; int count; }List; ListIndex avail,lastused; ListNode workspace[10]

Array Linked List – New Node ListIndex NewNode (void) { ListIndex newindex = -1; if(avail != -1) { newindex = avail; avail = workspace[avail].next; workspace[newindex].next = 0; } else if (lastused < MAXLIST - 1) { newindex = ++lastused; workspace[newindex].next = 0; } else printf (“ Error Overflow : workspace for linked list is full”); return newindex; }

Array Linked List - Insert void InsertList ( Position p, ListEntry x, List *list) { ListIndex newindex, previous; if ( p list->count) printf(“ Error inserting into a nonexistent position”); else { newindex = NewNode(); workspace[newindex].entry = x; if (p == 0) { workspace[newindex].next = list->head; list->head = newindex; } else { SetPosition(p-1, &previous, list); workspace[newindex].next=workspace[previous].next; workspace[previous].next = newindex; } list->count ++; } }

Array Linked List - Dispose void DisposeNode (ListIndex oldindex, List *list) { ListIndex previous; if( oldindex == -1) printf(“Error : Disposing a nonexistent node”); else { if ( oldindex == list-> head) list->head = workspace[oldindex].next; else { SetPosition(CurrentPosition(oldindex,list)– 1, &previous, list); workspace[previous].next=workspace[oldindex].next; } workspace[oldindex].next = avail; avail = oldindex; } }

Array Linked List - Traverse void TraverseList (List *list) { ListIndex current; for (current= list->head ; current != -1; workspace[current].next) printf(”data of workspace[%d].entry = %c”, current, workspace[current].entry); }

Class Exercise  Write a complete program to create, insert and delete a linked list. Each list will consist of ID (an integer) and Grade (a character)  Guides Declare linked list structure Writes all the ADT funtions (newNode, InsertNode, DeleteNode, TraverseNode) In main program:  Create new node  Traverse the list  Create another node  Insert into the existing list  Traverse list  Delete the last node  Traverse list  Insert new node  Traverst list