Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.

Slides:



Advertisements
Similar presentations
Linked Lists Mohammed Almashat CS /03/2006.
Advertisements

Stacks, Queues, and Linked Lists
EENG212 Algorithms and Data Structures
Linked Lists.
JAVA & Linked List Implementation
CSCE 3110 Data Structures & Algorithm Analysis
LIST PROCESSING.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Structures Spring 2013Programming and Data Structure1.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Linked Lists in C and C++ By Ravi Prakash PGT(CS).
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Multidimensional Arrays. Example Write a program to keep track of all warmup scores for all students. Need a list of a list of scores Student – score.
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.
Structures.
Lists: array implementation list_size = 5 lst Obj 1Obj 2Obj 3Obj 4Obj 5.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Pointers, Dynamic Memory Allocation and Structures.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
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.
D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to.
Linked Lists. A linear linked list is a collection of objects, called nodes, each of which contains a data item and a pointer to the next node in the.
Array, Structure and Union
Introduction to Data Structures Systems Programming Concepts.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
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.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
Algorithms and Data Structures
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
2/21/20161 List Operations Advanced Programming Ananda Gunawardena.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
 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.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
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:
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
UNIT-II Topics to be covered Singly linked list Circular linked list
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Programming Circular Linked List.
Chapter 12 – Data Structures
UNIT – I Linked Lists.
Review Deleting an Element from a Linked List Deletion involves:
Linked lists.
Linked list insertion Head. Linked list insertion Head.
Sequences 9/18/ :20 PM C201: Linked List.
ليست هاي پيوندي.
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Local Variables, Global Variables and Variable Scope
Review & Lab assignments
Linked Lists Chapter 4.
Chapter 17: Linked Lists.
CS148 Introduction to Programming II
Linked lists.
Linked Lists.
Variable Storage Memory Locations (Logical) Variable Classes Stack
Presentation transcript:

Linked Lists

Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked list

Linked List name = shoes number = 1234 cost = num_in_inventory = 10 name = shirts number = 1235 cost = num_in_inventory = 5 name = pants number = 1236 cost = num_in_inventory = 12

Linked List name = shoes number = 1234 cost = num_in_inventory = 10 name = shirts number = 1235 cost = num_in_inventory = 5 name = pants number = 1236 cost = num_in_inventory = 12

Linked List name = shoes number = 1234 cost = num_in_inventory = 10 name = shirts number = 1235 cost = num_in_inventory = 5 name = pants number = 1236 cost = num_in_inventory = 12 next_ptr = next_ptr = NULL

Defining the Structure typdef struct inventory_item_s { char name[80]; int number; double cost; int num_in_inventory; } inventory_item_t;

Defining the Structure typdef struct inventory_item_s { char name[80]; int number; double cost; int num_in_inventory; struct inventory_item_s *next; } inventory_item_t;

Typical Linked List Item 1Item 2Item 3Item N NULL head:

Traversing a List //assume head points to the first element of the list //traverse the list and print the name of each item (in the inventory) inventory_item_t *cur_node; cur_node = head; while(cur_node != NULL) { printf(“Name: %s\n”, cur_node->name); cur_node=cur_node->next; } Item 1Item 2Item 3Item N NULL head:

Building a List inventory_item_t *head = NULL; inventory_item_t *tmp = NULL; tmp = (inventory_item_t *)malloc(sizeof (inventory_item_t)); strcpy(tmp->name, “Shirts”); tmp->cost = 20; tmp->number=1234; tmp->num_in_inventory=10; tmp->next=NULL; head = tmp; Item N NULL head:

Insert an Element //we have head and tmp tmp = (inventory_item_t *)malloc(sizeof (inventory_item_t)); strcpy(tmp->name, “Pants”); tmp->cost = 70; tmp->number=1235; tmp->num_in_inventory=17; //insert at beginning Item N NULL head:

Insert an Element //we have head and tmp tmp = (inventory_item_t *)malloc(sizeof (inventory_item_t)); strcpy(tmp->name, “Pants”); tmp->cost = 70; tmp->number=1235; tmp->num_in_inventory=17; //insert at beginning // point new element next to current head // point current head to new element tmp->next=head; head=tmp; Item N-1 head: Item N NULL

Insert an Element tmp = (inventory_item_t *)malloc(sizeof (inventory_item_t)); strcpy(tmp->name, “Shoes”); tmp->cost = 40; tmp->number=1236; tmp->num_in_inventory=7; //insert at end Item N-1 head: Item N NULL

Insert an Element tmp = (inventory_item_t *)malloc(sizeof (inventory_item_t)); strcpy(tmp->name, “Shoes”); tmp->cost = 40; tmp->number=1236; tmp->num_in_inventory=7; //insert at end // point new element next to NULL // point last element next to new element Item N-1 head: Item N NULL

Insert an Element tmp = (inventory_item_t *)malloc(sizeof (inventory_item_t)); strcpy(tmp->name, “Shoes”); tmp->cost = 40; tmp->number=1236; tmp->num_in_inventory=7; //insert at end // point new element next to NULL // point last element next to new element tmp->next=NULL; //assume cur_node pointer was declared cur_node=head; while(cur_node != NULL && cur_node->next != NULL) { cur_node=cur_node->next; } cur_node->next=tmp; Item N-1 head: Item NItem N+1 NULL