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

Size: px
Start display at page:

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

Transcription

1 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 slow Sorted array: insertion and deletion is slow because it requires data movement Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Fall 2017 CS Husain Gholoom Page 1

2 Introduction to Linked Lists A singly linked list is a concrete data structure consisting of a sequence of nodes. It is data structure representing a list or a sequence of items that can only be accessed in order (that is, from first to last) A series of nodes chained together in sequence. Each node stores an element and a link to the next node Data items are stored in nodes, and each node contains a pointer to the next node A separate pointer (the head) points to the first item in the list. The last element points to nothing (NULL) Fall 2017 CS Husain Gholoom Page 2

3 Linked List and Values of the List Example: linked list with four nodes Fall 2017 CS Husain Gholoom Page 3

4 current = head; Copies value of head into current ; List after the statement current = head ; executes Fall 2017 CS Husain Gholoom Page 4

5 current = current->link; List after the statement current = current -> link ; executes Fall 2017 CS Husain Gholoom Page 5

6 Basic Operations on Linked Lists: The nodes are dynamically allocated - The list grows and shrinks as nodes are added / removed. Linked lists can easily insert a node between other nodes ( insert ) Linked lists can easily delete a node from between other nodes ( remove ) Linked lists can easily retrieve an element at a certain position ( findkth) Linked lists can easily locate the position of an object in a list ( find ) Linked lists can easily print the list ( printlist ) Node Insertion Example : Assume that you have the following list insert a new node with info 50 after p in this list: Fall 2017 CS Husain Gholoom Page 6

7 Suggested Steps Create New node, Store 50 in the new node Update the pointers Fall 2017 CS Husain Gholoom Page 7

8 Node Deletion Example : Node with info 34 is to be deleted: Fine the node to be deleted. Then point its previous pointer to its next pointer. Delete the pointer s node p->link = p->link ->link; Fall 2017 CS Husain Gholoom Page 8

9 Fall 2017 CS Husain Gholoom Page 9

10 Variations of linked lists Singly linked list Circular linked lists Doubly linked lists The Abstract Data Type A sequence of zero or more elements A1, A2, A3, AN N: length of the list A1: first element AN: last element Ai: position i If N=0, then empty list Linearly ordered - Ai precedes Ai+1 - Ai follows Ai-1 Fall 2017 CS Husain Gholoom Page 10

11 Implementation of an ADT Choose a data structure to represent the ADT - e.g. arrays, records, etc. Each operation associated with the ADT is implemented by one or more subroutines Two standard implementations for the list ADT - Array-based - Linked list Fall 2017 CS Husain Gholoom Page 11

12 Array Implementation Elements are stored in contiguous array positions Uses an iterator to move cursor Has a fixed size Insert at position 0 (making a new element) requires first pushing the entire array down one spot to make room Delete at position 0 requires shifting all the elements in the list up one On average, half of the lists needs to be moved for either operation Requires an estimate of the maximum size of the list waste space - printlist and find: O(n) - findkth: O(1) - insert and delete: O(n) Fall 2017 CS Husain Gholoom Page 12

13 Example : Array Implementation Let LA be a Linear Array (unordered) with N elements and K is a positive integer such that K<=N. Following is the algorithm where ITEM is inserted into the K th position of LA #include<iostream> using namespace std; int main() { int LA[] = { 1, 3, 5, 7, 8 ; int item = 10, k = 3, n = 5; int i = 0, j = n; cout << "The original array elements are :\n\n"; for (i = 0; i < n; i++) { cout << "LA[" << i << "] = " << LA[i] << endl; n = n + 1; while (j >= k) { LA[j + 1] = LA[j]; j = j - 1; LA[k] = item; cout << "\nthe array elements after insertion :\n\n"; for (i = 0; i < n; i++) { cout << "LA[" << i << "] = " << LA[i] << endl; Fall 2017 CS Husain Gholoom Page 13

14 Sample Run The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 The array elements after insertion : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 10 LA[4] = 7 LA[5] = 8 Fall 2017 CS Husain Gholoom Page 14

15 Example - Insertion at the Beginning of an Array #include<iostream> using namespace std; int main() { int array[] = { 2, 3, 4, 5 ; int N = 4; // number of elements in array int i = 0; // loop variable // print array before insertion cout << "\nprinting array before insertion \n\n"; for (i = 0; i < N; i++) { cout << "array[" << i << "] = " << array[i] << endl; // now shift rest of the elements downwards for (i = N; i >= 0; i--) { array[i + 1] = array[i]; // add new element at first position array[0] = 1000; // increase N to reflect number of elements N = N + 1; // print to confirm cout << "\nprinting array after insertion \n\n"; for (i = 0; i < N; i++) { cout << "array[" << i << "] = " << array[i] << endl; Fall 2017 CS Husain Gholoom Page 15

16 Sample Run Printing array before insertion array[0] = 2 array[1] = 3 array[2] = 4 array[3] = 5 Printing array after insertion array[0] = 1000 array[1] = 2 array[2] = 3 array[3] = 4 array[4] = 5 Fall 2017 CS Husain Gholoom Page 16

17 Pointer Implementation (Linked List) Requires no estimate of the maximum size of the list - No wasted space printlist and find: O(n) findkth: O(n) insert and delete: O(1) - e.g. insert at position 0 (making a new element) Insert does not require moving the other elements - e.g. delete at position 0 requires no shifting of elements - Insertion and deletion becomes easier, but finding the Kth element moves from O(1) to O(n) Node Organization Each node contains: - Data field may be organized as a structure, an object, etc. - A pointer that can point to another node Fall 2017 CS Husain Gholoom Page 17

18 Empty List An empty list contains 0 nodes. The list head points to NULL (address 0) (There are no nodes, it s empty) list head NULL Declaring the Linked List Data Type We will be defining a class for a linked list data type that can store values of type double. The data type will describe the values (the lists) and operations over those values. In order to define the values we must: Define a data type for the nodes Define a pointer variable (head) that points to the first node in the list. Fall 2017 CS Husain Gholoom Page 18

19 Declaring the Node data type Use a struct for the node type : this is just a data type, no variables declared next can hold the address of a ListNode. - it can also be NULL - self-referential data structure struct ListNode { double value; ListNode *next; ; Defining the Linked List variable Define a pointer for the head of the list: ListNode *head = NULL; It must be initialized to NULL to signify the end of the list. Now we have an empty linked list: head NULL Fall 2017 CS Husain Gholoom Page 19

20 Using NULL Equivalent to address 0. Used to specify end of the list. Use ONE of the following for NULL: #include <iostream> or #include <cstddef> To test a pointer for NULL (these are equivalent): while (p)... <==> while (p!= NULL)... if (!p)... <==> if (p == NULL)... Fall 2017 CS Husain Gholoom Page 20

21 So, in C++ The following are equivalent : ListNode *p = head; while ((*p).next!= NULL) p = (*p).next; ListNode *p = head; while (p->next) p = p->next; p=p->next is like i++ Fall 2017 CS Husain Gholoom Page 21

22 Linked List operations Basic operations: create a new, empty list append a node to the end of the list insert a node within the list delete a node display the linked list delete/destroy the list Linked List class declaration // file NumberList.h class NumberList { private: struct ListNode // the node data type { double value; // data ListNode *next; // ptr to next node ; ListNode *head; // the list head public: NumberList(); ~NumberList(); ; void appendnode(double); void insertnode(double); void deletenode(double); void displaylist(); Fall 2017 CS Husain Gholoom Page 22

23 Linked List functions: constructor Constructor: sets up empty list // file NumberList.cpp #include "NumberList.h" NumberList::NumberList() { head = NULL; Linked List functions: appendnode appendnode: adds new node to end of list Algorithm: Create a new node and store the data in it If the list has no nodes (it s empty) Make head point to the new node. else Find the last node in the list Make the last node point to the new node When defining list operations, always consider special cases: Empty list First element, front of the list (when head pointer is involved) Fall 2017 CS Husain Gholoom Page 23

24 Linked List functions code : appendnode void NumberList::appendNode(double num) { ListNode *newnode; // To point to the new node // Create a new node and store the data in it newnode = new ListNode; newnode->value = num; newnode->next = NULL; // If empty, make head point to new node if (!head) head = newnode; else { ListNode *nodeptr; // To move through the list nodeptr = head; // initialize to start of list // traverse list to find last node while (nodeptr->next) //it s not last nodeptr = nodeptr->next; //make it pt to next // now nodeptr pts to last node // make last node point to newnode nodeptr->next = newnode; Fall 2017 CS Husain Gholoom Page 24

25 Traversing and Displaying a Linked List Visit each node in a linked list, to - display contents, sum data, test data, etc. Basic process: set a pointer to point to what head points to while pointer is not NULL process data of current node go to the next node by setting the pointer to the pointer field of the current node end while Linked List functions code : displaylist void NumberList::displayList() { ListNode *nodeptr; //ptr to traverse the list // start nodeptr at the head of the list nodeptr = head; // while nodeptr pts to something (not NULL), continue while (nodeptr) { //Display the value in the current node cout << nodeptr->value << endl; //Move to the next node nodeptr = nodeptr->next; Fall 2017 CS Husain Gholoom Page 25

26 Or the short version: void NumberList::displayList() { ListNode *nodeptr; for (nodeptr = head; nodeptr; nodeptr = nodeptr->next) cout << nodeptr->value << endl; Driver to demo NumberList ListDriver.cpp //ListDriver.cpp: using NumberList #include "NumberList.h" int main() { // Define the list NumberList list; // Append some values to the list list.appendnode(2.5); list.appendnode(7.9); list.appendnode(12.6); // Display the values in the list. list.displaylist(); return 0; Output: Fall 2017 CS Husain Gholoom Page 26

27 Deleting a Node from a Linked List deletenode: removes node from list, and deletes (deallocates) the removed node. Requires two pointers: - one to point to the node to be deleted - one to point to the node before the node to be deleted. Change the pointer of the previous node to point to the node after the one to be deleted. previousnode->next = nodeptr->next; Now just delete the nodeptr node delete nodeptr; Fall 2017 CS Husain Gholoom Page 27

28 Delete Node Algorithm Delete the node containing num If list is empty, exit If first node is num make p point to first node make head point to second node delete p else use p to traverse the list, until it points to num or NULL --as p is advancing, make n point to the node before if (p is not NULL) make n s node point to what p s node points to delete p s node Fall 2017 CS Husain Gholoom Page 28

29 Linked List functions: deletenode void NumberList::deleteNode(double num) { if (!head) // empty list, exit ( head == NULL ) return; ListNode *nodeptr; // to traverse the list if (head->value == num) { // if first node is num nodeptr = head; head = nodeptr->next; delete nodeptr; else { ListNode *previousnode; // trailing node pointer nodeptr = head; // traversal ptr, set to first node // skip nodes not equal to num, stop at last while (nodeptr && nodeptr->value!= num) { previousnode = nodeptr; // save it! nodeptr = nodeptr->next; // advance it if (nodeptr) { // num is found, set links + delete previousnode->next = nodeptr->next; delete nodeptr; // else: end of list, num not found in list, do nothing Fall 2017 CS Husain Gholoom Page 29

30 Driver to demo NumberList ListDriver.cpp // set up the list NumberList list; list.appendnode(2.5); list.appendnode(7.9); list.appendnode(12.6); list.displaylist(); // Nice test case, checks all three cases cout << endl << "remove 7.9:" << endl; list.deletenode(7.9); list.displaylist(); cout << endl << "remove 8.9: " << endl; list.deletenode(8.9); list.displaylist(); cout << endl << "remove 2.5: " << endl; list.deletenode(2.5); list.displaylist(); Output: remove 7.9: remove 8.9: remove 2.5: 12.6 Fall 2017 CS Husain Gholoom Page 30

31 Destroying a Linked List The destructor must delete (deallocate) all nodes used in the list To do this, use list traversal to visit each node For each node, - Save the address of the next node in a pointer - Delete the node Linked List functions: destructor ~NumberList: deallocates all the nodes NumberList::~NumberList() { ListNode *nodeptr; // traversal ptr ListNode *nextnode; // saves the next node nodeptr = head; //start at head of list while (nodeptr) { nextnode = nodeptr->next; // save the next delete nodeptr; // delete current nodeptr = nextnode; // advance ptr Fall 2017 CS Husain Gholoom Page 31

32 Inserting a Node into a Linked List Requires two pointers: - pointer to point to the node after the insertion point - pointer to point to node before point of insertion New node is inserted between the nodes pointed at by these pointers The before and after pointers move in tandem as the list is traversed to find the insertion point - Like delete Insert Node Algorithm Insert node in a certain position Create the new node, store the data in it If list is empty, make head point to new node, new node to null else use p to traverse the list, until it points to node after insertion point or NULL --as p is advancing, make n point to the node before if p points to first node (n is null) make head point to new node new node to p s node else make n s node point to new node make new node point to p s node Fall 2017 CS Husain Gholoom Page 32

33 Note that the insertnode implementation that follows assumes that the list is sorted. The insertion point is - immediately before the first node in the list that has a value greater than the value being inserted or - at the end if the value is greater than all items in the list. Not always applicable, but it is a good demonstration of inserting a node in the middle of a list. Fall 2017 CS Husain Gholoom Page 33

34 Linked List functions: insertnode insertnode: inserts num into middle of list void NumberList::insertNode(double num) { ListNode *newnode; // ptr to new node ListNode *nodeptr; // ptr to traverse list ListNode *previousnode; // node previous to nodeptr //allocate new node newnode = new ListNode; newnode->value = num; // empty list, insert at front if (!head) { head = newnode; newnode->next = NULL; else { // initialize the two traversal ptrs nodeptr = head; previousnode = NULL; // skip all nodes less than num while (nodeptr && nodeptr->value < num) { previousnode = nodeptr; // save nodeptr = nodeptr->next; // advance if (previousnode == NULL) { //insert before first head = newnode; newnode->next = nodeptr; else { //insert after previousnode previousnode->next = newnode; newnode->next = nodeptr; What if num is bigger than all items in the list? Fall 2017 CS Husain Gholoom Page 34

35 Driver to demo NumberList ListDriver.cpp int main() { // set up the list NumberList list; list.appendnode(2.5); list.appendnode(7.9); list.appendnode(12.6); list.insertnode(10.5); list.displaylist(); return 0; Output: Fall 2017 CS Husain Gholoom Page 35

36 Advantages of linked lists (over arrays) A linked list can easily grow or shrink in size. - The programmer doesn t need to predict how many values could be in the list. - The programmer doesn t need to resize (copy) the list when it reaches a certain capacity. When a value is inserted into or deleted from a linked list, none of the other nodes have to be moved. Advantages of arrays (over linked lists) Arrays allow random access to elements: array[i] - linked lists allow only sequential access to elements (must traverse list to get to i th element). Arrays do not require extra storage for links Fall 2017 CS Husain Gholoom Page 36

37 Linked List variations Circular linked list - A list in which each node has a successor; the last element is succeeded by the first element Fall 2017 CS Husain Gholoom Page 37

38 Doubly linked list - Each node has two pointers, o one to the next node and o one to the previous node - Head points to first element, tail points to last. - Can traverse list in reverse direction by starting at the tail and using p=p->prev. Fall 2017 CS Husain Gholoom Page 38

39 Insertion We visualize operation insertafter(p, X), which returns position q Fall 2017 CS Husain Gholoom Page 39

40 Deletion We visualize remove(p), where p == last() Fall 2017 CS Husain Gholoom Page 40

41 Linked List in C++Standard Template Library ( STL ) Forward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements. It differs from list by the fact that forward list keeps track of location of only next element while list keeps track to both next and previous elements, thus increasing the storage space required to store each element. The drawback of forward list is that it cannot be iterated backwards and its individual elements cannot be accessed directly. Forward List is preferred over list when only forward traversal is required (same as singly linked list is preferred over doubly linked list) as we can save space. Some example cases are, chaining in hashing, adjacency list representation of graph, etc. Operations on Forward List : 1. assign() :- This function is used to assign values to forward list, its another variant is used to assign repeated elements. // Declaring forward list forward_list<int> flist1; // Assigning values using assign() flist1.assign({1, 2, 3); 2. push_front() :- This function is used to insert the element at the first position on forward list. The value from this function is copied to the space before first element in the container. The size of forward list increases by emplace_front() :- This function is similar to the previous function but in this no copying operation occurs, the element is created directly at the memory before the first element of the forward list. 4. pop_front() :- This function is used to delete the first element of list. // Initializing forward list forward_list<int> flist = {10, 20, 30, 40, 50; // Inserts 60 at front flist.push_front(60); Fall 2017 CS Husain Gholoom Page 41

42 // Deleting first value using pop_front() // Pops 60 flist.pop_front(); 5. insert_after() This function gives us a choice to insert elements at any position in forward list. The arguments in this function are copied at the desired position. // Initializing forward list forward_list<int> flist = {10, 20, 30 ; // Declaring a forward list iterator forward_list<int>::iterator ptr; // Inserting value using insert_after() // starts insertion from second position ptr = flist.insert_after(flist.begin(), {1, 2, 3); 6. erase_after() This function is used to erase elements from a particular position in the forward list. 7. remove() :- This function removes the particular element from the forward list mentioned in its argument. 8. remove_if() :- This function removes according to the condition in its argument. // Initializing forward list forward_list<int> flist = {10, 20, 30, 25, 40, 40; // Removing element using remove() // Removes all occurrences of 40 flist.remove(40); // Displaying the forward list cout << "The forward list after remove operation : "; for (int&c : flist) cout << c << " "; cout << endl; Fall 2017 CS Husain Gholoom Page 42

43 // Removing according to condition. Removes // elements greater than 20. Removes 25 and 30 flist.remove_if([](int x){ return x>20;); 9. splice_after() :- This function transfers elements from one forward list to other. // Initializing forward list forward_list<int> flist1 = {10, 20, 30; // Initializing second list forward_list<int> flist2 = {40, 50, 60; // Shifting elements from first to second // forward list after 1st position flist2.splice_after(flist2.begin(),flist1); Fall 2017 CS Husain Gholoom Page 43

44 List in C++ Standard Template Library (STL) List Lists are sequence containers that allow non-contiguous memory allocation. As compared to vector, list has slow traversal, but once a position has been found, insertion and deletion are quick. Normally, when we say a List, we talk about doubly linked list. For implementing a singly linked list, we use forward list. Functions used with List : front() Returns reference to the first element in the list back() Returns reference to the last element in the list push_front(g) Adds a new element g at the beginning of the list push_back(g) Adds a new element g at the end of the list pop_front() Removes the first element of the list, and reduces size of the list by 1 pop_back() Removes the last element of the list, and reduces size of the list by 1 begin() Returns an iterator pointing to the first element of the list end() Returns an iterator pointing to the theoretical last element which follows the last elementempty() Returns whether the list is empty(1) or not(0) insert() Inserts new elements in the list before the element at a specified position erase() Removes a single element or a range of elements from the list assign() Assigns new elements to list by replacing current elements and resizes the list remove() Removes all the elements from the list, which are equal to given element reverse() Reverses the list size() Returns the number of elements in the list sort() Sorts the list in increasing order Fall 2017 CS Husain Gholoom Page 44

45 Example #include <iostream> #include <list> #include <iterator> using namespace std; //function for printing the elements in a list void showlist(list<int> g) { list<int>::iterator it; for (it = g.begin(); it!= g.end(); ++it) cout << '\t' << *it; cout << '\n'; int main() { list<int> gqlist1, gqlist2; for (int i = 0; i < 10; ++i) { gqlist1.push_back(i * 2); gqlist2.push_front(i * 3); cout << "\nlist 1 (gqlist1) is : "; showlist(gqlist1); cout << "\nlist 2 (gqlist2) is : "; showlist(gqlist2); cout << "\ngqlist1.front() : " << gqlist1.front(); cout << "\ngqlist1.back() : " << gqlist1.back(); cout << "\ngqlist1.pop_front() : "; gqlist1.pop_front(); showlist(gqlist1); cout << "\ngqlist2.pop_back() : "; gqlist2.pop_back(); showlist(gqlist2); cout << "\ngqlist1.reverse() : "; gqlist1.reverse(); showlist(gqlist1); cout << "\ngqlist2.sort(): "; gqlist2.sort(); showlist(gqlist2); return 0; Fall 2017 CS Husain Gholoom Page 45

46 The output of the above program is : List 1 (gqlist1) is : List 2 (gqlist2) is : gqlist1.front() : 0 gqlist1.back() : 18 gqlist1.pop_front() : gqlist2.pop_back() : gqlist1.reverse() : gqlist2.sort(): Fall 2017 CS Husain Gholoom Page 46

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 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 information

! A data structure representing a list. ! A series of dynamically allocated nodes. ! 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 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 information

! 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 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 information

Ch. 17: Linked Lists. Introduction to Linked Lists

Ch. 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 information

Chapter 17: Linked Lists

Chapter 17: Linked Lists Chapter 17: Linked Lists 17.1 Introduction to the Linked List ADT Introduction to the Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures list head

More information

! A data type for which: ! An ADT may be implemented using various. ! Examples:

! A data type for which: ! An ADT may be implemented using various. ! Examples: List ADT: Linked lists vs. Arrays CS 2308 Fall 2018 Jill Seaman Abstract Data Type! A data type for which: - only the properties of the data and the operations to be performed on the data are specific,

More information

Chapter 17: Linked Lists

Chapter 17: Linked Lists Chapter 17: Linked Lists Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 17.1 Introduction to the

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

! 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 reresenting a list! A series of nodes chained together in sequence CS 2308 Sring 2015 Jill Seaman - Each node oints to one other

More information

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

Linked 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 information

C++ is Fun Part 14 at Turbine/Warner Bros.! Russell Hanson

C++ is Fun Part 14 at Turbine/Warner Bros.! Russell Hanson C++ is Fun Part 14 at Turbine/Warner Bros.! Russell Hanson Syllabus 1) First program and introduction to data types and control structures with applications for games learning how to use the programming

More information

1 Short Answer (7 Points Each)

1 Short Answer (7 Points Each) 1 Short Answer (7 Points Each) 1. Given the following function, what operations will need to be overloaded in the class T for this code to compile? template T square(t n) { return n * n; } The

More information

Dynamic Data Structures

Dynamic 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 information

CS 103 Unit 11. Linked Lists. Mark Redekopp

CS 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

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

CS 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 information

Linked List using a Sentinel

Linked 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 information

CS 103 Unit 11. Linked Lists. Mark Redekopp

CS 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

Ashish Gupta, Data JUET, Guna

Ashish Gupta, Data JUET, Guna Categories of data structures Data structures are categories in two classes 1. Linear data structure: - organized into sequential fashion - elements are attached one after another - easy to implement because

More information

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50 Lists, Stacks, and Queues (Lists, Stacks, and Queues ) Data Structures and Programming Spring 2016 1 / 50 Abstract Data Types (ADT) Data type a set of objects + a set of operations Example: integer set

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 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 information

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

EE 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 information

CA341 - Comparative Programming Languages

CA341 - 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

Data Structures (CS301) LAB

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 information

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

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 Data Structures Course Review FINAL Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Final When: Wednesday (12/12) 1:00 pm -3:00 pm Where: In Class

More information

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)

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) 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 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)

! 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 information

Abstract Data Types 1

Abstract Data Types 1 Abstract Data Types 1 Purpose Abstract Data Types (ADTs) Lists Stacks Queues 2 Abstract Data Types (ADTs) ADT is a set of objects together with a set of operations. Abstract in that implementation of operations

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 8: Dynamically Allocated Linked Lists 2017-2018 Fall int x; x = 8; int A[4]; An array is stored as one contiguous block of memory. How can we add a fifth element to the

More information

Abstract Data Types 1

Abstract Data Types 1 Abstract Data Types 1 Purpose Abstract Data Types (ADTs) Lists Stacks Queues 2 Primitive vs. Abstract Data Types Primitive DT: programmer ADT: programmer Interface (API) data Implementation (methods) Data

More information

Data Structure Series

Data Structure Series Data Structure Series This series is actually something I started back when I was part of the Sweet.Oblivion staff, but then some things happened and I was no longer able to complete it. So now, after

More information

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University Abstract Data Types CptS 223 Advanced Data Structures Larry Holder School of Electrical Engineering and Computer Science Washington State University 1 Purpose Abstract Data Types (ADTs) Lists Stacks Queues

More information

What is recursion? Recursion. Recursive message() modified. How can a function call itself? contains a reference to itself. Week 10. Gaddis:

What is recursion? Recursion. Recursive message() modified. How can a function call itself? contains a reference to itself. Week 10. Gaddis: Recursion What is recursion? Week 10 Gaddis:19.1-19.5 CS 5301 Spring 2017 Jill Seaman 1 l Generally, when something contains a reference to itself l Math: defining a function in terms of itself l Computer

More information

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Recursion What is recursion? Week 10 Generally, when something contains a reference to itself Gaddis:19.1-19.5 CS 5301 Spring 2014 Jill Seaman 1 Math: defining a function in terms of itself Computer science:

More information

Introduction to the Linked List ADT

Introduction to the Linked List ADT L rj TOPICS 17.1 Introduction to the Linked List ADT 17.4 Variations of the linked List '7.2 Linked List Operations 17.5 The STL l ist Container 17.3 A Linked List Template Introduction to the Linked List

More information

! A data type for which: ! An ADT may be implemented using various. ! Examples:

! A data type for which: ! An ADT may be implemented using various. ! Examples: Stacks and Queues Unit 6 Chapter 19.1-2,4-5 CS 2308 Fall 2018 Jill Seaman 1 Abstract Data Type A data type for which: - only the properties of the data and the operations to be performed on the data are

More information

Advanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists

Advanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists Advanced Linked Lists Doubly Linked Lists Circular Linked Lists Multi- Linked Lists Review The singly linked list: consists of nodes linked in a single direction. access and traversals begin with the first

More information

The time and space are the two measure for efficiency of an algorithm.

The 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 information

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

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S. Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S. Bowers 1 of 11 Doubly Linked Lists Each node has both a next and a prev pointer head \ v1 v2 v3 \ tail struct

More information

Standard Template Library

Standard Template Library Standard Template Library Wednesday, October 10, 2007 10:09 AM 9.3 "Quick Peek" STL history 1990s Alex Stepanov & Meng Lee of HP Labs 1994 ANSI/IS0 standard Components Container class templates Iterators

More information

Spring 2008 Data Structures (CS301) LAB

Spring 2008 Data Structures (CS301) LAB 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

More information

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

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays Unit 3: Linked Lists Part 2: More on Linked Lists 1 Deletion in singly linked lists (cont d) 1 Other Functions Engineering 4892: Data Structures 1 Doubly Linked Lists Faculty of Engineering & Applied Science

More information

Cpt S 122 Data Structures. Data Structures

Cpt S 122 Data Structures. Data Structures Cpt S 122 Data Structures Data Structures Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Self Referential Structures Dynamic Memory Allocation

More information

Linked List. April 2, 2007 Programming and Data Structure 1

Linked List. April 2, 2007 Programming and Data Structure 1 Linked List April 2, 2007 Programming and Data Structure 1 Introduction head A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element

More information

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

University 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 information

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

Implementation. 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 information

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted, [1] Big-O Analysis AVERAGE(n) 1. sum 0 2. i 0. while i < n 4. number input_number(). sum sum + number 6. i i + 1 7. mean sum / n 8. return mean Revision Statement no. of times executed 1 1 2 1 n+1 4 n

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)

! 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 information

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

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures Data Structures in C C Programming and Software Tools N.C. State Department of Computer Science Data Structures in C The combination of pointers, structs, and dynamic memory allocation allow for creation

More information

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

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD DATA STRUCTURES AND ALGORITHMS LECTURE 08 S IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD S ABSTRACT DATA TYPE An Abstract Queue (Queue ADT) is an abstract data type that emphasizes specific

More information

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

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2 CS32 - Week 2 Umut Oztok July 1, 2016 Arrays A basic data structure (commonly used). Organize data in a sequential way. Arrays A basic data structure (commonly used). Organize data in a sequential way.

More information

Template based set of collection classes STL collection types (container types)

Template based set of collection classes STL collection types (container types) STL Collection Types Template based set of collection classes STL collection types (container types) Sequences vector - collection of elements of type T list - doubly linked list, only sequential access

More information

SSE2034: System Software Experiment 3

SSE2034: System Software Experiment 3 SSE2034: System Software Experiment 3 Spring 2016 Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu STL Collection Types Template based set of collection

More information

CS 103 Unit 12 Slides

CS 103 Unit 12 Slides 1 CS 103 Unit 12 Slides Standard Template Library Vectors & Deques Mark Redekopp 2 Templates We ve built a list to store integers But what if we want a list of double s or char s or other objects We would

More information

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures C++ Review 1 Purpose of Review Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures 2 Class The Class defines the data structure

More information

Higher National Diploma in Information Technology. First Year Second Semester Examination IT 2003-Data Structure and Algorithm.

Higher National Diploma in Information Technology. First Year Second Semester Examination IT 2003-Data Structure and Algorithm. Higher National Diploma in Information Technology First Year Second Semester Examination-2013 IT 2003-Data Structure and Algorithm Answer Guide (01) I)What is Data structure A data structure is an arrangement

More information

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

University 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 information

EECE.2160: ECE Application Programming

EECE.2160: ECE Application Programming Fall 2017 Programming Assignment #10: Doubly-Linked Lists Due Monday, 12/18/17, 11:59:59 PM (Extra credit ( 5 pts on final average), no late submissions or resubmissions) 1. Introduction This assignment

More information

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19 Data Structures Alice E. Fischer Lecture 4, Fall 2018 Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall 2018 1 / 19 Outline 1 Ordered Lists 2 Sorted Lists Tail Pointers 3 Doubly Linked Lists

More information

CS183 Software Design. Textbooks. Grading Criteria. CS183-Su02-Lecture 1 20 May by Eric A. Durant, Ph.D. 1

CS183 Software Design. Textbooks. Grading Criteria. CS183-Su02-Lecture 1 20 May by Eric A. Durant, Ph.D. 1 CS183 Software Design Dr. Eric A. Durant Email: durant@msoe.edu Web: http://people.msoe.edu/~durant/ courses/cs183 (coming soon) Office: CC43 (enter through CC27) Phone: 277-7439 1 Textbooks Required (same

More information

LECTURE 03 LINKED LIST

LECTURE 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 information

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

First 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 information

CS32 Discussion Week 3

CS32 Discussion Week 3 CS32 Discussion Week 3 Muhao Chen muhaochen@ucla.edu http://yellowstone.cs.ucla.edu/~muhao/ 1 Outline Doubly Linked List Sorted Linked List Reverse a Linked List 2 Doubly Linked List A linked list where

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists Review from Lecture 8 CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately, erasing items

More information

Arrays and Linked Lists

Arrays 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 information

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

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013 Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every

More information

CMSC 341 Lecture 7 Lists

CMSC 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 information

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CSE030 Fall 2012 Final Exam Friday, December 14, PM CSE030 Fall 2012 Final Exam Friday, December 14, 2012 3-6PM Write your name here and at the top of each page! Name: Select your lab session: Tuesdays Thursdays Paper. If you have any questions or need

More information

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Gaddis:

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Gaddis: Recursion What is recursion? Week 10! Generally, when something contains a reference to itself Gaddis:19.1-19.5! Math: defining a function in terms of itself CS 5301 Spring 2015 Jill Seaman 1! Computer

More information

Jordan 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 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

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

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1 Lab 2: Pointers 1. Goals Further understanding of pointer variables Passing parameters to functions by address (pointers) and by references Creating and using dynamic arrays Combing pointers, structures

More information

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3 UNIT 3 LINEAR DATA STRUCTURES 1. Define Data Structures Data Structures is defined as the way of organizing all data items that consider not only the elements stored but also stores the relationship between

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists! CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists! Winter 2013 Instructor: Hassan Khosravi Problems with Arrays and Vectors With arrays and vectors you are allocated a large space.

More information

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

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms Instructor: Douglas Wilhelm Harder Time: 2.5 hours Aides: none 14 pages Final Examination

More information

Queue Implementations

Queue Implementations Queue Implementations 1 Circular Queues buffer of fixed capacity improvements and cost estimates 2 Deques the double ended queue queue as double linked circular list MCS 360 Lecture 17 Introduction to

More information

5 Phases of Software Life Cycle

5 Phases of Software Life Cycle Quiz One Answer Key 5 Phases of Software Life Cycle Lecture: 5/12 Question: #1 Waterfall Model Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, 2005 Pearson Education, Inc. All

More information

+ Abstract Data Types

+ Abstract Data Types Linked Lists Abstract Data Types An Abstract Data Type (ADT) is: a set of values a set of operations Sounds familiar, right? I gave a similar definition for a data structure. Abstract Data Types Abstract

More information

Introduction to Linked List: Review. Source:

Introduction to Linked List: Review. Source: Introduction to Linked List: Review Source: http://www.geeksforgeeks.org/data-structures/linked-list/ Linked List Fundamental data structures in C Like arrays, linked list is a linear data structure Unlike

More information

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists Review from Lecture 8 CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists Designing our own container classes Dynamically allocated memory in classes Copy constructors, assignment operators,

More information

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

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013 Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every

More information

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

University 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 Sample Exam 2 75 minutes permitted Print your name, netid, and

More information

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 The data of the problem is of 2GB and the hard disk is of 1GB capacity, to solve this problem we should Use better data structures

More information

Object Oriented Programming COP3330 / CGS5409

Object Oriented Programming COP3330 / CGS5409 Object Oriented Programming COP3330 / CGS5409 Intro to Data Structures Vectors Linked Lists Queues Stacks C++ has some built-in methods of storing compound data in useful ways, like arrays and structs.

More information

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours TED (10)-3071 Reg. No.. (REVISION-2010) (Maximum marks: 100) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours PART

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)

! 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 information

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 4

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 4 Steven J. Zeil July 31, 2013 Contents 1 Linked Lists: the Basics 4 1 2 Coding for Linked Lists 8 2.1 Traversing a Linked List............................... 12 2.2 Searching a Linked List................................

More information

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

MIDTERM 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 information

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues By: Pramod Parajuli, Department of Computer Science, St. Xavier

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 [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 information

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA 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 information

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists Lecture 6: Linked Lists Object References When you declare a variable of a non-primitive type you are really declaring a reference to that object String

More information

STL: C++ Standard Library

STL: C++ Standard Library STL: C++ Standard Library Encapsulates complex data structures and algorithms CSC 330 OO Software Design 1 We ve emphasized the importance of software reuse. Recognizing that many data structures and algorithms

More information

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

A 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 information

Programming in C/C Lecture 2

Programming in C/C Lecture 2 Programming in C/C++ 2005-2006 Lecture 2 http://few.vu.nl/~nsilvis/c++/2006 Natalia Silvis-Cividjian e-mail: nsilvis@few.vu.nl vrije Universiteit amsterdam News Check announcements on the C/C++ website

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 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 information

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 3

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 3 Steven J. Zeil July 31, 2013 Contents 1 Linked Lists: the Basics 3 2 Coding for Linked Lists 7 2.1 Traversing a Linked List........................... 10 2.2 Searching a Linked List............................

More information

MID 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. 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 information

.:: UNIT 3 ::. LIST AND LINKED LIST

.:: UNIT 3 ::. LIST AND LINKED LIST :: UNIT 3 :: LIST AND LINKED LIST 31 A list is a useful structure to hold a collection of data The simplest method to implement a List ADT is to use an array linear list, contiguous list Characteristics

More information

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp 1 EE 355 Unit 10 C++ STL - Vectors and Deques Mark Redekopp 2 Templates We ve built a list to store integers But what if we want a list of double s or char s or other objects We would have to define the

More information

Introduction to Data Structures and Algorithms

Introduction to Data Structures and Algorithms Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can perform operations on these data in an effective way. Data Structures

More information