LInked List Structure

Similar documents
Linked List Problems

Linked List Basics. Essential C ( features of the C programming language.

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Stacks. Linear data structures

Linked List as an ADT (cont d.)

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Data Structures Using C++ 2E. Chapter 5 Linked Lists

10CS35: Data Structures Using C

DATA STRUCTURES USING C

Linked Lists Linked Lists, Queues, and Stacks

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

PES Institute of Technology-BSC QUESTION BANK

The Advantages of Dan Grossman CSE303 Spring 2005, Lecture 25

Lecture 12 Doubly Linked Lists (with Recursion)

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Analysis of a Search Algorithm

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Algorithms and Data Structures

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Module 2 Stacks and Queues: Abstract Data Types

DATA STRUCTURE - STACK

ADTs,, Arrays, Linked Lists

System Software Prof. Dr. H. Mössenböck

Data Structures. Level 6 C Module Descriptor

List, Stack and Queue. Tom Chao Zhou CSC2100B Data Structures Tutorial 3

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Common Data Structures

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

CSE 211: Data Structures Lecture Notes VII

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

CmpSci 187: Programming with Data Structures Spring 2015

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

recursion, O(n), linked lists 6/14

LINKED DATA STRUCTURES

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

TREE BASIC TERMINOLOGIES

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Illustration 1: Diagram of program function and data flow

Introduction to Data Structures

CS107L Handout 04 Autumn 2007 October 19, 2007 Custom STL-Like Containers and Iterators

Chapter 5 Names, Bindings, Type Checking, and Scopes

Sample Questions Csci 1112 A. Bellaachia

Stack Allocation. Run-Time Data Structures. Static Structures

Data Structure with C

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Chapter 3: Restricted Structures Page 1

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

What Is Recursion? 5/12/10 1. CMPSC 24: Lecture 13 Recursion. Lecture Plan. Divyakant Agrawal Department of Computer Science UC Santa Barbara

Basic Data Structures and Algorithms

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Object Oriented Software Design II

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

Data Structures In Java

7.1 Our Current Model

Data Structure [Question Bank]

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

風 水. Heap Feng Shui in JavaScript. Alexander Sotirov.

The C Programming Language course syllabus associate level

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Node-Based Structures Linked Lists: Implementation

Data Structures and Algorithms V Otávio Braga

CompSci-61B, Data Structures Final Exam

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Binary Heap Algorithms

C++ INTERVIEW QUESTIONS

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Unordered Linked Lists

Tutorial on C Language Programming

Jorix kernel: real-time scheduling

Data Structures and Data Manipulation

Example 1/24. Example

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

ECS 165B: Database System Implementa6on Lecture 2

One Dimension Array: Declaring a fixed-array, if array-name is the name of an array

Project 4 DB A Simple database program

CS 2412 Data Structures. Chapter 2 Stacks and recursion

Ordered Lists and Binary Trees

Converting a Number from Decimal to Binary

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Splicing Maps and Sets

Leak Check Version 2.1 for Linux TM

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Abstract Data Types. Chapter 2

Chapter Objectives. Chapter 7. Stacks. Various Types of Stacks LIFO. Empty Stack. Stacks

SMTP-32 Library. Simple Mail Transfer Protocol Dynamic Link Library for Microsoft Windows. Version 5.2

1 Abstract Data Types Information Hiding

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Transcription:

Linked List 1 So far, you know two types of data structures, which are collections of data arrays stacks Linked lists are another collection type. Arrays, stacks and linked lists store "elements" on behalf of "client" code. The specific type of element is not important since essentially the same structure works to store elements of any type. Arrays / stacks of integers Arrays / stacks of doubles Arrays / stacks of customer accounts Arrays / stacks of...

LInked List Structure An array allocates memory for all its elements lumped together as one block of memory. A linked list allocates space for each element separately in its own block of memory called a "linked list element" or "node". All nodes of a list are connected together like the links in a chain. Each node contains two fields: a "data" field to store whatever element type the list holds for its client, and a "next" field which is a pointer used to link one node to the next node. Each node is allocated in the heap with a call to malloc(), so the node memory continues to exist until it is explicitly deallocated with a call to free(). The front of the list is a pointer to the first node. data = 1 next = 0xc124 data = 2 next = 0xc038 head = 0xc224 2

Create List The example below will create the three-element list struct node int data; struct node *next; ; data = 1 next = 0xc124 data = 2 next = 0xc038 head = 0xc224 /* Build the list 1, 2, 3 in the heap and store its head pointer in a local stack variable. Returns the head pointer to the caller. */ struct node * BuildOneTwoThree () struct node *head = NULL; struct node *second = NULL; struct node *third = NULL; head = malloc (sizeof (struct node)); /* allocate 3 nodes in the heap */ second = malloc (sizeof (struct node)); third = malloc (sizeof (struct node)); head->data = 1; /* setup first node */ head->next = second; second->data = 2; /* setup second node */ second->next = third; third->; /* setup third link */ third->; /* At this point, the linked list referenced by "head" matches the list in the drawing. */ return head; 3

Count Elements in the List Pass the list by passing the head pointer Iterate over the list with a local pointer data = 1 next = 0xc124 data = 2 next = 0xc038 head = 0xc224 /* Given a linked list head pointer, compute and return the number of nodes in the list. */ int Length (struct node *head) struct node *current = head; int count = 0; while (current!= NULL) count++; current = current->next; return count; void LengthTest () struct node *mylist = BuildOneTwoThree (); int len = Length (mylist); /* results in len == 3 */ 4

Display Elements in the List Iterate over the list with the local pointer Print the data contained in each node data = 1 next = 0xc124 data = 2 next = 0xc038 head = 0xc224 /* Given a linked list head pointer, display all numbers stored in the list. */ void Display (struct node *head) struct node *current = head; while (current!= NULL) printf("%d ",current->data); current = current->next; printf("\n"); 5

Add Element in Front of the List Pass the pointer to the head to be able to modify it data =? next =? newnode = 0xc246 data = 1 next = 0xc124 data = 2 next = 0xc038 *headptr = 0xc224 void Push (struct node **headptr, int data) struct node *newnode = malloc (sizeof (struct node)); newnode->data = data; newnode->next = *headptr; *headptr = newnode; void PushTest () struct node *head = BuildOneTwoThree (); Push (&head, 0); /* note the & */ Push (&head, 13); /* head is now the list 13, 0, 1, 2, 3 */ 6

Add Element in Front of the List Pass the pointer to the head to be able to modify it data = 0 next = 0xc224 newnode = 0xc246 data = 1 next = 0xc124 data = 2 next = 0xc038 *headptr = 0xc224 void Push (struct node **headptr, int data) struct node *newnode = malloc (sizeof (struct node)); newnode->data = data; newnode->next = *headptr; *headptr = newnode; void PushTest () struct node *head = BuildOneTwoThree (); Push (&head, 0); /* note the & */ Push (&head, 13); /* head is now the list 13, 0, 1, 2, 3 */ 7

Add Element in Front of the List Pass the pointer to the head to be able to modify it data = 0 next = 0xc224 newnode = 0xc246 data = 1 next = 0xc124 data = 2 next = 0xc038 *headptr = 0xc246 void Push (struct node **headptr, int data) struct node *newnode = malloc (sizeof (struct node)); newnode->data = data; newnode->next = *headptr; *headptr = newnode; void PushTest () struct node *head = BuildOneTwoThree (); Push (&head, 0); /* note the & */ Push (&head, 13); /* head is now the list 13, 0, 1, 2, 3 */ 8

Free List Free each node starting from the beginning data = 1 next = 0xc124 data = 2 next = 0xc038 head = 0xc224 t = 0xc124 void FreeList (struct node *head) while (head) struct node *t = head->next; free (head); head = t; ; 9

Free List Free each node starting from the beginning data = 2 next = 0xc038 head = 0xc224 t = 0xc124 void FreeList (struct node *head) while (head) struct node *t = head->next; free (head); head = t; ; 10

Free List Free each node starting from the beginning data = 2 next = 0xc038 head = 0xc124 t = 0xc124 void FreeList (struct node *head) while (head) struct node *t = head->next; free (head); head = t; ; 11

Free List Free each node starting from the beginning data = 2 next = 0xc038 head = 0xc124 t = 0xc038 void FreeList (struct node *head) while (head) struct node *t = head->next; free (head); head = t; ; 12

Free List Free each node starting from the beginning head = 0xc124 t = 0xc038 void FreeList (struct node *head) while (head) struct node *t = head->next; free (head); head = t; ; 13

Free List Free each node starting from the beginning head = 0xc038 t = 0xc038 void FreeList (struct node *head) while (head) struct node *t = head->next; free (head); head = t; ; 14

Free List Free each node starting from the beginning head = 0xc038 t = NULL void FreeList (struct node *head) while (head) struct node *t = head->next; free (head); head = t; ; 15

Free List Free each node starting from the beginning head = 0xc038 t = NULL void FreeList (struct node *head) while (head) struct node *t = head->next; free (head); head = t; ; 16

Free List Free each node starting from the beginning head = NULL t = NULL void FreeList (struct node *head) while (head) struct node *t = head->next; free (head); head = t; ; 17

Incorrect Implementation of Free List 18 Version below is shorter, but incorrect It attempts to access the freed memory area We cannot get rid of the temporary variable void FreeList (struct node *head) while (head) free (head); head=head->next; ;

Append Node 19 Function appends the node at the end of the list void AppendNode (struct node **headref, int num) struct node *current = *headref; struct node *newnode; newnode = malloc (sizeof (struct node)); newnode->data = num; newnode->; /* special case for length 0 */ if (current == NULL) *headref = newnode; else /* Locate the last node */ while (current->next!= NULL) current = current->next; current->next = newnode;

Copy List 20 Function returns the pointer to the copy of the list Note there is no need for special case for an empty list struct node * CopyList (struct node *src) struct node *head = NULL; struct node **dst=&head; while (src) *dst = malloc (sizeof (struct node)); (*dst)->data = src->data; (*dst)->; src = src->next; dst = &((*dst)->next); return head;

Homework Problems Write a Pop() function that is the inverse of Push(). Pop() takes a non-empty list, deletes the head node, and returns the head node's data. void PopTest() struct node* head = BuildOneTwoThree(); // build 1, 2, 3 int a = Pop(&head); /* deletes "1" node and returns 1 */ int b = Pop(&head); /* deletes "2" node and returns 2 */ int c = Pop(&head); /* deletes "3" node and returns 3 */ int len = Length(head); /* the list is now empty, so len == 0 */ Write an iterative Reverse() function that reverses a list in place by rearranging all the.next pointers and the head pointer. void ReverseTest() struct node* head; head = BuildOneTwoThree(); Reverse(&head); /* head now points to the list 3, 2, 1 */ FreeList(head); 21

Homework Problems 22 Write the Sort() functions that sorts the list in place in ascending order. Use the bubblesort algorithm. void SortTest() struct node* head = NULL; int i; for(i=0;i<10;i++) Push(&head,i) /* head now points to the list 9, 8,..., 1, 0 */ Sort(&head); /* head now points to the list 0, 1, 2,..., 9 */ FreeList(head); Write a complete set of functions (including above homework problems) for the list of zero-terminated strings. List should store the copies of strings, so do not forget about the proper memory management.