Fall 2005 Data Structure Linked List – Single Linked List.

Slides:



Advertisements
Similar presentations
Chapter 3: Linked List Tutor: Angie Hui
Advertisements

Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Linear Lists – Linked List Representation
Chapter 4 Lists Pointers Singly Linked Lists
LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.
CSEB324 Data Structures & Algorithms
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Double Linked List Operations Dr. David Tsai 2010/4/12.
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
CSCE 3110 Data Structures & Algorithm Analysis
LIST PROCESSING.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Linked List Variations
CSCI2100B Linked List Jeffrey
SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Doubly-linked list library.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Data Structures: A Pseudocode Approach with C
Ceng-112 Data Structures I 1 Chapter 3 Linear Lists.
Computer Programming Link List (Insertion, Printing and Deletion functions) Lecture 23.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Introduction to C Programming CE Lecture 20 Insertion and Deletion with Linear Linked Lists.
Class 3: Linked Lists. cis 335 Fall 2001 Barry Cohen What is a linked list? n A linked list is an ordered series of ‘nodes’ n Each node contains some.
Insertion into a B+ Tree Null Tree Ptr Data Pointer * Tree Node Ptr After Adding 8 and then 5… 85 Insert 1 : causes overflow – add a new level * 5 * 158.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees.
CGS 3460 Pointer n To hold the location of another variable n Declaration: la type and a name with an asterisk lE.g. int *ptr; n Assigning a value lptr.
Introduction to Data Structures Systems Programming.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
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.
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.
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,
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
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.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
Data Structure & Algorithms
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer Data Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Data Structures 7th Week
Lists.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
CSCE 3110 Data Structures & Algorithm Analysis
Lists.
Linked Lists Chris Wright Winter 2006.
Chapter 16-2 Linked Structures
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Random inserting into a B+ Tree
Review & Lab assignments
Chapter 16 Linked Structures
C Programming Lecture-8 Pointers and Memory Management
Presentation transcript:

Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005W.F. Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node

Fall 2005W.F. Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node

Fall 2005W.F. Insert Node Three steps to the insertion Allocate memory for the new node and insert data Point the new node to its successor. Point the new node s predecessor to it.

Fall 2005W.F. Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node

Fall 2005W.F. Insert into Empty List Creat a new empty list : Define the node s structure Creat a new empty list list_pointer ptr = NULL; typedef struct list_node *list_pointer; typedef struct list_node{ char data[4]; list_pointer link; } list_node ; ptr NULL

Fall 2005W.F. Insert into Empty List Insert a new node into empty list temp = (list_pointer) malloc (sizeof(list_node)); ptr = temp; ptr Address of first node NULL ptr->dataptr->link NULL temp

Fall 2005W.F. Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node

Fall 2005W.F. Insert at Beginning (1/2) 20 ptr 30 NULL 10 temp 20 ptr 30 NULL 10 Allocate memory for the new node and insert data Point the new node to its successor. Point the new node s predecessor to it. Before insert After insert

Fall 2005W.F. Insert at Beginning (2/2) #include list_pointer temp; temp = (list_pointer) malloc (sizeof(list_node)); temp->data = 10; if(*ptr){ temp->link = *ptr; *ptr = temp; } else{ temp->link = NULL; *ptr = temp } temp 30 NULL 20 ptr 10 #include //page 144

Fall 2005W.F. Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node

Fall 2005W.F. Insert in Middle (1/2) NULL ptr 10 temp Insert 20 between 10 and 30

Fall 2005W.F. Insert in Middle (2/2) ptr 10 temp NULL node void insert (list_pointer *ptr, list_pointer node) { if(*ptr){ temp->link = node->link; node->link = temp; } else{ temp->link = NULL; *ptr = temp } }

Fall 2005W.F. Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node

Fall 2005W.F. Insert at End 20 ptr temp NULL node void insert ( …, … ) { … if(*ptr){ temp->link = NULL; node->link = temp; } … }

Fall 2005W.F. Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node

Fall 2005W.F. Delete node (1/2) Delete node : locate the node first. Assume we have three points ptr : point to the start of the list node : points to the node that we wish to delete trail : points to the predecessor NULL ptr 10 nodetrail

Fall 2005W.F. Delete node (2/2) There are only two case: Delete the first node Delete any other node

Fall 2005W.F. Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node

Fall 2005W.F. Delete First Node (1/2) NULL ptr 10 trail node NULL Free (node);

Fall 2005W.F. Delete First Node (2/2) ptr 10 trail NULL NULL node void delete(list_pointer *ptr, list_pointer trail, list_pointer node) { if (trail) trail->link = node->link; else *ptr = (*ptr)->link; free(node); }

Fall 2005W.F. Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node

Fall 2005W.F. General Delete Node (1/4) Case I : delete node between first node and last node NULL ptr 10 trailnode

Fall 2005W.F. General Delete Node (2/3) ptr 10 trail NULL node void delete(list_pointer *ptr, list_pointer trail, list_pointer node) { if (trail) trail->link = node->link; else *ptr = (*ptr)->link; free(node); }

Fall 2005W.F. General Delete Node (3/4) Case II : delete last node NULL ptr 10 nodetrail NULL

Fall 2005W.F. General Delete Node (4/4) ptr 10 trail NULL node void delete(list_pointer *ptr, list_pointer trail, list_pointer node) { if (trail) trail->link = NULL; else *ptr = (*ptr)->link; free(node); } NULL

Fall 2005W.F. Q & A