Spring 2008 Data Structures (CS301) LAB
|
|
- Kristina Collins
- 5 years ago
- Views:
Transcription
1 Spring 2008 Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Singly Linked List practically using c++ and adding more functionality in it. o Enabling students to implement Doubly Linked List practically using c++ and adding more functionality in it. Introduction to Singly Linked List data structure Linked Memory: Linked Memory is the collection of objects in which each object is pointing to next object in linear form as shown below, Node 1 Node 2 Node 3 Node 4 Node 5 Node 6 For linked memory (collection of linked objects of same type) it is necessary to keep the pointer of next object in the previous object. We achieve this by declaring a class Node and in it we have two variables one is to store value and second is pointer to that Node to store the address of next node as shown below, class Node int value; // to store like we stored in array or in List Node * ptr; // to store address of next node object Now we will create as much objects of this class Node as necessary (how much values we want to store in Link List) and in every object we store the address of next node (it is like a chain) as shown below, Data Structures (CS301) 1
2 A Single Node Value A Single Node Value A Single Node Value A Single Node Value A Single Node Value Node * ptr Node * ptr Node * ptr Node * ptr Node * ptr Collection of Nodes Objects Picture in memory Each cell in linked memory is called a Node. We can link infinite number of nodes according to our requirements similarly we can also remove as much number of nodes as necessary from our linked memory so linked memory is dynamic which is not possible in the case of arrays that have fixed length defined at the time of their creation that can neither be increased nor decreased similarly in List we increment or decrement list size as multiple of 100 or more the difference between Array and Link List representation in memory is shown below, Array of 5 elements (fixed size once created) Link List (Dynamic size) (In start only head LinkList size = 0) (After adding three nodes LinkList size = 3) (After removing two nodes only one node left LinkList size = 1) Data Structures (CS301) 2
3 (After adding another node LinkList size = 2) Singly Linked List uses the concept of Linked Memory to store data values, in simple words it is linear collection of Nodes connected together. Link List (Linear Structure) All these nodes are independent of each other and connected using pointers. An analogy: Suppose in a shopping store there are 10 empty shopping carts available for coming customers as shown below, When a customer arrives, he takes a shopping cart and start putting the things he want to purchase in it in case shopping cart is full he takes another empty shopping cart and he can take as many shopping carts as he needs. Now suppose three customers A, B and C used 1, 2 and 3 shopping carts respectively for putting there goods as shown below, Customer A Customer B Customer C So 6 shopping carts were used and there are 4 shopping carts available for new customers. Data Structures (CS301) 3
4 If we see this scenario in the context of link list, The total number of shopping cart corresponds to the available computer memory. The customers with shopping cart make a linked list. You can well imagine that we can t define the fix size for this sort of memory requirements (shopping cart) because the no of persons demanding the shopping cart will change from time to time. At 8:00 am At 10:00 am At 12:00 pm At 2:00 pm Fig. LinkList status at different times Now suppose our customers A, B and C are computer processes that are requesting memory in the form of small nodes from the available computer memory. We can t allocate these nodes easily to processes as we are allocating shopping carts to customers in the case of shopping store. The problem is described below, Problem: We have computer memory in the form of large collection of bytes and many processes including those who belong to operating system itself are using this memory randomly when they need some memory they request for it to the operating system and get it. For example in case of three processes A, B and C an example diagram at a certain time is shown below, A Single Node int Value Node * ptr A single Node in Memory Value An integer (two bytes) Pointer (two bytes) Data Structures (CS301) 4
5 Suppose we created three nodes in our program they will be stored in memory at different locations as given below, Node I Node II Node III Now the question arises how we will access all these nodes, as these are present at different memory locations, we solve this problem by making a logical chain of nodes by connecting these nodes by pointers as shown below, Data Structures (CS301) 5
6 Node Pointer of first node points to second node int value of node in two bytes node pointer having address of next node(two bytes) int value of node in two bytes node pointer having address of next node(two bytes) Node I Node II Node Pointer of second node points to third node int value of node in two bytes node pointer having address of next node(two bytes) Node III Implementation of Singly linked list using Dev c++: To implement singly link list we need mainly two classes one is class node (like shopping cart) and second is the single link list class used to manage (add and remove) these nodes. The node class for singly link list is given blow, Data Structures (CS301) 6
7 /* Node.h file */ #include "iostream" using namespace std; class Node private: The data members of this Node class int object; Node * nextnode; One is of int type to store our data Second is Node pointer (Node *) to store the address of next node. public: void set (int ); int get(); void setnext(node * ); Node * getnext( ); Functions of this Node class As our node class has two members first data member is int variable to store an integer value in this node and second is pointer to next node) so there should be four functions, two for integer member (one for getting its value and one for setting its value) similarly two functions for Node pointer member (one getter and one setter). ; Now we write this Node class functions implementation, /* Node.cpp file */ #include Node.h int Node :: get() return object; void Node::set(int object) this->object = object; Data Structures (CS301) 7
8 Node * Node::getNext() return nextnode; void Node::setNext(Node*nextNode) this->nextnode = nextnode; Then we write a link list class so that we can create and mange a link list using the Node class objects we defined above. The implementation of Singly Link List: /* The List class List.h file*/ #include Node.h class List Including node.h file as we will use node class in list class. private: int size; Node * headnode; Node * currentnode; Node * lastcurrentnode; public: List(); void add (int addobject); int get(); bool next(); void remove(); void start(); The data members of this List class: Size to store size of current list. Head Node to store the address of the first element of the List. Current Node to store the address of the current node in the list. Last Current Node to store the address of the previous node to the current node in the list. Functions of this List class: Constructor to initialize the variables of the List. Function to add values in the List. Function to get values from the List. Function to move one position ahead. Function to remove current node Function to move current node pointer to head node. Data Structures (CS301) 8
9 ; friend void traverse(list list); friend List addnodes(); Two friend functions just to add values and display values in the list in a systematic way instead of writing all code in main function. Displaying the values in the list by moving in the list. Second to add values in the list. /* List.cpp file */ #include List.h /*Constructor of List*/ List::List() headnode = new Node(); headnode->setnext(null); currentnode = NULL; lastcurrentnode = NULL; size = 0; /* add() class method */ void List::add (int addobject) Node * newnode = new Node(); newnode->set(addobject); if( currentnode!= NULL ) newnode->setnext(currentnode- >getnext()); currentnode->setnext( newnode ); lastcurrentnode = currentnode; currentnode = newnode; else newnode->setnext(null); headnode->setnext(newnode); lastcurrentnode = headnode; currentnode = newnode; size ++; Creating Head node of the list Setting next node and previous node pointers to NULL as at this moment we have no Node having values head node is not used to store value it just point to first node in the list. Setting size of the List equal to zero. First we create a new node Setting this node value equal to the value we want to store in this node (passed to this function) Now looking if this node is the first node to be added in the list (in that case currentnode pointer will be NULL as we have set it NULL in constructor ) If it is not the first node to be added set this node next pointer to currentnode next pointer and current node next pointer to this node next pointer and now current node will be this node and last current node will the node which was current node previously. If it is first node to be added simply set head node next node pointer and current node pointer to this node and last current node pointer to head node (draw it pictorially you will have better idea) Data Structures (CS301) 9
10 void List:: remove() if(currentnode!=null && currentnode!= headnode) lastcurrentnode->setnext(currentnode- >getnext()); delete currentnode; currentnode = lastcurrentnode->getnext(); size--; ; If list is empty then do nothing (in that case either currentnode will be null or will be pointing to headnode first case will be if we have created list object only have not added any value and second case will be when we have created list object and have also called start method to move current pointer to head, in this case also list has no elements) If list has one or more elements then simply set lastcurrentnode next pointer value to currentnode next pointer value and set currentnode value to it also and delete currentnode. (pictorial diagram will explain this concept to you more ) /* get() class method */ int List::get() if (currentnode!= NULL) return currentnode->get(); Return current node value. /* next() class method */ bool List::next() if (currentnode == NULL) return false; Move current and last current node pointers one node ahead. If list size is zero or current node is already pointing to last node then simply return false otherwise returning true indicating that function call is successful. lastcurrentnode = currentnode; currentnode = currentnode->getnext(); if (currentnode == NULL size == 0) return false; else return true; /*position currentnode and lastcurrentnode at first element*/ void List::start() lastcurrentnode = headnode; currentnode = headnode; ; Move to the start of the list by setting current node and last current node pointers to head node. Data Structures (CS301) 10
11 /* Friend function to traverse linked list */ void traverse(list list) Node* savedcurrentnode = list.currentnode; list.currentnode = list.headnode; Simply moving in the list and displaying its values this can be done in main function as well but this way is more systematic. for(int i = 1; list.next(); i++) cout << "\n Element " << i << " " << list.get(); list.currentnode = savedcurrentnode; /* Friend function to add Nodes into the list */ List addnodes() List list; list.add(2); list.add(6); list.add(8); list.add(7); list.add(1); cout << "\n List size = " << list.size <<'\n'; return list; Simply adding some values to passed List object this can be done in main as well but this way is more systematic. /*At the end we write main function*/ #include "List.h" Main function in main.cpp file int main(int argc, char *argv[]) List list = addnodes(); traverse(list); system("pause"); return EXIT_SUCCESS; Data Structures (CS301) 11
12 The pictorial Diagrams how we will do this in Dev c++ are shown below, Data Structures (CS301) 12
13 Data Structures (CS301) 13
14 Data Structures (CS301) 14
15 Data Structures (CS301) 15
16 Doubly Link List Implementation: For doubly link list there will be two pointers in node class one to point to next node and other to point to previous node as shown below, class Node private: The data members of this Node class int object; Node * nextnode; Node * prevnode; One is of int type to store our data Second is Node pointer (Node *) to store the address of next node. Third is Node pointer (Node *) to store the address of previous node. public: /* Necessary functions to set and get the value of private data members of this node As node has now three members so there will be six functions now */ ; We will also write a doubly link list class to mange this node class, the functions we will write for this node class are given below with their description, DoublyLinkedList constructor This will be called once when we will create the doubly linked list object. In it we will create head node and will set this head node next and previous node pointers to NULL and list size equal to zero. Data Structures (CS301) 16
17 add Method next Method prev Method get Method Traverse forward method Traverse backward method We will also set currentnode and lastcurrentnode pointers to NULL. Pointers data members are assigned a value called NULL so that they don t point to any incorrect memory location before we assign them a value later ) This will add a new Node to the list it is similar to singly link list add method with the change that now we will also set prevnode pointer to previous node. Will move the currentnode and lastcurrrentnode pointers one node ahead. Will move currentnode and lastcurrentnode pointers to one node backward. This method will return the current node value (the node pointed by currentnode pointer) This method will traverse (move from first node called head to last node called tail of the list) and will show each node value. This method will traverse backward (move from last node called tail to the first node head of the list) and will show each node value. The process for this doubly link list project is shown below, Create new project: Data Structures (CS301) 17
18 Data Structures (CS301) 18
19 Save this project file in a new folder, Now we will add new files in this project first adding Node.h as shown below, Data Structures (CS301) 19
20 Then we will write the code for Node.h as shown below, Data Structures (CS301) 20
21 Similarly we will add three more files, having names, Node.cpp DoublyLinkList.h DoublyLinkList.cpp Data Structures (CS301) 21
22 Our project will look like this, The basic code for doubly link list we will write in these files is given below, /*Node.h file*/ #include <iostream> #include <stdlib.h> using namespace std; /* The Node class declaration */ class Node public: int get(); void set(int ); Node * getnext(); void setnext(node *); Node * getprev(); void setprev(node *); private: int object; Node * nextnode; Node * prevnode; ; /*Node.cpp file*/ #include "Node.h" Data Structures (CS301) 22
23 /* The Node class implementation*/ int Node:: get() return object; void Node:: set(int object) this->object = object; Node * Node::getNext() return nextnode; void Node:: setnext(node * nextnode) this->nextnode = nextnode; Node * Node::getPrev() return prevnode; void Node:: setprev(node * prevnode) this->prevnode = prevnode; /*DoublyLinkList.h file*/ #include "Node.h" /* The LinkList class declaration*/ class DoublyLinkList public: DoublyLinkList(); void add (int addobject); Data Structures (CS301) 23
24 int get(); bool next(); bool prev (); friend void traverseforward(doublylinklist list); friend void traversereverse(doublylinklist list); friend DoublyLinkList addnodes(); private: int size; Node * headnode; Node * currentnode; Node * lastcurrentnode; ; /*DoublyLinkList.cpp file*/ #include "DoublyLinkList.h" /* The LinkList class implementation*/ /* Constructor */ DoublyLinkList::DoublyLinkList() headnode = new Node(); headnode->setnext(null); headnode->setprev(null); currentnode = NULL; lastcurrentnode = NULL; size = 0; /* add() class method */ void DoublyLinkList::add (int addobject) Node * newnode = new Node(); newnode->set(addobject); if( currentnode!= NULL ) // If list is not empty newnode->setnext(currentnode->getnext()); if (currentnode->getnext()!= NULL) currentnode->getnext()->setprev(newnode); Data Structures (CS301) 24
25 newnode->setprev(currentnode); currentnode->setnext( newnode ); lastcurrentnode = currentnode; currentnode = newnode; else // If list is empty newnode->setnext(null); newnode->setprev(headnode); headnode->setnext(newnode); lastcurrentnode = headnode; currentnode = newnode; size ++; /* get() class method */ int DoublyLinkList::get() if (currentnode!= NULL) return currentnode->get(); /* next() class method */ bool DoublyLinkList::next() if (currentnode == NULL) return false; // no node in list lastcurrentnode = currentnode; currentnode = currentnode->getnext(); if (currentnode == NULL size == 0) // we have reached at the tail no more nodes return false; else return true; /* prev() class method */ bool DoublyLinkList::prev() if (currentnode == NULL) return false; // no node in list Data Structures (CS301) 25
26 currentnode = lastcurrentnode; lastcurrentnode = lastcurrentnode->getprev(); if (lastcurrentnode == NULL size == 0) // we have reached at the head no more nodes return false; else return true; /* Friend function to traverse linked list */ void traverseforward(doublylinklist list) Node* savedcurrentnode = list.currentnode; list.currentnode = list.headnode; cout<<"\n Traversing in Forward Direction"; for(int i = 1; list.next(); i++) cout << "\n Element " << i << " of the list is " << list.get()<< endl; list.currentnode = savedcurrentnode; void traversereverse(doublylinklist list) Node* savedcurrentnode = list.currentnode; cout<<"\n Traversing in Reverse Direction"; do cout << "\n Element " << list.size << " of the list is " << list.get()<< endl; list.size = list.size -1; while (list.prev()); list.currentnode = savedcurrentnode; Data Structures (CS301) 26
27 /* Friend function to add Nodes into the list */ DoublyLinkList addnodes() DoublyLinkList list; int temp; for(int i = 0; i < 9 ; i++) cout <<"\n Please Enter the Element No # "<<i+1<<": "; cin>>temp; list.add(temp); cout << "\n\n List size is = " << list.size <<'\n'; return list; /*Main file code*/ #include "DoublyLinkList.h" int main() DoublyLinkList list = addnodes(); traverseforward(list); traversereverse(list); system("pause"); return 0; Sample out put is Data Structures (CS301) 27
28 Data Structures (CS301) 28
Data Structures (CS301) LAB
Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Doubly Linked List practically using c++ and adding more functionality in it. Introduction to Singly
More informationData Structures. Lecture 04 Sohail Aslam AL 1
Data Structures Lecture 04 Sohail Aslam 1 C++ Code for Linked List // position current before the first // list element void start() { lastcurrentnode = headnode; currentnode = headnode; }; 2 C++ Code
More informationLinked List using a Sentinel
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
More informationData Structures Lab II. Binary Search Tree implementation
Data Structures Lab II Binary Search Tree implementation Objectives: Making students able to understand basic concepts relating to Binary Search Tree (BST). Making students able to implement Binary Search
More information// Pointer to the first thing in the list
Linked Lists Dynamic variables combined with structs or classes can be linked together to form dynamic lists or other structures. We define a record (called a node) that has at least two members: next
More informationCA341 - Comparative Programming Languages
CA341 - Comparative Programming Languages David Sinclair Dynamic Data Structures Generally we do not know how much data a program will have to process. There are 2 ways to handle this: Create a fixed data
More information! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first
Linked Lists Introduction to Linked Lists A data structure representing a Week 8 Gaddis: Chapter 17 CS 5301 Spring 2014 Jill Seaman A series of dynamically allocated nodes chained together in sequence
More informationCSCE 110 PROGRAMMING FUNDAMENTALS
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists Prof. amr Goneid, AUC 1 Linked Lists Prof. amr Goneid, AUC 2 Linked Lists The Linked List Structure Some Linked List
More informationLecture No.04. Data Structures
Lecture No.04 Data Structures Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 07: Linked Lists MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Linked Lists 2 Introduction Linked List Abstract Data Type SinglyLinkedList ArrayList Keep in Mind Introduction:
More informationJordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A
Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A Name: ID#: Section #: Day & Time: Instructor: Answer all questions as indicated. Closed book/closed
More information[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University
[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9 Seungkyu Lee Assistant Professor, Dept. of Computer Engineering Kyung Hee University CHAPTER 9 Pointers #1~2 Pointer int main () { int a; int b; int c;
More informationMIDTERM EXAMINATION Spring 2010 CS301- Data Structures
MIDTERM EXAMINATION Spring 2010 CS301- Data Structures Question No: 1 Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the
More informationLinked Lists. Chapter 4
Linked Lists Chapter 4 1 Linked List : Definition Linked List: A collection of data items of the same type that are stored in separate objects referred to as "nodes". Each node contains, in addition to
More informationLECTURE 03 LINKED LIST
DATA STRUCTURES AND ALGORITHMS LECTURE 03 LINKED LIST IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD LINKED LISTS DEFINITION A linked list is a data structure where each object is stored in
More informationC++ - Lesson 2 This is a function prototype. a' is a function that takes an integer array argument and returns an integer pointer.
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
More informationDynamic Data Structures
Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To
More informationLinked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list
Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is
More informationLinked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list
Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is
More informationC:\Temp\Templates. Download This PDF From The Web Site
11 2 2 2 3 3 3 C:\Temp\Templates Download This PDF From The Web Site 4 5 Use This Main Program Copy-Paste Code From The Next Slide? Compile Program 6 Copy/Paste Main # include "Utilities.hpp" # include
More informationl 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)
Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationUniversity of Illinois at Urbana-Champaign Department of Computer Science. First Examination
University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section
More informationlecture09: Linked Lists
lecture09: Largely based on slides by Cinda Heeren CS 225 UIUC 24th June, 2013 Announcements mp2 due tonight mt1 tomorrow night! mt1 review instead of lab tomorrow morning mp3 released tonight, mp3.1 extra
More informationCh. 17: Linked Lists. Introduction to Linked Lists
Ch. 17: Linked Lists Part 1 CS 2308 Fall 2011 Jill Seaman Lecture 16 Using content from textbook slides: Starting Out with C++, Gaddis, Pearson/Addison-Wesley 1 Introduction to Linked Lists A data structure
More informationStudent Name and ID CS 32, WINTER 2015, PRACTICE MIDTERM I.
UCLA Computer Science Department TA: Kung-Hua Chang Student Name and ID CS 32, WINTER 2015, PRACTICE MIDTERM I. Problem # Maximal Possible Points Received 1.1 3 1.2 5 1.3 5 1.4 5 1.5 5 2 3 3.1 4 3.2 5
More information! 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)
Final Exam Exercises CS 2308 Spring 2014 Jill Seaman Chapters 1-7 + 11 Write C++ code to: Determine if a number is odd or even Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationLinked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil
Linked Lists Gaddis Ch. 17 CS 2308 :: Spring 2016 Molly O'Neil List ADT A list is an abstract data type representing an ordered sequence of values For example, both MP3 Player assignments have used lists:
More informationReview Questions for Final Exam
CS 102 / ECE 206 Spring 11 Review Questions for Final Exam The following review questions are similar to the kinds of questions you will be expected to answer on the Final Exam, which will cover LCR, chs.
More informationUniversity of Illinois at Urbana-Champaign Department of Computer Science. First Examination
University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section
More informationFORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):
FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each): 1. If a function has default arguments, they can be located anywhere
More informationCS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp
1 CS 103 Unit 15 Doubly-Linked Lists and Deques Mark Redekopp 2 Singly-Linked List Review Used structures/classes and pointers to make linked data structures Singly-Linked Lists dynamically allocates each
More informationFirst Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19
Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2008 7p-9p, Tuesday, February 19 Name: NetID: Lab Section (Day/Time): This is a closed book and closed
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 07: Linked Lists and Iterators MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Linked Lists 2 Introduction Linked List Abstract Data Type General Implementation of the ListADT
More information! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first
Ch. 17: Linked Lists 17.1 Introduction to Linked Lists! A data structure representing a list! A series of nodes chained together in sequence CS 2308 Spring 2013 Jill Seaman - Each node points to one other
More information2 2
1 2 2 3 3 C:\Temp\Templates 4 5 Use This Main Program 6 # include "Utilities.hpp" # include "Student.hpp" Copy/Paste Main void MySwap (int Value1, int Value2); int main(int argc, char * argv[]) { int A
More informationData Structure. Recitation III
Data Structure Recitation III Topic Binary Search Abstract Data types Java Interface Linked List Binary search Searching a sorted collection is a common task. A dictionary is a sorted list of word definitions.
More informationImplementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations
Readings List Implementations Chapter 20.2 Objectives Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations Additional references:
More informationCMPT 225. Lecture 6 linked list
CMPT 225 Lecture 6 linked list 1 Last Lecture Class documentation Linked lists and its operations 2 Learning Outcomes At the end of this lecture, a student will be able to: define one of the concrete data
More informationLargest Online Community of VU Students
WWW.VUPages.com http://forum.vupages.com WWW.VUTUBE.EDU.PK Largest Online Community of VU Students MIDTERM EXAMINATION SEMESTER FALL 2003 CS301-DATA STRUCTURE Total Marks:86 Duration: 60min Instructions
More informationMID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.
MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the elements may locate at far positions
More informationName: UTLN: CS login: Comp 15 Data Structures Midterm 2018 Summer
[Closed book exam] There are 7 questions leading up to 100 points. Max alloted time: 1 hour Problem 1 (2x10=20 points). Fill in the blanks in terms of the big-theta (Θ) notation to show the asymptotic
More information1. Which of the following best describes the situation after Line 1 has been executed?
Instructions: Submit your answers to these questions to the Curator as OQ3 by the posted due date and time. No late submissions will be accepted. For the next three questions, consider the following short
More informationCSCE 110 PROGRAMMING FUNDAMENTALS
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table
More informationCommon Misunderstandings from Exam 1 Material
Common Misunderstandings from Exam 1 Material Kyle Dewey Stack and Heap Allocation with Pointers char c = c ; char* p1 = malloc(sizeof(char)); char** p2 = &p1; Where is c allocated? Where is p1 itself
More informationAnnouncements. Prelude (2) Prelude (1) Data Structures and Information Systems Part 1: Data Structures. Lecture 6: Lists.
Announcements MODULE WEB-SITE: http://www.csc.liv.ac.uk/ michele/teaching/comp102/comp102.html FIRST ASSIGNMENT DEADLINE: Wednesday, February 1st, 14.30, LAB 7 Boxes (late submissions to be left in the
More informationCSCE 2014 Final Exam Spring Version A
CSCE 2014 Final Exam Spring 2017 Version A Student Name: Student UAID: Instructions: This is a two-hour exam. Students are allowed one 8.5 by 11 page of study notes. Calculators, cell phones and computers
More informationAssignment 4: SkipList
Assignment 4: SkipList Goals : Working with dynamic arrays, pointers, doubly linked lists For this assignment, you will write a Skip List data structure to store integers. When searching a Skip List, items
More informationCE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings
CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location
More informationEE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp
1 EE 355 Unit 11b Doubly-Linked Lists and Deques Mark Redekopp 2 Singly-Linked List Review Used structures/classes and pointers to make linked data structures Singly-Linked Lists dynamically allocates
More informationInsertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue
Queues CSE 2011 Fall 2009 9/28/2009 7:56 AM 1 Queues: FIFO Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Applications,
More informationIntroduction to Linked Data Structures
Introduction to Linked Data Structures A linked data structure consists of capsules of data known as nodes that are connected via links Links can be viewed as arrows and thought of as one way passages
More informationSolution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:
VIRG INIA POLYTECHNIC INSTITUTE AND STATE U T PROSI M UNI VERSI TY Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted
More informationWeek 11. Abstract Data Types. CS 180 Sunil Prabhakar Department of Computer Science Purdue University
Week 11 Abstract Data Types CS 180 Sunil Prabhakar Department of Computer Science Purdue University Unknown input size Consider a program that has to read in a large number of names from input and print
More informationMidterm Exam. Sample Solutions
Name: CS 410 Introduction to Software Engineering Fall 2016 Instructor: Marc Pomplun Midterm Exam Sample Solutions No books, no notes, and no calculators are allowed. Question 1: out of points Question
More informationAgenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures
The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF
More informationCS 103 Unit 11. Linked Lists. Mark Redekopp
1 CS 103 Unit 11 Linked Lists Mark Redekopp 2 NULL Pointer Just like there was a null character in ASCII = '\0' whose ue was 0 There is a NULL pointer whose ue is 0 NULL is "keyword" you can use in C/C++
More informationCSCS 261 Programming Concepts Exam 2 Fall EXAM 2 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.
Name: Print legibly! Section: COMPUTER SCIENCE 261 PROGRAMMING CONCEPTS EXAM 2 VERSION 1 Fall 2014 150 Points Absolutely no electronic devices may be used during this exam. 1. No cell phones, computers,
More information! 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)
Final Exam Exercises Chapters 1-7 + 11 Write C++ code to:! Determine if a number is odd or even CS 2308 Fall 2018 Jill Seaman! Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationCMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am
CMSC 202 Section 010x Spring 2007 Computer Science II Final Exam Name: Username: Score Max Section: (check one) 0101 - Justin Martineau, Tuesday 11:30am 0102 - Sandeep Balijepalli, Thursday 11:30am 0103
More informationC++ for Java Programmers
Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even
More informationCS427 Inheritance and Virtual Functions. Linked lists. 2/27. 01Replacement.cpp link! i n t GetY ( void ) { return ( y ) ; } ;
Inheritance and Virtual Functions. Linked lists. CS427 Lecture 12.2, 11am, 26th March 2012 In today s class 1 Recall... Inheritance 2 3 4 Limitations of arrays 5 Linked lists 6 7 8 Further linked list
More informationCSCS 261 Programming Concepts Exam 2 Fall EXAM 2 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.
Name: Print legibly! Section: COMPUTER SCIENCE 261 PROGRAMMING CONCEPTS EXAM 2 VERSION 1 Fall 2014 150 Points Absolutely no electronic devices may be used during this exam. 1. No cell phones, computers,
More informationLinked List Practice Questions
Linked List Practice Questions 1. The following function reverse() is supposed to reverse a singly linked list. There is one line missing at the end of the function. /* head_ref is a double pointer which
More informationA506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).
A506 / C201 Computer Programming II Placement Exam Sample Questions For each of the following, choose the most appropriate answer (2pts each). 1. Which of the following functions is causing a temporary
More informationEECE.3220: Data Structures Spring 2017
EECE.3220: Data Structures Spring 2017 Lecture 14: Key Questions February 24, 2017 1. Describe the characteristics of an ADT to store a list. 2. What data members would be necessary for a static array-based
More informationCS 103 Unit 11. Linked Lists. Mark Redekopp
1 CS 103 Unit 11 Linked Lists Mark Redekopp 2 NULL Pointer Just like there was a null character in ASCII = '\0' whose ue was 0 There is a NULL pointer whose ue is 0 NULL is "keyword" you can use in C/C++
More information! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)
Final Exam Exercises Chapters 1-7 + 11 Write C++ code to:! Determine if a number is odd or even CS 2308 Spring 2013 Jill Seaman! Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationData Structures and Algorithms
Data Structures and Algorithms First Semester 2017/2018 Linked Lists Eng. Anis Nazer Linked List ADT Is a list of nodes Each node has: data (can be any thing, int, char, Person, Point, day,...) link to
More informationHomework Assignment #1
CISC 2200 Data Structure Spring, 2016 Homework Assignment #1 1 Short practices on linked list, see Textbook Page 205, Problem 9-12 2 Pointers: operations (deference, address-of), and syntax errors (a)
More informationCSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1
CSE 143 Linked Lists [Chapter 4; Chapter 6, pp. 265-271] Linked Lists A linked list is a collection of dynamically allocated nodes Each node contains at least one member (field) that points to another
More informationAssignment of Objects
Copying Objects 1 Assignment of Objects 2 Slides 1. Table of Contents 2. Assignment of Objects 3. Dynamic Content 4. Shallow Copying 5. Deep Copying 6. this Pointer 7. Improved Deep Copy 8. Passing an
More information2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked
Chapter 6 LISTS AND STRINGS 1. List Specifications 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked 3. Strings 4. Application:
More informationCS 103: Introduction to Programming Fall Written Final Exam 12/11/16, 4:30PM 6:30PM
CS 103: Introduction to Programming Fall 2017 - Written Final Exam 12/11/16, 4:30PM 6:30PM Name: USC Email Address: Lecture (Circle One): Redekopp 2:00 TTh Goodney: 2 MW 9:30 TTh 11:00 TTh Complete the
More informationPointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.
Pointers and Arrays CS 201 This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers Powerful but difficult to master Used to simulate pass-by-reference
More informationCS 216 Exam 1 Fall SOLUTION
CS 216 Exam 1 Fall 2004 - SOLUTION Name: Lab Section: Email Address: Student ID # This exam is closed note, closed book. You will have an hour and fifty minutes total to complete the exam. You may NOT
More informationCMSC 341 Lecture 7 Lists
CMSC 341 Lecture 7 Lists Today s Topics Linked Lists vs Arrays Nodes Using Linked Lists Supporting Actors (member variables) Overview Creation Traversal Deletion UMBC CMSC 341 Lists 2 Linked Lists vs Arrays
More informationAssignment #1 (50 points; due 11:59 P.M.)
Assignment #1 (50 points; due 9/25/2017 @ 11:59 P.M.) This project is designed to help you master pointers. To that end, you'll get the most out of it by working through the problems by hand. Only after
More informationCS2141 Software Development using C/C++ C++ Basics
CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short
More informationMULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR
STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM
More informationA linked list. Subtopic: Linked lists. List-node structure type. Array implementation of a linked list. Inserting into a sorted collection
Subtopic: Linked lists A linear structure, like an array Composed of self-referential nodes Operations: insert, delete, traverse Array implementation Dynamic-allocation implementation David Keil 1/03 1
More informationC++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I
C++ ARRAYS POINTERS POINTER ARITHMETIC Problem Solving with Computers-I General model of memory Sequence of adjacent cells Each cell has 1-byte stored in it Each cell has an address (memory location) Memory
More informationName MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.
CMPSC11 Final (Study Guide) Fall 11 Prof Hartman Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) This is a collection of statements that performs
More informationA collision is said to occur when two or more keys hash to the same index location.
Problem Set #8 Solutions Problem 1-9 points Given the tree below, traverse the tree a. Inorder : a/b c * d*e / f + g b. Preorder: * - / a b c / * d e + f g c. Postorder a b / c d e * f g + / * * - / /
More informationCs301 LetureWise Question from 1 to 22 Lecture. Lecture No 1:
Cs301 LetureWise Question from 1 to 22 Lecture What are the constraints? Lecture No 1: Constraint defines a limitation or restriction on resources of a system. In the computer, we have hard disk, memory
More informationProgramación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana
POINTERS Programación de Computadores Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana 2018-01 Pointers A pointer is a reference to
More informationBasic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable
Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is
More informationScott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes
Scott Gibson Pointers & Dynamic Memory Lecture #1 Office: LAH 103 Email: sgibson@brookdalecc.edu sgib@optonline.net Web: www.brookdalecc.edu/fac/cos/sgibson Phone: (732) 224 2285 1 2 Pre & Co Requisites
More informationReview Questions for Final Exam KEY
CS 102 / ECE 206 Spring 11 Review Questions for Final Exam KEY The following review questions are similar to the kinds of questions you will be expected to answer on the Final Exam, which will cover LCR,
More informationThe time and space are the two measure for efficiency of an algorithm.
There are basically six operations: 5. Sorting: Arranging the elements of list in an order (either ascending or descending). 6. Merging: combining the two list into one list. Algorithm: The time and space
More informationOBJECT ORIENTED PROGRAMMING
OBJECT ORIENTED PROGRAMMING LAB 3 CLASES AND OBJECTS (CONTINUATION), CONSTRUCTOR WITH PARAMETERS, COPY CONSTRUCTOR. FRIEND FUNCTIONS. In the prior lab we saw that we can initialize with values the attributes
More informationChapter 10 - Notes Applications of Arrays
Chapter - Notes Applications of Arrays I. List Processing A. Definition: List - A set of values of the same data type. B. Lists and Arrays 1. A convenient way to store a list is in an array, probably a
More informationLinked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }
Linked Lists Since a variable referencing an object just holds the address of the object in memory, we can link multiple objects together to form dynamic lists or other structures. In our case we will
More informationCS301- Data Structures LATEST SOLVED MCQS & SUBJECTIVE FROM MIDTERM PAPERS. 18 December 2014
س ت ع ين و ا ك ن ع ب د ا ك اے الله!) ی دت اور د CS301- Data Structures LATEST SOLVED MCQS & SUBJECTIVE FROM MIDTERM PAPERS With Reference Mega File 18 December 2014 Ijaz Ahmad BS Computer Science Virtual
More informationCSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes
CSE 250 Final Exam Fall 2013 Time: 3 hours. Dec 11, 2013 Total points: 100 14 pages Please use the space provided for each question, and the back of the page if you need to. Please do not use any extra
More information#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }
1. (9 pts) Show what will be output by the cout s in this program. As in normal program execution, any update to a variable should affect the next statement. (Note: boolalpha simply causes Booleans to
More informationChapter 1: Object-Oriented Programming Using C++
Chapter 1: Object-Oriented Programming Using C++ Objectives Looking ahead in this chapter, we ll consider: Abstract Data Types Encapsulation Inheritance Pointers Polymorphism Data Structures and Algorithms
More informationArrays and Linked Lists
Arrays and Linked Lists Abstract Data Types Stacks Queues Priority Queues and Deques John Edgar 2 And Stacks Reverse Polish Notation (RPN) Also known as postfix notation A mathematical notation Where every
More informationCS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren
CS32 Discussion Sec.on 1B Week 2 TA: Zhou Ren Agenda Copy Constructor Assignment Operator Overloading Linked Lists Copy Constructors - Motivation class School { public: }; School(const string &name); string
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationTemplates and STL Vectors
Templates and STL Vectors Let s start with a singly-linked-list class of strings. The code to free the list from memory is missing. Here is the relevant code: class Node private: string name; Node* next;
More information