Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#include using namespace std; struct node{ int data; node *next; }; class List{ private: int size; node *head; node *tail; public: List(){ head = NULL;
#includeusing namespace std; struct node{ int data; node *next; }; class List{ private: int size; node *head; node *tail; public: List(){ head = NULL; //The constructer will make them NULL to avoid any garbage value. tail = NULL; size=0; } void insert_start(int value){ node *temp = new node(); temp->data = value; temp->next = head; if (head==NULL){ tail=temp; } head = temp; size++; } void insert_end(int value){ node *temp = new node; temp->data = value; temp->next = NULL; if (head==NULL){ head = temp; tail = temp; temp=NULL; } else{ tail->next = temp; tail = temp; } size++; } //Solution for Ex#3 void insert_position(int value, int position){ if (position next; } pre->next=temp; temp->data=value; temp->next=cur; size++; } else if(position == size){ insert_end(value); } } void displayList (){ node *temp = new node; temp =head; while(temp!=NULL){ cout data next; } } int getsize(){ return size; } //Solution for Ex#3 void delete_position(int position){ node *cur = head; node *pre = new node; if (position next; } pre->next=cur->next; cur=NULL; size--; } else if (position==size){ for(int i=1;i next; } tail=pre; pre->next=NULL; delete cur; size--; } } //*****INSERT YOUR CODE HERE FOR HOMEWORK#2****** //*****Code for Homework#2 ****** bool isEmpty(){ //returns true if the list is empty; it returns false otherwise. //INSERT CODE HERE!!! } int front(){ //returns the data of the first element, if the list is empty return 0. //INSERT CODE HERE!!! } int back(){ //returns the data of the last element, if the list is empty return 0. //INSERT CODE HERE!!! } int findKth(int pos) { //returns the element at position pos. If the position pos is greater than the size of the list, return 0. //INSERT CODE HERE!!! } int find(int value) { //returns the position of the first occurrence of value, if the element cannot be found, return 0. //INSERT CODE HERE!!! } }; int main(int argc, const char * argv[]) { //DO NOT MODIFY THE FOLLOWING CODE List myList; //Check if myList is empty cout 20 -> 30 -> 40 -> 50 //Check again if myList is empty cout Task 2 - Implementing a Linked List 15 points For this task, you will need to complete the implementation of the following five member functions of Class List. The partial implementation of this class is provided in the same folder as this homework instructions. bool isEmpty returns true if the list is empty; it returns false otherwise. [2 points] int findKth (int pos) returns the element at position pos. If the position pos is greater than the size of the list, return 0. [5 points] int find (int value) returns the position of the first occurrence of value, if the element cannot be found, return 0. 4 points] int front returns the data of the first element, if the list is empty return 0. [2 points] int back returns the data of the last element, if the list is empty return 0. [2 points] The main0 function in the file instantiates a linked list (named myList) and invokes several functions of the linked list. After completing all the member functions above, your output should look like this when the program runs: myList is empty? true Size is :0 First element is :0 Last element is :0 myList is empty? false Size is :5 First element is :10 Last element is :50 myList is empty? false Size is :10 First element is :10 Last element is :100 100 is at position :10 10 is at position 1 60 is at position :6 120 is at position :0 Data at position 2 is :20
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started