Question

This is a reminder that this is not a C++ or Java course. It is a...

This is a reminder that this is not a C++ or Java course. It is a Data Structures course. This means that you are to write your own code unless otherwise specified in the assignment. The use of built in data structures types like linked lists, stacks, queues, trees, maps, graphs etc. is prohibited and will result in a 60% reduction in your grade. Furthermore, the lecture material presents code that should be used to get you started. Any data structure that is not derived from what is shown in the lecture material will not be accepted. You will receive a grade of 0 for submitting it.

Task 1 - The Linked List

The linked list is at the core of most data structures. We see it in lists, stacks, queues and trees to name a few.

From the lecture material create a linked list class. This class should be generic so that it will operate on any given type. In C++ we call this a template class. In Java it is a Generic. Of course in Java a generic will not operate on a primitive type but that should be ok for what we are doing

Your class should have the following functionality:

  • void addToEnd(T data) - adds an item to the end of the list
  • void insert(T search, T data) - Adds data as a new Node following the node that contains search
  • deleteNode(T search) - Deletes the node that contains search.
  • void printList() - Prints the entire linked list.
  • bool contains(T search) - returns true if the list contains T and false if it doesn't

If you could use C++ and describe each function to help me understand I would appreciate it. Thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#include<iostream>

using namespace std;

//node class
template<class T>
class node
{
public:
T info;
node *next;
//constructor
node(T data)
{
info = data;
next = nullptr;
}
};

//LinkedList class
template<class T>
class LinkedList
{
private:
node<T> *head;
public:
//constructor
LinkedList()
{
head = nullptr;
}

//adds an item to the end of the list
void addToEnd(T data)
{
//create a new node
node<T> *newnode = new node<T>(data);

//check is the list empty
if(head==nullptr)
{
head = newnode;
return;
}

//traverse the list
node<T> *temp = head;
while(temp->next!=nullptr)
{
temp = temp->next;
}
//add the newnode at the end of the list
temp->next = newnode;
}

//Adds data as a new Node following the node that contains search
void insert(T search, T data)
{
//create a new node
node<T> *newnode = new node<T>(data);

//traverse the list to find the search
node<T> *temp = head;
while(temp!=nullptr && temp->info != search)
{
temp = temp->next;
}
//check if search is found
if(temp!=NULL && temp->info == search)
{
node<T> *post = temp->next;
//insert following the node that contains search
temp->next = newnode;
newnode->next = post;
}
}

//Deletes the node that contains search.
void deleteNode(T search)
{
node<T> *temp, *pre;
temp = head; pre = nullptr;
//check search contains first node
if(temp->info==search)
{
head = temp->next;
temp->next = nullptr;
delete temp;
return;
}
//traverse the list
while(temp!=nullptr && temp->info!= search)
{
pre = temp;
temp = temp->next;
}
//not found
if(temp==nullptr) return;

//delete the node contains search
pre->next = temp->next;
temp->next = nullptr;
delete temp;
}

//Prints the entire linked list.
void printList()
{
//check is the list empty
if(head==nullptr)
{
cout<<"EMPTY LIST";
return;
}
node<T> *temp = head;
//traverse the list
while(temp!=nullptr){
cout<<temp->info<<" ";
temp=temp->next;
}
cout<<endl;
}

//returns true if the list contains T and false if it doesn't
bool contains(T search)
{
node<T> *temp = head;
//search the list
while(temp!=nullptr){
//check the search value with the node value
if(temp->info==search)
return true;
temp=temp->next;
}
return false;
}
};


//main function
int main()
{
//create LinkedList object
LinkedList<int> list;

//insert some integers
list.addToEnd(5);
list.addToEnd(10);
list.addToEnd(15);
list.addToEnd(20);
list.addToEnd(25);

//print the list
cout<<"List after create: ";
list.printList();
//delete 15 from the list
list.deleteNode(15);
//print the list after deletion
cout<<"List after deletion: ";
list.printList();

return 0;
}

Output:

List after create: 5 10 15 20 25
List after deletion: 5 10 20 25

N.B. Whether you face any problem or need any modification then share with me in the comment section, I'll happy to help you.

Add a comment
Know the answer?
Add Answer to:
This is a reminder that this is not a C++ or Java course. It is a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Java You are given a Node class and a List class: public class Node {...

    Using Java You are given a Node class and a List class: public class Node {    int   data;     Node next; } public class List {     Node first; } You are also given a Stack class. The following functions are available for use: public class Stack { public boolean isEmpty(){}; public void push(int n){}; public int pop(){};} Write a Java method snglyRevToStck that pushes the data found in a linked list t in reverse order into the stack...

  • Data Structures and Algorithms (Java Programming) a) When devising an algorithm for linked lists, why must...

    Data Structures and Algorithms (Java Programming) a) When devising an algorithm for linked lists, why must you be careful about the order in which you change the references? b) What code would be needed to change the references in a linked list when moving up one node? c) Why did we have a previous reference in our linked list implementation? d) Write a class for a linked list node with just one constructor that allows the initialization of all instance...

  • Data Structures and Algorithms (Java Programming) a) When devising an algorithm for linked lists, why must...

    Data Structures and Algorithms (Java Programming) a) When devising an algorithm for linked lists, why must you be careful about the order in which you change the references? b) What code would be needed to change the references in a linked list when moving up one node? c) Why did we have a previous reference in our linked list implementation? d) Write a class for a linked list node with just one constructor that allows the initialization of all instance...

  • In Java The following Java implementation of a class Node is given: private class Node<Object> {...

    In Java The following Java implementation of a class Node is given: private class Node<Object> { Node() { this(null, null); } Node(Object d) { this(d, null); } Node(Object d, Node n) { data = d; next = n; } Object data; Node next; } Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a reference to the header node. Using the class Node described above, write a MySingleLinkedList...

  • Write a java code : Student ID at University is composed of year of admission and...

    Write a java code : Student ID at University is composed of year of admission and students number. we aim to implement a structure that improves operations of inserting and searching for a student. To enhance these operations, we will build a tree of linked lists (TreeOfLists) to combine advantages of both structures. Each node in this tree contains a linked list of all students who were admitted in that year. Each node in the linked list represents a student(id,...

  • 5. (34 pts) Stacks and Queues a. Stacks and queues are similar and different. For each of stack a...

    5. (34 pts) Stacks and Queues a. Stacks and queues are similar and different. For each of stack and queue, give a drawing showing how each would be stored in an array and a linked list. Use your first name (or usual nickname) as the data put into the container as an example for your pictures. Label the accessible element. (4 pictures; 16 pts) the text and lecture. (10 pts) You will not receive full credit without appropriate stack pictures....

  • CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...

    CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...

  • Generic Linked Lists ( Program should be written in Java language). Modify the linked list class...

    Generic Linked Lists ( Program should be written in Java language). Modify the linked list class presented in this chapter so it works with generic types. Add the following methods drawn from the java.util.List interface: -void clear(): remove all elements from the list. -E get(int index): return the element at position index in the list. -E set(int index, E element): replace the element at the specified position with the specified element and return the previous element. Test your generic linked...

  • Java programming course use java import. Follow instructions and comments to explain the code tha...

    java programming course use java import. Follow instructions and comments to explain the code thanks in advance Print L A polymomial can be represented as a linked list, where each node called a polyhede contoins the coefficient and the exponent of a term of the pelynomial For example, The pelynomial 4x-31-5 would be represented as the linked list 3 3xa Write a Polynomial class that has methods for creating a polhynomial, reading and writing a polynomial, and adding a pair...

  • Interfaces 1. What is inside an interface definition? What does a class do to an interface...

    Interfaces 1. What is inside an interface definition? What does a class do to an interface and what keyword is involved? How does a class do this to an interface (what should we find in the class)? Can an interface have a generic parameter? How do you instantiate an interface as an object? What methods can’t you use on an interface type? Abstract Data Types 2. What does an ADT define? Does an ADT specify programming language and/or data structures...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT