CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.

Slides:



Advertisements
Similar presentations
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Advertisements

Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
Data Structures: A Pseudocode Approach with C
CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)
CSE1303 Part A Data Structures and Algorithms Lecture A6 – Dynamic Memory.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A6 – Dynamic Memory.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A15 – Hash Tables: Collision Resolution.
Topic 3 Basic Data Structures continued CSE1303 Part A Data Structures and Algorithms.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A4 – Basic Data Structures – Continued (Queues)
CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)
CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A8 – Linked Stacks and Linked Queues.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A9 – Linked Lists.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A11 – Recursion.
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.
CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Lists CSE1303 Part A Data Structures and Algorithms.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A5 – Basic Data Structures – Continued (Lists)
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
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.
Stack and Heap Memory Stack resident variables include:
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Introduction to Data Structures Systems Programming Concepts.
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.
Data Structure & Algorithm
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
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.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
1 Joe Meehean. 2  empty is the queue empty  size  enqueue (add) add item to end of queue  dequeue (remove) remove and return item at front of queue.
Basic Data Structures (Stacks). 2 Basic Data Structures Stacks Queues Lists.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Data Structure & Algorithms
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
UNIT-II Topics to be covered Singly linked list Circular linked list
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A3 – Basic Data Structures (Stacks)
CSE 1342 Programming Concepts
CSCI-255 LinkedList.
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Data Structure Dr. Mohamed Khafagy.
UNIT-3 LINKED LIST.
Linked Lists.
Basic Data Structures – Continued (Queues)
Lectures on data structures and C 3ed CS(CSI33) By, CHANDRASHEKAR A.M.
Chapter 16-2 Linked Structures
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Review & Lab assignments
Chapter 16 Linked Structures
FIFO Queues CSE 2320 – Algorithms and Data Structures Alexandra Stefan
CMPT 225 Lecture 5 – linked list.
Presentation transcript:

CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures

2 Overview of Topic Review List Implementations. Nodes. Linked Stacks. Linked Queues Linked Lists. Other List Operations Today’s Lecture

3 Lists Kept in alphabetical or numerical order No spaces between elements Simple lists are implemented in arrays

4 A List ADT Initialize the list. Determine whether the list is empty. Determine whether the list is full. Find the size of the list. Insert an item anywhere in the list. Delete an item anywhere in a list. Go to a particular position in a list. A sequence of elements together with these operations: Lists need to be able to expand

5 Simple List Implementation

6 Expansion and Insertion Copy old array, leave space for the new value 6 Simple List Implementation

7 Disadvantages Lots of memory needs to be allocated. Lots of copying needs to be done.

8 Linked List Implementation: Using Pointers x20000x20080x20100x20180x2020 0x20180x2000NULL start 10 0x2008 insert: 6

9 Method 1 start x20000x20080x20100x20180x2020 0x20180x2000NULL 10 0x2008 0x30F00x30F80x31000x31080x3110 newStart 0x3118 insert: x30F80x31000x31080x31100x3118NULL x30F0 Copy old array, leave space for the new value

10 Method x20000x20080x20100x20180x2020 0x20180x2000NULL start 10 0x2008 insert: 6 6 newItem 0x30F0

11 Method x20000x20080x20100x20180x2020 0x30F00x20180x2000NULL start 10 0x newItem 0x30F0 0x2020

12 Advantages Only a little amount of memory needs to be allocated. Only a little amount of copying needs to be done.

13 struct NodeRec { float value; struct NodeRec* nextPtr; }; typedef struct NodeRec Node; Node: nextPtr: value:

14 #ifndef NODEH #define NODEH struct NodeRec { float value; struct NodeRec* nextPtr; }; typedef struct NodeRec Node; Node* makeNode(float item); #endif

15 Make Node To make a new node for an item –take enough bytes from the heap –remember its address in memory –put the item in that location –set “next” link to NULL –return the node’s address

Node* makeNode(float item) { Node* newNodePtr = (Node*)malloc(sizeof(Node)); if (newNodePtr == NULL) { fprintf(stderr, “Out of memory”); exit(1); } else { newNodePtr->value = item; newNodePtr->nextPtr = NULL; } return newNodePtr; } 16

#include #include “node.h” int main() { float item; Node* headPtr = NULL; Node* newNodePtr = NULL; while (scanf(“%f”, &item) != EOF){ if (headPtr == NULL){ headPtr = makeNode(item); } else { newNodePtr = makeNode(item); newNodePtr->nextPtr = headPtr; headPtr = newNodePtr; } 17 Insert nodes at the start of a list

18 0x30F0 headPtr: 0x30F0 0x2124 0x2A40 1 nextPtr: value: 6 NULL nextPtr: value: 3 0x2124 nextPtr: value:

19 Linked Structure 1 0x30F0 6 30x2124 0x2A40 NULL 0x2A40 0x30F0 headPtr:

20 Linked Structure 1 0x30F0 30x2124 0x2A40 0x30F0 headPtr: 6 0x2124 NULL

21 Linked Structure 1 3 headPtr: 6

22 Revision Node How to make a new Node Linked Structures Revision: Reading Kruse 4.5 Preparation Next lecture: Linked Stacks and Queues Read in Kruse et al.