Question

1. Build a double linked list with 5 in the first node following the instructions: Insert...

1. Build a double linked list with 5 in the first node following the instructions: Insert a node with 3 at the top of the list Insert a node with 10 at the bottom of the list Insert a node with 7 between nodes with 5 and 10 2. Start deleting nodes from the list in the following order; Delete the node with 7 Delete the node with 3 Delete the node with 10 3. Print the resulting list forward and backwards. write your statements in C.

Homework Answers

Answer #1

code: doublyLinkedList.c

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

typedef struct node{
        int data;
        struct node *next, *prev;
}Node;

void displayList(Node *head){
        Node *p = head, *r, *q;
        r = q = p;
        printf("Forward->\n");
        while(p){
                printf("%d, ",p->data);
                r=p;
                p = p->next;
        }
        printf("\n<-Backward\n");
        while(r){
                printf("%d, ", r->data);
                r= r->prev;
        }
        printf("\n");
}

Node * createNode(int data){
        Node * p = (Node *)malloc(sizeof(Node));
        p->data = data;
        p->next = NULL;
        p->prev = NULL; 
        return p;
}

Node * addNodeToList(Node *head, int data){
        Node *p, *q, *r;
        q = createNode(data);
        if(head==NULL){
                head = q;
                return head;
        }
        if(head->next==NULL){
                if(head->data > data){
                        q->next = head;
                        q->next->prev = q;
                        head = q;
                }
                else{
                        head->next = q;
                        q->prev = head;
                }
                return head;
        }
        p = head;
        while(p && p->data < data){
                r = p;
                p = p->next;
        }
        if(p==NULL){
                r->next = q;
                q->prev = r;
        }
        else{
                r->next = q;
                q->next = p;
                q->prev = r;
                p->prev = q;
        }
        return head;
}

Node * deleteNodeFromList(Node *head, int key){
        Node *p, *r;
        if(head==NULL){
                return head;
        }
        if(head->next==NULL && head->data==key){
                p = head;
                free(p);
                head = NULL;
                return head;
        }
        if(head->data==key && head->next!=NULL){
                p = head;
                head = p->next;
                head->prev = NULL;
                free(p);
                return head;
        }
        p = head;
        while(p && p->data != key){
                r = p;
                p = p->next;
        }
        if(p==NULL){
                return ;
        }
        else{
                if(p->next==NULL){
                        r->next = NULL;
                }
                else{
                        p->next->prev = r;
                        r->next = p->next;        
                }
                free(p);
        }
        return head;
}


int main(void){
        Node *list = NULL;
        list = addNodeToList(list, 5);  
        list = addNodeToList(list, 3);
        list = addNodeToList(list, 10);
        list = addNodeToList(list, 7);
        list = deleteNodeFromList(list, 7);
        list = deleteNodeFromList(list, 3);
        list = deleteNodeFromList(list, 10);
        displayList(list);
        return 0;
}

output:

Know the answer?
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
Similar Questions
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists In this lab, you will create simple single linked structures consisting of Node objects. Each node will have a pointer to the next node. You will use a head pointer to keep track of the first node in the linked list, and a tail pointer to keep track of the last node in the linked list. Set both head and tail to NULL when...
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
2- Given following data structures of a double linked list :    class ListNode      {...
2- Given following data structures of a double linked list :    class ListNode      {         String info ;         ListNode prev ;        ListNode next ;           …….      }    Write a Java method which prints the content of double linked list in reverse order.
Implement a singly linked list having all unique elements with the following operations.I 0 x –...
Implement a singly linked list having all unique elements with the following operations.I 0 x – Inserts element x at the end. I 1 y x – If the element y exists, then insert element x after the element y, else insert element y before the existing element x. Assuming either the element x or the element y exists. I 2 z y x – Inserts element x in the middle of the elements z and y. The element z...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
Project 1 - NodeList write in c++ with 5 files: main.cpp List.h List.cpp ListNode.h ListNode.cpp Building...
Project 1 - NodeList write in c++ with 5 files: main.cpp List.h List.cpp ListNode.h ListNode.cpp Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3,...
   vi. Assume that a linked list stores the data, 20, 11, 13, 19, 12, 14...
   vi. Assume that a linked list stores the data, 20, 11, 13, 19, 12, 14 in that order. Assume that Node head references the first item in the list. What is the result to the linked list of       the following instructions? Assume that newNode          is a Node, already constructed. newNode.data = 1;                         newNode.next = head.next;                         head = newNode;       a. The value 1 is inserted into the linked list before 20       b. The...
What will be the final linked-list after executing the following method on the given input singly...
What will be the final linked-list after executing the following method on the given input singly linked-list? Consider that the singly linked-list does not have a tail reference. Input: 1->2->3->4->5->6->7->8->null                                                                                                  void method(list){ if(list.head == null) return; Node slow_ref = list.head; Node fast_ref = list.head; Node prevS = null; Node prevF = null; while(fast_ref != null && fast_ref.next != null){ prevS = slow_ref; slow_ref = slow_ref.next; prevF = fast_ref; fast_ref = fast_ref.next.next; } prevS.next = slow_ref.next; prevF.next.next = slow_ref;...
This is in java and you are not allowed to use Java API classes for queues,...
This is in java and you are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own implementations for them. You should construct a BST by inserting node values starting with a null tree. You can re-use the code for the insert method given in the sample code from the textbook. -insert method is provided below Your code should have a menu driven user interface at the command line with...
The main goal is to implement two recursive methods, each is built to manipulate linked data...
The main goal is to implement two recursive methods, each is built to manipulate linked data structures. To host these methods you also have to define two utterly simplified node classes. 1.) Add a class named BinaryNode to the project. This class supports the linked representation of binary trees. However, for the BinaryNode class Generic implementation not needed, the nodes will store integer values The standard methods will not be needed in this exercise except the constructor 2.) Add a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT