Question

In: Computer Science

Write a complete C program to do the following:(i) Define a function Nodeptr CreateDLL(char str[])which takes...

Write a complete C program to do the following:(i) Define a function Nodeptr CreateDLL(char str[])which takes a string as parameter and creates a Doubly Linked List of characters and returns the pointer to the first node. (ii) Define a function int IsPalindrome(Nodeptr first)to check whether the string represented by the above doubly linked list pointed to by first, is a palindrome or not and return 1/0 accordingly. Do not use any additional data structure.Write a main function to read a string and create Doubly Linked of characters and check whether the string is a palindrome using above functions. Assume the following structure definition:typedef struct NODE *Nodeptr;struct NODE {

char letter;

Nodeptr llink, rlink;

};

} ;

Solutions

Expert Solution

Complete C code for the question with two functions as described:

#include <stdio.h>
#include <stdlib.h>
#include<string.h>

//structure def
typedef struct NODE *Nodeptr;
struct NODE {
        char letter;
        Nodeptr llink, rlink;
};

/* protypes for functions defined after main */
Nodeptr CreateDLL(char *);
int IsPalindrome(Nodeptr);

//function to create double linked list
Nodeptr CreateDLL(char str[])
{
        int n = strlen(str);
        Nodeptr first = NULL;
        for (int i = 0; i < n; i++)
        {
                Nodeptr new_node = (struct NODE*)malloc(sizeof(struct NODE));

                Nodeptr last = first;
                new_node->letter = str[i];

                new_node->rlink = NULL;

                //if first node is NULL
                if (first == NULL) {
                        new_node->llink = NULL;
                        first = new_node;
                        continue;
                }

                //to get the last node to insert the new char at the end
                while (last->rlink != NULL)
                        last = last->rlink;

                last->rlink = new_node;

                new_node->llink = last;
        }

        return first;
}

//function to check palindrome
int IsPalindrome(Nodeptr first)
{
        Nodeptr last = first;
        while (last->rlink != NULL)
                last = last->rlink;

        while (first != last)
        {
                //return 0 if any character is mismatched
                if (first->letter != last->letter)
                        return 0;

                first = first->rlink;
                if (first == last)
                        break;
                last = last->llink;
        }

        return 1;
}



int main() {
        char str[1000];
        printf("Enter the string to be checked\n");
        scanf("%[^\n]%*c", str);

        Nodeptr first = CreateDLL(str);
        int val = IsPalindrome(first);
        if (val == 1)
                printf("Palindrome.\n");
        else
                printf("Not a plaindrome.\n");

        return 0;
}


EXPLANATION:

In the first function createDLL i have interated through the string and for each character, added it as a node to the end of the double linkedlist and returned the pointer to the first node.

In the second function Ispalindrome i have first extracted the pointer to the last node through while loop after that i have taken the first and the last node and simultaneously checked whether both the letters are same or not if not then return 0 from there onwards else continue untill both last and first become equal, atlast return 1 as we know all the characters are equal if it didn't return 0 earlier.

In the main function i have asked for the string input passed to string to createDLL then passed the first pointer to Ispaplindrome then based on val =1/0 returned printed if it is palindrome or not.

NOTE: If there is any kinf of doubt or problem regarding the code or its explanation please let me know in the comment section and i will resolve that asap and kindly UPVOTE !!


Related Solutions

[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes the character x from string s. For s[] = “America”, a call to deleteSymbol(s, ‘a’) converts s[] = “Ame”
c++ Write the definition of a function named ‘isLower’ that takes as input a char value...
c++ Write the definition of a function named ‘isLower’ that takes as input a char value and returns true if the character is lowercase; otherwise, it returns false.•Print the message “The character xis lowercase” when returned value above is true, and vice versa.
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds the string 'f' in the string 'b' and replaces it with the string 't'. You can assume that f and t same length Example: char string[] = "zap";     replace(string, "ap", "oo"); --changes 'string' to "zoo".     *don't assume substring being replaced is singular, or that its own substrings are unique.     *Don't re scan letters already checked and replaced         char string[] =...
I have this program in C that takes three char arrays that each have a first...
I have this program in C that takes three char arrays that each have a first and last name. I have two functions that reverese the name and change it to all upper case. I have the program completeed but need to change both functions to use pointers instead of arrays. I will bold the functions I need to use pointers. #include <stdio.h> void upper_string(char []); int main() { char name1[100]="John Smith"; char name2[100]="Mary Cohen"; char name3[100]="Carl Williams"; upper_string(name1);// calling...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates to string s with all occurrences of c removed. The string c is guaranteed to be a length-1 string; in other words a single character string. For example (remove-char "abc" "b") should evaluate to "ac". Here is pseudocode that you could implement.
Write a complete C++ program that prompts the user for and takes as input, numbers until...
Write a complete C++ program that prompts the user for and takes as input, numbers until the user types in a negative number. the program should add all of the numbers together. Then if the result is less than 20 the program should multiply the result by 3, otherwise subtract 2 from the result. Finally, the program should printout the result.
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both.
Write a complete C++ program that at least consists of the main() function and at least...
Write a complete C++ program that at least consists of the main() function and at least two recursive functions. The first function has no return value and can be named printPrime(). It prints first n prime numbers with proper prompt. Note that number 1 is not regarded as a prime number. We assume the first prime number is 2. The printout should start from 2. The prototype of the recursive function should be void printPrime(int n); The algorithm of printPrime()...
Write a complete program in java that will do the following:
Write a complete program in java that will do the following:Sports:             Baseball, Basketball, Football, Hockey, Volleyball, WaterpoloPlayers:           9, 5, 11, 6, 6, 7Store the data in appropriate arraysProvide an output of sports and player numbers. See below:Baseball          9 players.Basketball       5 players.Football           11 players.Hockey            6 players.Volleyball        6 players.Waterpolo       7 players.Use Scanner to provide the number of friends you have for a team sport.Provide an output of suggested sports for your group of friends. If your...
Can someone do this python program? Write a function that takes a number as a parameter...
Can someone do this python program? Write a function that takes a number as a parameter and then prints out all of the factors for that number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT