1-Way Linked Lists. struct Node { Node *next; int value; }; Node *head = NULL;

Similar documents
Queue Implementations

Lecture 12 Doubly Linked Lists (with Recursion)

Unordered Linked Lists

Cpt S 223. School of EECS, WSU

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

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

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

Common Data Structures

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

10CS35: Data Structures Using C

Analysis of a Search Algorithm

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

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

Data Structures and Algorithms Lists

LINKED DATA STRUCTURES

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

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:

DATA STRUCTURES USING C

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Algorithms and Data Structures

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Algorithms and Data Structures

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

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

Universidad Carlos III de Madrid

Project 4 DB A Simple database program

Answers to Review Questions Chapter 7

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.

Linked Lists Linked Lists, Queues, and Stacks

STACKS,QUEUES, AND LINKED LISTS

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

ADTs,, Arrays, Linked Lists

Sample Questions Csci 1112 A. Bellaachia

Introduction to Object-Oriented Programming

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

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

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

PES Institute of Technology-BSC QUESTION BANK

Node-Based Structures Linked Lists: Implementation

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Data Structure [Question Bank]

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

AP Computer Science Java Mr. Clausen Program 9A, 9B

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

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

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Data Structures Using C++

Introduction to Data Structures and Algorithms

Circular Linked List. Algorithms and Data Structures

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

Sequences in the C++ STL

Cours de C++ Utilisations des conteneurs

Linked Lists: Implementation Sequences in the C++ STL

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

Data Structures Fibonacci Heaps, Amortized Analysis

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction

COMPUTER SCIENCE. Paper 1 (THEORY)

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

Binary Heap Algorithms

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

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

Data Structures UNIT III. Model Question Answer

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Data Structures, Practice Homework 2, with Solutions (not to be handed in)

Creating a Simple Visual C++ Program

Ordered Lists and Binary Trees

Introduction to data structures

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

CSE 211: Data Structures Lecture Notes VII

Stacks. Linear data structures

Data Structures and Algorithms

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

Module 2 Stacks and Queues: Abstract Data Types

Singly linked lists (continued...)

Lecture Notes on Binary Search Trees

Help on the Embedded Software Block

Queues. Manolis Koubarakis. Data Structures and Programming Techniques

Passing 1D arrays to functions.

C++FA 5.1 PRACTICE MID-TERM EXAM

Pointers and Linked Lists

Industrial Programming

Chapter One Introduction to Programming

Visual Studio 2008 Express Editions

Data Structures. Level 6 C Module Descriptor

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

Lecture 11 Array of Linked Lists

CmpSci 187: Programming with Data Structures Spring 2015

Abstract Data Types. Chapter 2

Basics of I/O Streams and File I/O

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Data Structures and Data Manipulation

Chapter 8: Bags and Sets

Appendix K Introduction to Microsoft Visual C++ 6.0

Algorithms and Data Structures Written Exam Proposed SOLUTION

Data Structures and Algorithms!

Transcription:

Linked Lists In this paper we'll discuss different strategies for managing linked lists. Singly or 1-way linked lists maintain a single forward pointer in each node that points to the next item in the list. Doubly or 2- way linked lists maintain two pointers in each node: a pointer to the next item, and a pointer to the previous item. 1-Way Linked Lists Linked lists are a convenient way to store data for sequential access. Let's assume we want to store a list of integers. The following code illustrates a typical node in the list, and sets up a head pointer with a NULL value. struct Node { Node *next; int value; ; Node *head = NULL; To add and remove items from the front of the list we'll define functions push_front and pop_front. Function displaylist will output the contents of the entire list. // add an item to front of list head = p; // remove an item from front of list Node *p = head; head = head->next; // display entire list void displaylist() { Node *p; for (p = head; p; p = p->next) cout << p->value << endl;

To add and remove items from the back of the list we need to know the address of the last node on the list so we can update its next pointer. The following versions of push_back and pop_back chain through the list to add or remove items from the end of the list. // add item to end of list Node *p, *last; p = new Node; p->next = NULL; // find last node for (last = head; last && last->next; last = last->next) ; // insert at end of list if (last) last->next = p; head = p; // list must be empty // remove item from end of list void pop_back() { Node *p, *prev; prev = NULL; p = head; // chain until p points to last node // and prev points to next-to-last node while (p && p->next) { prev = p; p = p->next; // adjust previous node if (prev) prev->next = NULL; head = NULL; // deleted first node Practice problem #1 Given t is a pointer to node in the list and s is a pointer to a node not in the list. 1. Insert s after node t. 2. Insert s before node t. 3. Delete node t.

For faster access to the back of the list let's maintain a tail pointer that points to the last node on the list. First we'll fix push_front and pop_front, incorporating tail pointer logic. Node *tail = NULL; Node *head = NULL; // points to last item on list // points to 1st item on list head = p; if (!tail) tail = p; Node *p = head; head = head->next; if (!head) tail = NULL; Now we can add the improved version of push_back that uses the tail pointer to find the last node. // construct new node p->next = NULL; if (tail) tail->next = p; head = p; // list must be empty tail = p; To code pop_back we need a pointer to the next-to-last node so we can update its next pointer. Then, if we do another pop_back, we'll need a pointer to the node before that. This is a neverending problem, so for this purpose the tail pointer won't suffice. How would you output the list in reverse order? That is, display the last item, the next-to-last item, and so on. In summary, 1-way linked lists work well for adding and removing items at the front of the list. However, adding and removing items from the back of the list, or displaying the list in reverse order, can be an expensive operation. This is true even if a tail pointer is included in the design.

2-Way Linked Lists There are several strategies you can use to implement 2-way linked lists. We'll explore two possibilities: NULL-terminated lists and circular lists with a sentinel node. The following structure will be used in our examples. struct Node { Node *next; Node *prev; int value; Node; NULL-Terminated Lists For this method we'll maintain a head pointer that's initially NULL. The next pointers are forward pointers that point to the next node, and the prev pointers are backward pointers that point to the previous node. The last next pointer at the end of the foward chain is NULL, and the last prev pointer (1 st node in list) is NULL. Node *tail = NULL; Node *head = NULL; // add an item to front of list p->prev = NULL; if (head) head->prev = p; tail = p; // inserting 1st node head = p; // add an item to end of list p->next = NULL; p->prev = tail; if (tail) tail->next = p; head = p; // inserting 1st node tail = p;

// remove an item from front of list // remove from list Node *p = head; head = head->next; // adjust pointers if (head) head->prev = NULL; tail = NULL; // removing 1st node // remove an item from end of list void pop_back() { Node *p = tail; tail = tail->prev; // adjust pointers if (tail) tail->next = NULL; tail = NULL; // removing 1st node // display entire list void displaylist() { Node *p; for (p = head; p; p = p->next) cout << p->value << endl; Using 2-way lists we no longer have to chain through the entire list to implement pop_back. How would you display the list in reverse order? Practice Problem #2 Given t is a pointer to node in the list and s is a pointer to a node not in the list. 1. Insert s after node t. 2. Insert s before node t. 3. Delete node t.

Circular Lists with Sentinel Nodes To avoid complications associated with head/tail pointers, 2-way lists are often implemented as circular lists with a sentinel node. When the list is empty the sentinel node simply points to itself. The following code illustrates logic using a sentinel node. Prior to using the data structure the user must call initlist to allocate and initialize the sentinel node. Node *head; // allocate an initialize sentinel node void initlist() { head = new Node; head->next = head->prev = head; // add an item to front of list p->next = head->next; p->prev = head; p->next->prev = p; p->prev->next = p; // add an item to end of list p->prev = head->prev; p->next->prev = p; p->prev->next = p;

// remove an item from front of list Node *p = head->next; p->next->prev = p->prev; p->prev->next = p->next; // remove an item from end of list void pop_back() { Node *p = head->prev; p->next->prev = p->prev; p->prev->next = p->next; // display entire list void displaylist() { Node *p; for (p = head->next; p!= head; p = p->next) cout << p->value << endl; Since the head is also a node, no special logic is required for edge cases. How would you display the list in reverse order? Practice Problem #3 Given t is a pointer to node in the list and s is a pointer to a node not in the list. 1. Insert s after node t. 2. Insert s before node t. 3. Delete node t.