Question

(WRITE THIS PROGRAM IN C LANGUAGE).Before I ask the question, i'm just letting you know that...

(WRITE THIS PROGRAM IN C LANGUAGE).Before I ask the question, i'm just letting you know that I asked this question before but I unfortunately the answer was wrong, in fact, it didn't even answer my questions. So please make an effort to answer this question.

In this question, we will learn about a variant of linked list called \doubly Linked List". In addition to the \next" pointer pointing to the next node in the list, a node in a doubly linked list also contains a pointer \prev" pointing to the previous node in the list. One implementation of the doubly linked List is as follows. #include #include // Node d e f i n i t i o n f o r doubly l i n k e d l i s t typedef struct d l l n o d e { int data ; struct d l l n o d e * next ; struct d l l n o d e * prev ; } DLLnode t ; // De f ini t i on of doubly l i n k e d l i s t typedef struct { DLLnode t * head ; } DLL t ; // Creates a doubly l i n k e d l i s t DLL t * DLLCreate ( ) { DLL t * r e t = mal loc ( sizeof (DLL t ) ) ; r e t->head = NULL; return r e t ; } // Appends a DLLnode t containing the value x int o a DLL t void DLLAppend(DLL t * i n t l i s t , int x ) f // Create a DLLnode t DLLnode t * newNode = mal loc ( s izeof (DLLnode t ) ) ; newNode->data = x ; newNode->prev = NULL; newNode->next = NULL; // Point head to new node if list is empty i f ( i n t l i s t ->head == NULL) { i n t l i s t ->head = newNode ; return ; } DLLnode t * temp =i n t l i s t ->head ; while ( temp->next != NULL) { temp = temp->next ; // Go To last Node } temp->next = newNode ; newNode->prev = temp ; } // Prints the elements of a doubly l i n k e d l i s t void DLLPrint (DLL t * i n t l i s t ) f DLLnode t * temp = i n t l i s t ->head ; while ( temp != NULL) { p r i n t f ( "%d " , temp->data ) ; temp = temp->next ; } p r i n t f ( "\n" ) ; } Part a) Based on the doubly linked list implementation given above, implement a func- tion called \DLLReverse" to reverse a list. For example, if a doubly linked list originally contains the list f4, 5, 7, 7, 9g, after calling DLLReverse, the list would become f9, 7, 7, 5, 4g. The function prototype is given below. // Reverses a DLL t void DLLReverse (DLL t * i n t l i s t ) f } Part b) Implement a function called \DLLRemove" to remove any node at a specied index \ind". Just as with C++ arrays, the rst element of the list should have an index of 0. Starting at 0, indices go up to one less than the length of the list. Your DLLRemove function should give the following message if the user passes in an invalid index: \Warning: Invalid index!" No other operations should be done if the index is invalid. The function prototype is given below. // Removes the element at index ind f o r a DLL t , and // warns the user i f the index i s i n v a l i d void DLLRemove(DLL t * i n t l i s t , int ind ) {

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>
#include <stdlib.h>

//Node definition for doublylinkedlist 
typedef struct dllnode 
{
    int data; 
    struct dllnode *next;
    struct dllnode *prev;
} DLLnodet;

//Definition of doublylinkedlist
typedef struct{
    DLLnodet* head;
}DLLt;

//Creates a doublylinkedlist
DLLt* DLLCreate(){
    DLLt *ret=malloc(sizeof(DLLt)); 
    ret->head=NULL;
    return ret;
}

//Appends a DLLnodet containing the value x into a DLLt
void DLLAppend(DLLt *intlist, int x) {
    //Create a DLLnodet
    DLLnodet*newNode=malloc(sizeof(DLLnodet));
    newNode->data=x;
    newNode->prev=NULL;
    newNode->next=NULL;

    //Point head to new node if list is empty
    if(intlist->head==NULL){
        intlist->head=newNode;
        return;
    }

    DLLnodet*temp=intlist->head;
    while(temp->next!=NULL){
        temp=temp->next;
        //Go To lastNode
    }

    temp->next=newNode;
    newNode->prev=temp;
}

//Prints the elements of a doublylinkedlist
void DLLPrint(DLLt* intlist){
    DLLnodet*temp=intlist->head;
    while(temp!=NULL){
        printf("%d ",temp->data);
        temp=temp->next;
    }
    printf("\n");
}

void DLLReverse(DLLt *intlist) {
    DLLnodet* current = intlist->head;
    DLLnodet* prev = NULL;

    while(current != NULL) {
        DLLnodet *tmp = current->next;
        if(prev != NULL) {
            prev->prev = current;
        }
        current->next = prev;
        prev = current;
        current = tmp;
    }
    // prev now points to last node, which should become head now.
    intlist->head = prev;
    prev->prev = NULL;
}

void DLLRemove(DLLt *intlist, int ind) {
    if(intlist == NULL) {
        return;
    }
    if(ind == 0) {
        DLLnodet* t = intlist->head;
        intlist->head = t->next;

        if(intlist->head != NULL) {
            intlist->head->prev = NULL;
        }
        free(t);
        return;
    } else {
        int i = 1;
        DLLnodet *prev = intlist->head;
        DLLnodet *current = intlist->head->next;

        while((current != NULL) && (ind != i)) {
            current = current->next;
            i++;
        }

        if(current != NULL) {
            DLLnodet *p = current->prev;
            DLLnodet *n = current->next;

            if(p != NULL) {
                p->next = n;
            }
            if(n != NULL) {
                n->prev = p;
            }
            free(current);
        } else {
printf("Invalid Index");
}
    }
}

int main() {
    DLLt* list = DLLCreate();
    DLLAppend(list, 1);
    DLLAppend(list, 2);
    DLLAppend(list, 3);
    DLLAppend(list, 4);
    DLLAppend(list, 5);

    DLLPrint(list);

    DLLReverse(list);
    DLLPrint(list);

    DLLRemove(list, 4);
    DLLPrint(list);

    DLLRemove(list, 0);
    DLLPrint(list);
}


Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

a main.c 102 103 104 105 106 107 108 109 110 Files saved https://AfraidHoneydewMacroinstructio if(p != NULL) { p->next clang verston 6.0.0-1ubuntu2 (tags/R 1 2 3 4 5 5 4 3 2 1 5 4 3 2 4 3 2 main.c = n; != NULL) { n->prev if(n p; = free(current); 112 113 114 int main() [ 115 116 DLLt" list -DLLCreate); DLLAppend(list, 1); DLLAppend (list, 2); DLLAppend(list, 3); DLLAppend(list, 4); DLLAppend (list, 5); 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 DLLPrint(list); DLLReverse(list); DLLPrint (list); DLLRemove(list, 4) DLLPrint (list); DLLRemove(list, ) DLLPrint (list);

Add a comment
Know the answer?
Add Answer to:
(WRITE THIS PROGRAM IN C LANGUAGE).Before I ask the question, i'm just letting you know that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In java Write a method public void printReverse() that prints the elements of a doubly linked...

    In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...

  • In java Write a method public void printReverse() that prints the elements of a doubly linked...

    In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • C LANGUAGE I just need the void push() function, which inserts new node to the front...

    C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

  • This is a c programming problem. Would you please help me to write this problem??? I...

    This is a c programming problem. Would you please help me to write this problem??? I really appreciate it if you add comments for explanation step by step. Thank you. Reverse a Doubly linked list using recursion: Given a doubly linked list. Reverse it using recursion. Original Doubly linked list: next pointer - DDHIHI Null prev painter Reversed Doubly linked list: next pointer Start Pointer Null prev pointer Include: a) A struct for a node of the doubly linked list....

  • I'm having trouble getting this program to compile and run. It is in C++ Language and...

    I'm having trouble getting this program to compile and run. It is in C++ Language and being run with CodeBlocks. Problem Statement: create and manage a linked list. Program will loop displaying a menu of user operations concerning the management of a linked list. Included will be:       H create link at head       R remove link at head       T create link at tail       K remove link at tail       I remove link at ID          S search...

  • Improve the speed of public T get(int i) by having it work backward from the end...

    Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> {    private static class Node<T> {        public Node<T> prev, next;...

  • This program uses C++. This program reads in a line from the user and prints it...

    This program uses C++. This program reads in a line from the user and prints it out in a certain format. An example would be Input: 1 2 3 4 5 would result Output: [{1}, {2}, {3}, {4}, {5}]. When quotations marks are added into the input the format becomes different. For instance, Input 1 2 "3 4 5" would result in [{1}, {2}, {3 4 5}]. When I ad multiple quotation marks into the input, it will only use...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT