Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS 2413 LAB -2 Linked List 1. file linkedlist.cpp that include the linkedlist.h

ID: 3751619 • Letter: C

Question

CS 2413 LAB -2 Linked List 1. file linkedlist.cpp that include the linkedlist.h and defines all the member functions. You may use the following code snippet to get started. Your program should define a separate main class called LinkedListTest.cpp to test each of the insert, deletion and search functions. Submit a Word or a PDF file with your code and output screenshots. Use appropriate comments wherever necessary 10 points] //linkedlist.h class Node public: int info; Node* next; class LinkedList public Node* head, *tail; LinkedList( //initialize head and tail to O or NULL LinkedList(); void insertAtHead(int value); void insertAtTail(int value); // Print the info value before deletion of the node void deleteAtHead(). void deleteAtTail(); void search(int value); void displayList();

Explanation / Answer


linkedlist.h
----------
#ifndef linkedlist_h
#define linkedlist_h
#include <iostream>
class Node
{
public:
int info;
Node* next;
};
class LinkedList
{
public:
Node *head, *tail;
LinkedList();
~LinkedList();
void insertAtHead(int value);
void insertAtTail(int value);
void deleteAtHead();
void deleteAtTail();
void search(int value);
void displayList();
};
#endif /* linkedlist_h */

linkedlist.cpp
------------

#include "linkedlist.h"
#include <iostream>
using namespace std;
LinkedList::LinkedList()
{
head = NULL;
tail = NULL;
}
LinkedList::~LinkedList()
{
Node *temp ;
while(head != NULL){
temp = head->next;
delete head;
head = temp;
}
}
void LinkedList::insertAtHead(int value)
{
Node *n = new Node;
n->info = value;
n->next = head;
head = n;
if(tail == NULL)
tail = n;
}
void LinkedList::insertAtTail(int value)
{
Node *n = new Node;
n->info = value;
n->next = NULL;
if(head == NULL)
head = tail = n;
else
{
tail->next = n;
tail = n;
}
}
void LinkedList::deleteAtHead()
{
cout << "delete at head->";
if(head == NULL)
cout << "list empty" << endl;
else
{
cout << "deleting " << head->info << endl;
Node *temp = head;
head = head->next;
if(head == NULL)
tail = NULL;
delete temp;
}
}
void LinkedList::deleteAtTail()
{
cout << "delete at tail->";
if(head == NULL)
cout << "list empty" << endl;
else
{
cout << "deleting " << tail->info << endl;
Node *prev = NULL, *curr = head;
while(curr != tail)
{
prev = curr;
curr = curr->next;
}
if(prev == NULL) //deleting the only single element in list
head = tail = NULL;
else
{
prev->next = NULL;
tail = prev;
}
delete curr;
}
}
void LinkedList::search(int value)
{
cout << "searching " << value << endl;
Node *curr = head;
while(curr != NULL)
{
if(curr->info == value)
break;
curr = curr->next;
}
if(curr == NULL)
cout << value << " NOT found in list" << endl;
else
cout << value << " found in list" << endl;
}
void LinkedList::displayList()
{
Node *curr = head;
cout << "list contains: ";
while(curr != NULL)
{
cout << curr->info << " ";
curr = curr->next;
}
cout << endl;
}

LinkedListTest.cpp
-----------------
#include <iostream>
#include "linkedlist.h"
using namespace std;
int main()
{
LinkedList llist;
cout << "inserting 1, 2, 3 at head" << endl;
llist.insertAtHead(1);
llist.insertAtHead(2);
llist.insertAtHead(3);
llist.displayList();
cout << "inserting 4, 5, 6 at tail" << endl;
llist.insertAtTail(4);
llist.insertAtTail(5);
llist.insertAtTail(6);
llist.displayList();
cout << "delete head" << endl;
llist.deleteAtHead();
cout << "delete tail" << endl;
llist.deleteAtTail();
llist.search(3); //not found
llist.search(1);
}

output
-----
inserting 1, 2, 3 at head
list contains: 3 2 1
inserting 4, 5, 6 at tail
list contains: 3 2 1 4 5 6
delete head
delete at head->deleting 3
delete tail
delete at tail->deleting 6
searching 3
3 NOT found in list
searching 1
1 found in list