Linked List using a Sentinel

Similar documents
Data Structures and Algorithms

Spring 2008 Data Structures (CS301) LAB

CMSC 341 Lecture 7 Lists

CSCE 110 PROGRAMMING FUNDAMENTALS

lecture09: Linked Lists

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Ch. 17: Linked Lists. Introduction to Linked Lists

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

CSCE 110 PROGRAMMING FUNDAMENTALS

Student Name and ID CS 32, WINTER 2015, PRACTICE MIDTERM I.

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil

Intermediate Programming, Spring 2017*

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

CA341 - Comparative Programming Languages

Computer Programming

Common Misunderstandings from Exam 1 Material

CMSC 202 Midterm Exam 1 Fall 2015

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

CPSC 427: Object-Oriented Programming

Pointers, Dynamic Data, and Reference Types

do { statements } while (condition);

Part 3: Function Pointers, Linked Lists and nd Arrays

Review Questions for Final Exam

moretosearch = (location < length);

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Chapter 17: Linked Lists

Data Structures (CS301) LAB

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

EECE.3220: Data Structures Spring 2017

Midterm Exam. Sample Solutions

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Due Date: See Blackboard

CPSC 427: Object-Oriented Programming

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Midterm Practice TA: Brian Choi Section Webpage:

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

CS2141 Software Development using C/C++ C++ Basics

A linked list. Subtopic: Linked lists. List-node structure type. Array implementation of a linked list. Inserting into a sorted collection

typedef Labeling<unsigned char,short> LabelingBS; typedef Labeling<unsigned char,short>::regioninfo RegionInfoBS;

My First Command-Line Program

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Introduction to the C programming language

Name. CPTR246 Spring '17 (100 total points) Exam 2

การทดลองท 8_2 Editor Buffer Array Implementation

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Introduction to the C programming language

Example Final Questions Instructions

COMP 2355 Introduction to Systems Programming

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Lab 1: First Steps in C++ - Eclipse

Exceptions, Case Study-Exception handling in C++.

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

1 #include <iostream> 2 using namespace std; 3 4 // implementing the dynamic List ADT using Linked List 5 6 class Node{ 7 8 private: 9 int data; 10

Program template-smart-pointers-again.cc

Programming Language. Functions. Eng. Anis Nazer First Semester

The University of Nottingham

C:\Temp\Templates. Download This PDF From The Web Site

Program threaded-fft-mutex.cc

1 Short Answer (7 Points Each)

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

Example Final Questions Instructions

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2

Object Oriented Programming COP3330 / CGS5409

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

BEng (Hons) Telecommunications. Examinations for 2016 / Semester 2

Program threaded-fft-bakery.cc

Short Notes of CS201

CS Data Structure Spring Answer Key- Assignment #3

Program template-smart-pointers.cc

Chapter 10 - Notes Applications of Arrays

Data Structures Lab II. Binary Search Tree implementation

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CS201 - Introduction to Programming Glossary By

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

// Pointer to the first thing in the list

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

vector<process*> Delete(vector<process*> DeleteQ, int ID); vector<process*> DeleteW(vector<process*> DeleteWQ, int IDW);

Due Date: See Blackboard

Advanced Systems Programming

Object oriented programming

Structured Data. CIS 15 : Spring 2007

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

Strings and Stream I/O

1. Which of the following best describes the situation after Line 1 has been executed?

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

CS 103 Unit 11. Linked Lists. Mark Redekopp

The Memory Manager Project

Chapter 17: Linked Lists

Due Date: See Blackboard

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Transcription:

Linked List using a Sentinel Linked List.h / Linked List.h Using a sentinel for search Created by Enoch Hwang on 2/1/10. Copyright 2010 La Sierra University. All rights reserved. / #include <iostream> using namespace std; class LinkedList{ private: struct ListNode { int data; ListNode next; ; ListNode head; ListNode tail; public: LinkedList(); void InsertNode(int d); bool SearchNode(int d); void PrintList(); bool DeleteNode(int d); ;

Linked List.cpp #include "Linked List.h" LinkedList::LinkedList(){ head = NULL; tail = NULL; // constructor void LinkedList::InsertNode(int d){ ListNode np = new ListNode; // allocate memory for new node np >data = d; // put data in new node if(head == NULL){ // empty list special case head = np; // set head and tail pointers tail = np; np >next = NULL; else{ // search for correct location to insert node // set up the sentinal first. This part is almost the same as the code in the Search routine ListNode p = head; // pointer for traversing the list ListNode pp = p; // previous pointer that follows p while (p >data < d) { pp = p; // update previous pointer delete sp; // see where to insert the new node if (p == head) { // insert to head of list np >next = head; head = np; else if (p == sp) { // insert to tail of list tail >next = np; np >next = NULL; tail = np; else { // insert to middle of list np >next = pp >next; pp >next = np; cout << " Node " << d << " inserted" << endl; bool LinkedList::SearchNode(int d){ if (head == NULL) return false; // empty list

// set up the sentinal at the end of the list ListNode p = head; while (p >data!= d) { delete sp; // note that even though the memory is released, sp is still // pointing at that location so the test below is still ok // return node found or not found if (p!= sp) return true; // found if p is not pointing at the sentinal else return false; void LinkedList::PrintList(){ ListNode ptr = head; cout << "Here's the linked list..." << endl; while(ptr!= NULL){ cout << " " << ptr >data; ptr = ptr >next; cout << endl << endl; if (head == NULL) { cout << "head is pointing to NULL" << endl; cout << "tail is pointing to NULL" << endl; else { cout << "head is pointing to node " << head >data << endl; cout << "tail is pointing to node " << tail >data << endl; bool LinkedList::DeleteNode(int d){ if (head == NULL) { cout << " Did not find " << d << " to delete" << endl; return false; // empty list // search for node to delete // set up the sentinal first. This part is almost the same as the code in the Search routine

ListNode p = head; // pointer for traversing the list ListNode pp = p; // previous pointer that follows p while (p >data < d) { pp = p; // update previous pointer delete sp; if (p == sp) { // node not found so no deletion cout << " Did not find " << d << " to delete" << endl; return false; // delete the node where p is pointing at // see where the node is in the list if (p == head) { // delete the head head = p >next; // remove node from the head of the list delete p; // release the memory pointed to by p else if (p == tail) { // delete the tail pp >next = NULL; delete tail; tail = pp; else { // delete the middle pp >next = p >next; delete p; cout << " Node " << d << " deleted" << endl; return true; main.cpp / This program demonstrates the creation of a linked list with a sentinal for search Created by Enoch Hwang on 2/1/10. Copyright 2010 La Sierra University. All rights reserved. / #include <iostream> #include "Linked List.h" using namespace std; void main (int argc, char const argv[]) { LinkedList L; // create an empty list int n, select; while (1) {

cout << "1 = insert" << endl; cout << "2 = search" << endl; cout << "3 = delete" << endl; cout << "4 = print list" << endl; cout << "0 = end" << endl; cout << "? "; cin >> select; switch (select) { case 1: cout << "Enter number to insert? "; L.InsertNode(n); case 2: cout << "Enter number to search? "; if(l.searchnode(n) == true) cout << " " << n << " is found in the list" << endl; else cout << " " << n << " is NOT found in the list" << endl; case 3: cout << "Enter number to delete? "; L.DeleteNode(n); case 4: cout << "Linked list..." << endl; L.PrintList(); default: exit (1);