Hi, I use Eclipse for my Java code. I am trying to turn my old code and turn it
ID: 3560492 • Letter: H
Question
Hi, I use Eclipse for my Java code. I am trying to turn my old code and turn it into a singly linked circular list. My code will be below the description. If i could get help on how to turn it into a circular list.
Description: Make a class for a singly linked circular list that has no end and no beginning. The only access to the list is a single reference, current, that can point to any link on the list. This reference can move around the list as needed. Your list should handle insertion, searching, and deletion. You may find it convenient if these operations take place one link downstream of the link pointed to by current. (Because the upstream link is singly linked, you can
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/* Link list node */
struct node
{
char data;
struct node* next;
};
// Initial parameters to this function are &head and head
bool isPalindromeUtil(struct node **left, struct node *right)
{
/* stop recursion when right becomes NULL */
if (right == NULL)
return true;
/* If sub-list is not palindrome then no need to
check for current left and right, return false */
bool isp = isPalindromeUtil(left, right->next);
if (isp == false)
return false;
/* Check values at current left and right */
bool isp1 = (right->data == (*left)->data);
/* Move left to next node */
*left = (*left)->next;
return isp1;
}
// A wrapper over isPalindromeUtil()
bool isPalindrome(struct node *head)
{
isPalindromeUtil(&head, head);
}
/* Push a node to linked list. Note that this function
changes the head */
void push(struct node** head_ref, char new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to pochar to the new node */
(*head_ref) = new_node;
}
// A utility function to print a given linked list
void printList(struct node *ptr)
{
while (ptr != NULL)
{
printf("%c->", ptr->data);
ptr = ptr->next;
}
printf("NULL ");
}
/* Drier program to test above function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
char str[] = "abacaba";
int i;
for (i = 0; str[i] != ''; i++)
{
push(&head, str[i]);
printList(head);
isPalindrome(head)? printf("Is Palindrome "):
printf("Not Palindrome ");
}
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.