1. Engineering
  2. Computer Science
  3. c program with linked list you just need to complete...

Question: c program with linked list you just need to complete...

Question details

c program with linked list

You just need to complete the ******implement the deleteSpecific function******

typedef struct node {
    int data; // the data
    struct node *next; // the next pointer

    struct node *previous; // the previous pointer
} Node;

Node* deleteSpecific(Node *listHead, int dataValue) {
    // assume data to delete always exist and is always unique in
    // this exercise
   
    printf("deleting %d from list \n", dataValue);
   
    // TODO: implement!
   
    return listHead;
}

///// Update version of insert with previous
Node* insert(Node* listHead, int dataToInsert) {
    // add the data in the end of linked list
    if (listHead == NULL) {
        // list is emtpy, let's insert our first element!
        printf("inserting first element with data %d\n",dataToInsert);
        Node *newNode = malloc(sizeof(Node));
        newNode->data = dataToInsert;
        newNode->next = NULL;
        newNode->previous = NULL;
        return newNode;
    } else {
        // list is not empty, let's insert at the end
        printf("inserting element with data %d\n",dataToInsert);
        Node *current = listHead; // CAUTION!
        while (current != NULL) {
            if (current->next == NULL) {
                // if it's end of list...
                Node *newNode = malloc(sizeof(Node));
                newNode->data = dataToInsert;
                newNode->next = NULL;
                current->next = newNode;
                newNode->previous = current;
                return listHead;
            } else {
                // otherwise...
                current = current->next;
            }
        }
    }

}

 Node* deleteFirst(Node* listHead) {     ///Q1

    printf("deleting first element from list: \n");

    struct node *current;

    current = listHead;

    listHead = listHead ->next;

    listHead ->previous =NULL;

    free(current);

    return listHead;

}



Node* deleteLast(Node* listHead) {      ///Q2

    printf("deleting last element from list: \n");



    struct node *current;

    current = listHead;

    while (current ->next !=NULL)

        current =current->next;

    current ->previous ->next = NULL;

    free(current);

    return listHead;

}

void printBackwardsFromZero(Node* listHead) {
    printf("printing list in backwards from the first 0 element (or end of list): \n");
    Node *current = listHead;
    while (current->data != 0) {
        current = current->next;
    }
    if(current->data == 0)
    {
        current = current->previous;
        while (current!=NULL)
        {
            printf("%d ",current->data);
            current = current->previous;
        }
    }

    printf("\n");
}

int main()
{
    Node *myHead = NULL; // head of the linked list, not really MY HEAD!

    myHead = insert(myHead, 2000);
    myHead = insert(myHead, 2018);
    printList(myHead);
    myHead = insert(myHead, 2001);
    myHead = insert(myHead, 1998);
    myHead = insert(myHead, 0);
    myHead = insert(myHead, 1999);
    myHead = insert(myHead, 2021);
    myHead = insert(myHead, 2020);
    printList(myHead);

    myHead = deleteFirst(myHead);
    printList(myHead);
   
    myHead = deleteLast(myHead);
    printList(myHead);
   
    myHead = deleteSpecific(myHead, 1998);
    printList(myHead);
    myHead = deleteSpecific(myHead, 2018);
    printList(myHead);
   
    printBackwardsFromZero(myHead);
   
    return 0;
}

Expected output:

2001

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution