Question

In: Computer Science

6.9 Lab: Singly-Linked Lists As an entry-level programmer you have to be able to read, understand...

6.9 Lab: Singly-Linked Lists

As an entry-level programmer you have to be able to read, understand existing code and update it (add new features). One of this assignment’s goals is to read about 400 lines of code in five files, compile and run the program, understand it, and change it as required.

Download and review the following files (read code and all comments carefully):

  • College.h
  • College.cpp
  • LinkedList.h
  • LinkedList.cpp
  • main.cpp

---------------------------------------------------------

//colleges.txt

3 SBCC Santa Barbara City College; 18524
97 ZZC ZZ College; 9997
5 PCC Pasadena City College; 17666
7 NVC Napa Valley College; 18920
15 PVC Palo Verde College; 18266
4 DVC Diablo Valley College; 20579
6 FC Foothill College; 19302
12 CS College of the Siskiyous; 21936
99 CPC Cupertino College; 9999
10 CC Cuesta College; 19135
8 OC Ohlone College; 15878
98 ABC AB College; 9998
1 DAC De Anza College; 19302
9 IVC Irvine Valley College; 20577

--------------------------------------------------------------------------------

//LinkedList.cpp

#include <iostream>

#include "LinkedList.h"

using namespace std;

//**************************************************

// Constructor

// This function allocates and initializes a sentinel node

// A sentinel (or dummy) node is an extra node added before the first data record.

// This convention simplifies and accelerates some list-manipulation algorithms,

// by making sure that all links can be safely dereferenced and that every list

// (even one that contains no data elements) always has a "first" node.

//**************************************************

LinkedList::LinkedList()

{

head = new Node; // head points to the sentinel node

head->next = NULL;

length = 0;

}

//**************************************************

// The insertNode function inserts a new node in a

// sorted linked list

//**************************************************

void LinkedList::insertNode(College dataIn)

{

Node *newNode; // A new node

Node *pCur; // To traverse the list

Node *pPre; // The previous node

  

// Allocate a new node and store num there.

newNode = new Node;

newNode->college = dataIn;

// Initialize pointers

pPre = head;

pCur = head->next;

// Find location: skip all nodes whose code is less than dataIn's code

while (pCur && newNode->college.getCode() > pCur->college.getCode())

{

pPre = pCur;

pCur = pCur->next;

}

  

// Insert the new node between pPre and pCur

pPre->next = newNode;

newNode->next = pCur;

  

// Update the counter

length++;

}

//**************************************************

// The deleteNode function searches for a node

// in a sorted linked list; if found, the node is

// deleted from the list and from memory.

//**************************************************

bool LinkedList::deleteNode(string target)

{

Node *pCur; // To traverse the list

Node *pPre; // To point to the previous node

bool deleted = false;

  

// Initialize pointers

pPre = head;

pCur = head->next;

// Find node containing the target: Skip all nodes whose gpa is less than the target

while (pCur != NULL && pCur->college.getCode() < target)

{

pPre = pCur;

pCur = pCur->next;

}

  

// If found, delte the node

if (pCur && pCur->college.getCode() == target)

{

pPre->next = pCur->next;

delete pCur;

deleted = true;

length--;

}

return deleted;

}

//**************************************************

// displayList shows the value

// stored in each node of the linked list

// pointed to by head, except the sentinel node

//**************************************************

void LinkedList::displayList() const

{

   Node *pCur; // To move through the list

   // Position pCur: skip the head of the list.

   pCur = head->next;

   // While pCur points to a node, traverse the list.

   while (pCur)

   {

   // Display the value in this node.

   pCur->college.hDdisplay();

   // Move to the next node.

   pCur = pCur->next;

}

cout << endl;

}

//**************************************************

// The searchList function looks for a target college

// in the sorted linked list: if found, returns true

// and copies the data in that node to the output parameter

//**************************************************

bool LinkedList::searchList(string target, College &dataOut) const

{

bool found = false; // assume target not found

Node *pCur; // To move through the list

  

/* Write your code here */

  

return found;

}

//**************************************************

// Destructor

// This function deletes every node in the list.

//**************************************************

LinkedList::~LinkedList()

{

Node *pCur; // To traverse the list

Node *pNext; // To hold the address of the next node

  

// Position nodePtr: skip the head of the list

pCur = head->next;

// While pCur is not at the end of the list...

while(pCur != NULL)

{

// Save a pointer to the next node.

pNext = pCur->next;

  

// Delete the current node.

delete pCur;

  

   // Position pCur at the next node.

pCur = pNext;

}

  

delete head; // delete the sentinel node

}

--------------------------------------------------------------------------------------

//LinkedList.h

#ifndef LINKED_LIST_H

#define LINKED_LIST_H

#include "College.h"

class LinkedList

{

private:

struct Node

{

College college;

Node *next;

};

Node *head;

int length;

public:

LinkedList(); // constructor

~LinkedList(); // destructor

// Linked list operations

int getLength() const {return length;}

void insertNode(College);

bool deleteNode(string);

void displayList() const;

bool searchList(string, College &) const;

};

#endif

Solutions

Expert Solution

As nothing is mentioned, I'm assuming you need the searchList function done, as it's left incomplete.

Code:

bool LinkedList::searchList(string target, College &dataOut) const
{
    bool found = false;  // assume target not found
    ListNode *pCur; //To move through the list

    pCur = head->next;
    //Loop throught the list until we get the target
    while (pCur && pCur->dataOut.getCode() != target)
        pCur = pCur->next;
    if (pCur)
    {
        dataOut = pCur->dataOut;
        found = true;
    }
    return (found);
}

Screenshot:

Can't provide output as the main function is not provided.

Let me know if you need anything else.

-------------------------END---------------------

Please give a thumbs up(upvote) if you liked the answer.


Related Solutions

Data Structures Homework – Singly Linked Lists Create a singly linked that represents a school. The...
Data Structures Homework – Singly Linked Lists Create a singly linked that represents a school. The school has multiple classes. Each class has a different number of students. class student { Long ID; string Name; string Address; float grades[3]; student *below; }; class Node // the node represents a class in school { int ID; int NoOfStudents; int NoOfQuizes; student *t;// a linked list of students is allocated dynamically Node *Next; }; class school { string Name; Node *Head; int...
towers of hanoi c++ program using stacks and singly linked lists.
towers of hanoi c++ program using stacks and singly linked lists.
1. Considering singly linked list, be able to determine if inserting nodes at the end of...
1. Considering singly linked list, be able to determine if inserting nodes at the end of a LinkedList is less time-efficient than inserting them at the front of the list. 2. Be able to differentiate between the data structures Stack, Queue, and Linked List. 3. Determine between the acronyms LIFO and FIFO, and be able to give one real life example where each is applicable
(C++) All programs will be included! This lab gives you a little practice with linked lists...
(C++) All programs will be included! This lab gives you a little practice with linked lists ·Debug insertSorted() and implement sorted() in numberlist.cpp ·Hint: insertSorted() is just missing a few parts. What is in the function can work fine once you add the right code, though you are free to rewrite it ·You need to have main.cpp, numberlist.h and numberlist.cpp in this project, and if you are using Code::Blocks, remember to set the Build Options to indicate you want to...
Write a pseudocode function that interchanges two adjacent items of: (a) singly linked lists (b) doubly...
Write a pseudocode function that interchanges two adjacent items of: (a) singly linked lists (b) doubly linked lists if you can make it clean and short that be nice
Topic: Students will be able to create skills in the use of linked lists, the stack,...
Topic: Students will be able to create skills in the use of linked lists, the stack, and the queue abstract data types, by implementing solutions to fundamental data structures and associated problems. Add the code for the methods where it says to implement. The main class is already done. There is a sample of the output. 1. A double-ended queue, or deque, is a data structure consisting of a list of items on which the following operations are defined: addToBack(x):...
You are given a singly linked list. Write a function to find if the linked list...
You are given a singly linked list. Write a function to find if the linked list contains a cycle or not. A linked list may contain a cycle anywhere. A cycle means that some nodes are connected in the linked list. It doesn't necessarily mean that all nodes in the linked list have to be connected in a cycle starting and ending at the head. You may want to examine Floyd's Cycle Detection algorithm. /*This function returns true if given...
Is it important for a business owner to be able to read and understand their balance...
Is it important for a business owner to be able to read and understand their balance sheet, income statements and cash flow statement? Explain your answer using the bakery example business from question 1.
8. Assume you have a singly linked list with no tail pointer. Implement removeTail(). Raise an...
8. Assume you have a singly linked list with no tail pointer. Implement removeTail(). Raise an exception of the method is called on an empty list. template<typename Object> class LinkedList { private: class Node { Object data; Node* next; }; Node *head; public: LinkedList() : head(nullptr) {} Object removeTail(Object data); }; 9. What are iterators? What purpose do they serve? 10. What does it mean to invalidate an iterator? 11. Explain the difference between separate chaining and open addressing in...
1 Problem Description This lab will cover a new topic: linked lists. The basic idea of...
1 Problem Description This lab will cover a new topic: linked lists. The basic idea of a linked list is that an address or a pointer value to one object is stored within another object. By following the address or pointer value (also called a link), we may go from one object to another object. Understanding the pointer concept (which has be emphasized in previous labs and assignments) is crucial in understanding how a linked list operates. Your task will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT