Question

In: Computer Science

you will be implementing two functions for the LinkedList class. The function that you are implementing...

you will be implementing two functions for the LinkedList class. The function that you are implementing is called reverse and isPalindrome. The isPalindrome function should determine if the linked list is a palindrome or not and the reverse function should reverse the linked list.

When you are finished with the implementation, try to use valgrind to see if there are any other errors or memory leaks to deal with. valgrind ./lab7 or make val

  1. Expected Output

This is an example of what the output should look like. Note that the program must compile without any warnings or memory leaks.

==2353315== Memcheck, a memory error detector

==2353315== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.

==2353315== Using Valgrind-3.16.0 and LibVEX; rerun with -h for copyright info

==2353315== Command: ./lab7

==2353315==

The word: tenet is a palindrome.

The word: LOLO is not a palindrome.

The word: Zendaya is not a palindrome.

The word: kayak is a palindrome.

The word: step on no pets is a palindrome.

The word: I did did I is a palindrome.

The word: CMSC 202 is not a palindrome.

The word: wasitacatisaw is a palindrome.

==2353315==

==2353315== HEAP SUMMARY:

==2353315==     in use at exit: 0 bytes in 0 blocks

==2353315==   total heap usage: 158 allocs, 158 frees, 76,512 bytes allocated

==2353315==

==2353315== All heap blocks were freed -- no leaks are possible

==2353315==

==2353315== For lists of detected and suppressed errors, rerun with: -s

==2353315== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Solutions

Expert Solution

class LinkedList

{

class Node {

public:

int data;

Node* next;

Node(int x){

this->data=x;this->next=NULL;}

};

Node* reverse(Node*head)
{
Node* c= head;
Node* s= NULL;
Node* n=NULL;
while(c!= NULL)
{
s=c;
c=c->next;
s->next=n;
n=s;
}
return s;
}

//this method extracts the second half of list and reverses it.Then we compare first half with second half.

bool isPalindrome(Node *head)
{
if(!head || !head->next)
return true;
Node *slow=head,*fast=head;
Node *pre=NULL;
while(slow && fast && fast->next)
{
pre=slow;
slow=slow->next;
fast=fast->next->next;
}
if(fast==NULL)
{
pre->next=NULL;
Node *x=reverse(slow);
}
else
{
pre=slow->next;
slow->next=NULL;
Node *x=reverse(pre);
}
Node *temp=head;
while(temp && x)
{
if(temp->data!=x->data)
return false;
temp=temp->next;
x=x->next;
}
return true;
}

return s;

}

};

AS THERE ARE NO MEMORY ALLOCATIONS EXCEPT POINTERS,THERE ARE NO MEMORY LEAKS!!


Related Solutions

Create a concrete LinkedList class that extends the provided ALinkedList class. You will need to override...
Create a concrete LinkedList class that extends the provided ALinkedList class. You will need to override the extract()method in your class. You can use your main() method for testing your method (although you do not need to provide a main method). Recall that a list is an ordered collection of data X_0, X_1, X_2, ..., X_n-1 The extract(int start, int end) method removes all elements X_start, X_start_1, ..., X_end-1 from the list. It also returns all removed elements as a...
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class....
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class. Create a method to find a node with given value in a LinkedList. Return the value is this value exists in the LinkedList. Return null if not exists. Use these two examples to test your method. Example 1: Input: 1 -> 2 -> 3, and target value = 3 Output: 3 Example 2: Input: 1 -> 2 -> 3, and target value = 4...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse();...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse(); //reverses the order of elements in the linked list void insert(int value); private: struct Node{ int data; Node* next; Node* prev; }; Node* head; Node* tail; //Add your helper function here that recursively reverses the order of elements in the linked list }; Write the declaration of a helper function in the class provided above that recursively reverses the order of elements in the...
Laboratory Tasks public class LinkedList {              Node head;               class Node {    &nbsp
Laboratory Tasks public class LinkedList {              Node head;               class Node {                       int data;                     Node next;                     Node(int d) {                             data = d;                             next = null;                     }                 } } Complete the above java program by adding the following methods: Part1: isEmpty() checks if the linked list is empty or not. (return type is boolean) printList() prints all data in the linked list. (void method) insertFirst(int newData) add newData at the head of the linked list. (void method) insertLasL(int newData) add newData at...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
This LinkedListUtil class tests various usages of the LinkedList class. The single param is an array...
This LinkedListUtil class tests various usages of the LinkedList class. The single param is an array of string. You will create a linked list with the string elements and return the linked list with all but the 1st two elements removed. Java code Complete the following file: LinkedListUtil.java import java.util.LinkedList; import java.util.ListIterator; /** This LinkedListUtil class tests various usages of the LinkedList class */ public class LinkedListUtil { /** Constructs a LinkedListUtil. @param list is the initialized list */ public...
This LinkedListUtil class tests various usages of the LinkedList class. The single param is an array...
This LinkedListUtil class tests various usages of the LinkedList class. The single param is an array of string. You will create a linked list with the string elements and return the linked list with all but the 1st two elements removed. Note: In this question use a for or while loop instead of the suggested iterator. You should also ignore CodeCheck’s error message about a missing class (LinkedListUtil.class). Your code still needs to pass all test cases. EDIT: For clarifcation...
This LinkedListUtil class tests various usages of the LinkedList class. The single param is an array...
This LinkedListUtil class tests various usages of the LinkedList class. The single param is an array of string. You will create a linked list with the string elements and return the linked list with all but the 1st two elements removed. Note: In this question use a for or while loop instead of the suggested iterator. You should also ignore CodeCheck’s error message about a missing class (LinkedListUtil.class). Your code still needs to pass all test cases. EDIT: you want...
python 3.6: You will have two functions: Function 1: update(user_dictionary): This function will take a dictionary...
python 3.6: You will have two functions: Function 1: update(user_dictionary): This function will take a dictionary entry as user that has keys, ‘username’,’password’,’uid’, and ‘gid’ along with values. You will search if the username is already in your user_list that you get from your userfile.json file that you have created in your previous task. If the username in your user_dictionary is not in the list that is in your .json file then add the user_dictionary into the existing list and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT