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...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL....
Using C++ / provide code comments so I can understand. Create a simple linked list program...
Using C++ / provide code comments so I can understand. Create a simple linked list program to create a class list containing class node { void *info; node *next; public: node (void *v) {info = v; next = 0; } void put_next (node *n) {next = n;} node *get_next ( ) {return next;} void *get_info ( ) {return info;} }; Be able to initially fill the list. Provide functions to insert/append nodes and remove nodes from the linked list. Be...
1. Create a linked list using the Node class that contains the first million prime numbers...
1. Create a linked list using the Node class that contains the first million prime numbers (primes4.txt). It should have a menu with these options: 1 = Search for a Number (let the user enter a number to search for) 2 = Add a new Number (let the user enter a number and add it to the head of the list) 3 = Delete a Number (let the user enter a number and delete it if found) 4 = Exit...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT