C++ - Lesson 2 This is a function prototype. a' is a function that takes an integer array argument and returns an integer pointer.

Similar documents
CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

Implementing Linked Lists

Data Structures (CS301) LAB

Chapter 17: Linked Lists

Dynamic Data Structures

Chapter 17: Linked Lists

Spring 2008 Data Structures (CS301) LAB

CSCE 110 PROGRAMMING FUNDAMENTALS

ICOM 4035 Data Structures. Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

Assignment 4: SkipList

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

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

Do not write in this area A.1 A.2 A.3 A.4 A.5 A.6 B.1 B.2 B.3 TOTAL. Maximum possible points:

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

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

Advanced Systems Programming

Object-Oriented Principles and Practice / C++

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays

Data Structures and Algorithms

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B /13:30-14:50 MC-4021/RCH-211

CSCE 110 PROGRAMMING FUNDAMENTALS

Doubly-Linked Lists

CS32 Discussion Week 3

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

Common Misunderstandings from Exam 1 Material

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

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++

Cpt S 122 Data Structures. Data Structures

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

ComS 228 Exam 1. September 27, 2004

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

Linked Lists. Chapter 4

Programming II (CS300)

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)

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Solution for Data Structure

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

Top of the Stack. Stack ADT

COEN244: Class & function templates

LECTURE 03 LINKED LIST

CSCI-1200 Data Structures Fall 2018 Lecture 21 Hash Tables, part 1

Homework Assignment #1

#ifndef DOUBLE_LIST /* this string SHOULD NOT previously exist */ #define DOUBLE_LIST

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

CA341 - Comparative Programming Languages

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

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

1 Short Answer (5 Points Each)

// Pointer to the first thing in the list

Functions. Arash Rafiey. September 26, 2017

CS201- Introduction to Programming Current Quizzes

Data Structures. Lecture 04 Sohail Aslam AL 1

ADVENTURE_IO Input/Output format and libraries for ADVENTURE modules List of Input/Output Functions February 17, 2006

An Introduction to C++

Midterm Practice TA: Brian Choi Section Webpage:

Lesson 8: Linked List Deque

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

What is an algorithm?

Lab 2: ADT Design & Implementation

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

4.1 Ordered Lists Revisited

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Example Final Questions Instructions

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

IS0020 Program Design and Software Tools Midterm, Fall, 2004

C++ Programming Assignment 3

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

Motivation for Templates

Linked Data Structures. Linked lists. Readings: CP:AMA The primary goal of this section is to be able to use linked lists and trees.

Program template-smart-pointers-again.cc

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

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Top of the Stack. Stack ADT

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

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

C Programming, Autumn 2013, Exercises for the Second Week

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list.

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

7.1 Optional Parameters

Introduction to Programming (Java) 4/12

Introduction to Programming Using Java (98-388)

CSC 210, Exam Two Section February 1999

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination

Review Questions for Final Exam

Part I: Short Answer (12 questions, 65 points total)

1 Short Answer (7 Points Each)

Linked List using a Sentinel

1 Short Answer (8 Points Each)

Transcription:

C++ - Lesson 2 1. Explain the following declarations: a) int *a(int a[]); This is a function prototype. 'a' is a function that takes an integer array argument and returns an integer pointer. b) const char [100]; This declaration is for a string of 100 characters whose data is constant. c) const string *sptr; 'sptr' is a pointer to a constant string. [the string is R(Read-only) / 'sptr' is RW(Readable/Writable)] d) string *const sptr; 'sptr' is a constant pointer to a string. [the string is RW / 'sptr' is R] e) const string *const sptr 'sptr' is a constant pointer to a constant string. [the string is R / 'sptr' is R] f) bool (*comp)(int, int); 'comp' is a pointer to a function that takes two integer arguments and returns a bool. g) void f(bool (*c) (int, int)); // Write the call to f 'f' is a function prototype that takes a function pointer (like the question above) and returns void. Example: f(comp); h) int ia[3][4]; 'ia' is a 2-dimensional array with 3 rows and 4 columns. i) int (*ip)[4]; 'ip' is a pointer to an array of 4 integer elements. ( to have an array 4 integer pointers you would write: int *ip[4] )

2. Write a template-class for doubly linked lists. Your class should provide the following functionalities: a. A constructor to create an empty list. b. A copy constructor. c. A destructor. d. A member function to insert an element in a list. e. A member function to remove an element from a list. f. A member function to search for an element in a list. g. An assignment operator. h. A member function that merges two sorted lists, L1 and L2, and returns a new sorted listed. The user should be able to call this function through the expression L1+L2. //----- doublelist.h -----// /******** DOUBLE LIST NODE *********/ class DoubleListNode public: DoubleListNode(DoubleListNode *prevnode, DoubleListNode *nextnode) : next(nextnode), prev(prevnode) T value; DoubleListNode<T> *next; DoubleListNode<T> *prev; ; /********* DOUBLE LIST ***********/ class DoubleList public: DoubleList(); DoubleList(const DoubleList &); ~DoubleList(); DoubleList& insert(t); DoubleList& remove(t); T* search(t value); const DoubleList& operator=(const DoubleList &); DoubleList operator+(doublelist&); private: void copyfromlist(const DoubleList &); DoubleListNode<T> *head; DoubleListNode<T> *tail; ; //----- END doublelist.h -----//

//----- doublelist.cpp -----// #include <doublelist.h> DoubleList<T>::DoubleList() head = new DoubleListNode<T>(NULL, NULL); tail = new DoubleListNode<T>(head, NULL); head->next = tail; DoubleList<T>::DoubleList(const DoubleList<T> &copy) head = new DoubleListNode<T>(NULL, NULL); tail = new DoubleListNode<T>(head, NULL); head->next = tail; copyfromlist(copy); DoubleList<T>::~DoubleList() DoubleListNode<T> *index = head; while (index!= NULL) DoubleListNode<T> *temp = index->next; delete index; index = temp; /* * Insert a new value at the end of the list. */ DoubleList<T>& DoubleList<T>::insert(T val) while(index->next!= NULL) DoubleListNode<T> *newnode = new DoubleListNode<T>(index->prev, index); newnode->value = val; // point old links to the new node. index->prev = index->prev->next = newnode; DoubleList<T>& DoubleList<T>::remove(T val) while (index->next!= NULL && index->value!= val) if (index->next!= NULL) index->prev->next = index->next; index->next->prev = index->prev; delete index;

T* DoubleList<T>::search(T val) while (index->next!= NULL) if (index->value == val) return &(index->value); return NULL; const DoubleList<T>& DoubleList<T>::operator=(const DoubleList<T> &copy) if (this == &copy) // Delete all elements in current list. DoubleListNode<T> *tmp; while (index->next!= NULL) tmp = index; index= index->next; delete tmp; // index now points to the tail head->next = index; // join the head and index->prev = head; // tail together. // Now copy over all elements. copyfromlist(copy); DoubleList<T> DoubleList<T>::operator+(DoubleList<T> &copy) // Merge two SORTED lists together (ascending). DoubleList<T> newlist; DoubleListNode<T> *index1 = head->next; DoubleListNode<T> *index2 = (copy.head)->next; while (index1->next!= NULL index2->next!= NULL) if ( index1->next!= NULL && (index2->next == NULL (index2->value >= index1->value))) newlist.insert(index1->value); index1 = index1->next; if (index1->next == NULL && index2->next == NULL) break; if ( index2->next!= NULL && (index1->next == NULL (index1->value >= index2->value))) newlist.insert(index2->value); index2 = index2->next; return newlist; void DoubleList<T>::copyFromList(const DoubleList<T> &copy) DoubleListNode<T> *index = (copy.head)->next; while (index->next!= NULL)

insert(index->value); //----- END doublelist.cpp -----//