Data structures exam2

Ace your homework & exams now with Quizwiz!

prepend

add to beginning

A list traversal algorithm support what types of list?

singly-linked and doubly-linked lists.

Given a list: {55, 16, 9, 40} Append(list, GetLength(list)) Remove(list, 16) Sort(list) Print(list) how would this list look after all this?

{4, 9, 40, 55};

JAVA LList: Write out the first if statement

if (firstNode == null){ firstNode = newNode;}

JAVA Array Based List: initialize Array based list in main

ArrayBasedList list = new ArrayBasedList();

C++ Doubly-linked List: In the insert function write out the else statement if the list is not empty

Tail->next = newNode; newNode->prev = Tail; Tail = newNode;

the InsertAfter operation for a singly-linked list

does what inserts the new node after a provided existing list node.

C++ Doubly-linked list: in the append function what happens in the else statement if list is not empty

tail->next = newNode; newNode->prev = tail; tail = newNode;

JAVA AList: what is in the outer if statement for the remove method?

// get entry to be removed result = list[givenPostion -1]; //move subsequent entries toward entry to be removed, // unless it is last in last if ( givenPosition < length ) { // remove gap for(int i = givenPosition; i < length - 1; i++){ list[ i ] = list[i + 1] } } length--;

C++Singly linked list append: if list not empty

//append after tail tail->next = newNode; tail = newNode;

JAVA AList: What is in the if statement of the add method given newPosition?(Hint: the add method with two parameters)

//make room for(int i = length;i > newPosition;i--){ //move elements down list[ i ] =list[ i - 1 ] ; } // add newEntry into newPostion list[newPosition - 1] = newEntry; length++;

What value does ListSearch return if the search key is not found?

0 ** The algorithm returns 0 to indicate a matching node was not found.

C++ Doubly-linked list: two diffenreces from singly linked list

1. has another pointer that points to the the previos node Node* prev = NULL; 2. the else statement in append changed to tail->next = newNode; newNode->prev = tail tail = newNode;

Given numsList: 70, 82, 41, 120, 357, 66 after executing the following statements: ListRemove(numsList, node 82) ListRemove(numsList, node 357) ListRemove(numsList, node 66)

70, 41, 120

A reverse traversal does what ?

A reverse traversal visits all nodes starting with the list's tail node and ending after visiting the list's head node

The ListSearch starts at what part of the list?

Head of the list

given nodes [head] ->[85] -> [2]->[13]-> [64]->0 ListTraverseReverse visits which node second?

Node 13

C++ LinkedList: write c++ statement that pointer to traverse the listset current to point to the firstNode node in the list

Node* currentNode = firstNode;

Make a pointer to the firstNode node of the list C++

Node* firstNode;

Make a pointer to the lastNode node of the list C++

Node* lastNode;

create the new node in C++

Node* newNode = new Node;

C++ Singly Linked List: make new Node and place entry into newNode and make the next blank

Node* newNode = newNode; newNode->data = newEntry; newNode->next = NULL

C++ Singly Linked List: print function

Node* temp = head; while(temp != NULL){ if(temp->next= NULL) cout << temp->data <<; else cout << temp->data<< ","; temp = temp->next }

ListTraverse can be used to traverse a doubly-linked list.(True or False)

True ***The algorithm can traverse a singly- or doubly-linked list, since in either case, each list node has a next pointer.

ListTraverse Reverse can be used to traverse a singly-linked list.

True ***Reversal traversal cannot be used for a singly-linked list, because singly-linked list nodes do not contain a pointer to the previous node.

A doubly-linked list is what?

a data structure for implementing a list ADT, where each node has data, a pointer to the next node, and a pointer to the previous node.

search algorithm does what

a search algorithm returns the first node whose data matches that key, or returns 0 if a matching node was not found. A simple linked list search algorithm checks the current node (initially the list's head node), returning that node if a match, else pointing the current node to the next node and repeating. If the pointer to current node is 0, the algorithm returns 0

append

add to end

C++ ArrayList: write out whole add function

bool add(int newEntry){ isSuccessful = true; if(!isFull()){ list[length] = newEntry; lenght++; } else{ isSuccessful = false; } return isSuccessful; }

C++ LinkedList: In the display function writ out the while statement that loops as long that currentNode != NULL

cout << currentNode ->data << endl; currentNode = currentNode->next;

JAVA LList: write out the System.out statement.

currentNode = currentNode.next

C++ Doubly Linked-List: in the remove function if the Node is found write out else if statement

else if (temp->data == num) { temp->previous->next = temp-> next; temp->next->previos = temp-> previous; }

C++ Doubly Linked-List: in the remove function if the Node is at the end of the list write out else if statement

else if((temp->data == num ) &&(temp == Tail){ temp->previous->next = NULL; Tail = temp->previous; }

C++ Singly Linked List: A for loop that append 1 - 10 to the array

for (int i = 1; i<=10; i++){ list->append(i); }

JAVA AList: write out the for loop condition and statement for the contains method

for(index = 0; !found && (index < length);length++){ if(anEntry == list[index]){ found = true; } }

C++ ArrayList: move element down code in add method given newPosition and newEntry

for(int i = length; i> newPosition;i--){ list[ i ] = list [ i - 1] }

JAVA Array Based List: A for loop that append 1 - 10 to the array

for(int i =1; i<=10;i++){ list.append(i); }

JAVA Array Based List: print function

for(int index = 0; index < length; index++) System.out.println(list[index]);

Given numList is: 5, 8, 2, 1. ListTraverse(numsList) visits _____ node(s).

four A list traversal visits all nodes. ListTraverse points curNode to the list's head node, and then repeatedly points curNode to curNode's next node, until curNode is 0.

C++ Doubly Linked-List: in the remove function if the Node is in the beginning write out the if statement

if (temp->data == num && temp == Head) { Head->next = temp->next; temp->previous = Head; }

C++ Singly Linked List: if list is empty This works for both append and preappend as this is the first thing checked before going to the else statement

if head == NULL then head = newNode; tail = newNode;

JAVA AList: What is the if condition for the add method given newPosition?(Hint: the add method with two parameters)

if(!isFull() && (newPosition >=1) && (newPosition <=length +1) pseudo code : if not full and newPostion is between 1 and length +1

JAVA AList: What is the outer if condtion for the remove method ?

if((givenPosition >= 1) && (givenPosition <= length)) pseudo code: if givenPosition is between 1 and length go on

JAVA AList: In the replace method what is in the if condition

if((givenPosition >= 1) && (givenPosition <=length)) pseudo code: if givenPosition is in between 1 and length

C++ Doubly-linked List: In the insert function if the list is empty write out the condition and the if statement

if(Head -> next == NULL){ newNode->previous = Head; Head->next = newNode; Tail = newNode; }

C++ Doubly-Linked List: if appending an newEntry and and the list is empty write the C++ statements that sets the newNode

if(head ===NULL){ head = newNode; tail = newNode; }

the InsertAfter operation for a doubly-linked list does what?

inserts the new node after a provided existing list node.

A doubly-linked list is similar to a singly-linked list, but WHAT?

instead of using a single pointer to the next node in the list, each node has a pointer to the next and previous nodes.

How is doubly-linked list different to a singly-linked list

instead of using a single pointer to the next node in the list, each node has a pointer to the next and previous nodes. Such a list is called "doubly-linked" because each node has two pointers, or "links".

Make a variable to store the number of elements in the list C++

int length;

The Remove operation for a doubly-linked list does what?

removes a provided existing list node. curNode is a pointer to an existing list node.

JAVA AList: what is in the else statement of the add method?(This else statement is the same for the add method given newEntry or given newEntry and newPosition)

isSuccessful = false;

write the C++ staement that make lastNode point to the actual lastNode node

lastNode = newNode;

write the C++ staement that insert newNode after lastNode

lastNode->next = newNode;

C++ Singley-linked list: write the code that does remove(1); remove(4); remove(9); remove(10); print();

list->remove(1); list->remove(4); list->remove(9); list->remove(10); list->print();

JAVA AList: in the replace method what is in the if statement?

list[givenPosition - 1] = newEntry;

JAVA AList: The add method adds a new entry at the end of the list. Using an array based implementation for the list, if not full what java code

list[length] = new Entry; length++

JAVA Array Based List: Add the code that appends

list[length] = newEntry; length++;

JAVA Array Based List: append newEntry when called append

list[length] = newEntry; length++;

write the C++ staement that storez the new item in the node in C++

newNode->data = newEntry;

write the C++ staement that set the next field of newNode

newNode->next = NULL;

C++ Singly Linked List preappend: if list not empty

newNode->next = head; head = newNode;

C++ Doubly-Linked List: Write out the else C++ Doubly-Linked List: if prepend list and list is not empty

newNode-next = head; head-> prev = newNode; head= newNode;

JAVA AList: Whole add method

public boolean add(int newEntry){ boolean isSuccessful =true; if(!isFull()){ list[length] = newEntry; length++; } else{ isSuccessful = false; } return isSuccessful; }

JAVA AList: write the display method

public void display(){ for( int index = 0; index < length; index++ ){ System.out.println(list[index ]); } }

ListTraverse begins with _____.

the list's head node **To print all nodes, the traversal starts with the head node.

C++ LinkedList: in the deleteNode function if node found do what? write out the C++ statements

trailCurrent->next = currentNode; length--; if(lastNode == currentNode) lastNode = trailCurrent; delete currentNode;

A list traversal algorithm does what?

visits all nodes in the list once and performs an operation on each node. ****A common traversal operation prints all list nodes.

C++ LinkedList: write out the whole display function

void display(){ Node* currentNode = firstNode; while(currentNode != NULL ) { cout << currentNode ->data << endl; currentNode = currentNode->next;" } }


Related study sets

CCNA 4 chapter 6 Quality of Service

View Set

Reading Smartbook- Chapter 16 SOCI 1301

View Set

Biochemistry Exam #2 Section 2.2-2.4

View Set

The Art of Public Speaking: MIDTERM

View Set