Question

Project 6: Create a double-linked list and traverse the list forward and backward. Document the code and make sure to include an explanation of how the link list works. If the student adds additional features, he/she should document that as well. In testing the list, show what happens in the case of the NULL list. Show why this type list is easier to search than a single-linked list.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note:

The code explained below deals with

1. Two types of insertions in a Double Linked List(In some places referred as D.L.L).

2. Forward and reverse traversal of a D.L.L

Solution:

In the images below a basic idea of a double linked list is provided, the code is provided at the end.

Double Linked List? A Doubledhas an extra pointey uhich points to the addres o the previots node G0, It is typically a ist of Nodes, each Node consisting et a data field, a Porda to next Node and a pofntn to preuto us Node Head Next Next Wext NULL NOLL pev peu pRev Insertion eh Nodes Let us co nsider hoo baste casu ob ingenHơn I. Node at beginig DeL.L 2. Node at end DLrL Nade ad begmuniry tHead NULL Head must point to neus node と NOL L r paevtou e the New node wode should point to nes node No te

Code:

//The code is explained for a test case, you can perform any number of insertions.

#include <stdio.h>

#include <stdlib.h>

  

// A Typical Double Linked List Node

struct Node {

int data;

struct Node* next;

struct Node* prev;

};

  

/*New node at the beginning*/

void push(struct Node** head, int data)

{

/* 1. allocate a new node */

struct Node* temp = (struct Node*)malloc(sizeof(struct Node));

  

/* 2. put in the data */

temp->data = data;

  

/* 3. Make next of new node as head and previous as NULL */

temp->next = (*head);

temp->prev = NULL;

  

/* 4. change prev of head node to new node */

if ((*head) != NULL)

(*head)->prev = temp;

  

/* 5. move the head to point to the new node */

(*head) = temp;

}

  

  

/* New node at the end */

void append(struct Node** head, int data)

{

/* 1. allocate node */

struct Node* temp = (struct Node*)malloc(sizeof(struct Node));

  

struct Node* last = *head; /* used in step 5*/

  

/* 2. put in the data */

temp->data = data;

  

/* 3. This new node is going to be the last node, so

make next of it as NULL*/

temp->next = NULL;

  

/* 4. If the Linked List is empty, then make the new

node as head */

if (*head == NULL) {

temp->prev = NULL;

*head = temp;

return;

}

  

/* 5. Else traverse till the last node */

while (last->next != NULL)

last = last->next;

  

/* 6. Change the next of last node */

last->next = temp;

  

/* 7. Make last node as previous of new node */

temp->prev = last;

  

return;

}

// This function prints contents of linked list starting from the given node

void printList(struct Node* node)

{

struct Node* last;

printf("\nTraversal in forward direction \n");

while (node != NULL) {

printf(" %d ", node->data);

last = node;

node = node->next;

}

//Since the pointer is now at the end we can go back using 'last'

printf("\nTraversal in reverse direction \n");

while (last != NULL) {

printf(" %d ", last->data);

last = last->prev;

}

}

/* Main Function to test the functionality*/

int main()

{

/* Start with the empty list */

struct Node* head = NULL;

// Insert 6.

append(&head, 6);

// Insert 7 at the beginning.

push(&head, 7);

// Insert 1 at the beginning.

push(&head, 1);

// Insert 4 at the end.

append(&head, 4);

//The list now is 1->7->6->4->NULL

printf("Created DLL is: ");

printList(head);

return 0;

}

Advantages over Single Linked List:

1. We can traverse a DLL in both directions.

2. We can insert a new node before a specific node easily.

3. The DELETE operation can be performed more efficiently.

Add a comment
Know the answer?
Add Answer to:
Project 6: Create a double-linked list and traverse the list forward and backward. Document the code...
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
  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for ...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • 1. Create a class MLL, a singly linked, non-circular list where each node only has one...

    1. Create a class MLL, a singly linked, non-circular list where each node only has one link next and the list has a head and a tail link (think about implementation of node). The MLL class also implements the Iterable interface. The following should be implemented as well: 2. Add the methods to the MLL class: a. iterator() to implement the Iterable interface. This method returns an instance of MLLI initialized appropriately. b. addFirst( value) that adds a value to...

  • In JAVA, please. THANK YOU In this project you will create a generic linked list using...

    In JAVA, please. THANK YOU In this project you will create a generic linked list using Java Generics. Description: Create a generic class called GenLinkedList. GenLinkedList will use nodes that store a value of the generic type to store its contents. It should have the following methods. The methods should all operate on the object making the call (none are static). Perform checking of the parameters and throw exceptions where appropriate. The linked list should be singly-linked. It should not...

  • Using Python. 3) Create a single-linked list (StudentList) where the data in the link is an object of type pointer to Student as defined below. Note that you need to store and pass a pointer of type...

    Using Python. 3) Create a single-linked list (StudentList) where the data in the link is an object of type pointer to Student as defined below. Note that you need to store and pass a pointer of type Student so that you do not create duplicate objects. Test your list with the provided program. You will find example code for a single-linked list of integers in Moodle that you can base your solution on. For your find methods, you can either...

  • Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation...

    Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...

  • Open BlueJ and create a new project (Project->New Project...). Create a new class named ListsDemo. Open the source code and delete all the boilerplate code. Type in the code to type (code to type...

    Open BlueJ and create a new project (Project->New Project...). Create a new class named ListsDemo. Open the source code and delete all the boilerplate code. Type in the code to type (code to type with bigger font) exactly as shown, filling in your name and the date in the places indicated. The code is provided as an image because you should type it in rather than copy-paste. If you need the code as text for accessibility, such as using a...

  • this is i have code for double linked list with insert at sorted list. i have...

    this is i have code for double linked list with insert at sorted list. i have some error that stdout: ------- Error: compilation stderr: ------- InsertDouble.c: In function ‘list* InsertDouble(LIST, int)’: InsertDouble.c:51:14: error: cannot convert ‘list’ to ‘list*’ in assignment Currentptr = *head; // set a pointer which is current one ^ it keep give me this error i am not sure how to fix is anyone possible to help me? #include <stdio.h> #include <stdlib.h> typedef struct list {   ...

  • ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE...

    ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE ANSWERING CODING SECTIONS HW07 #Q1-Q5 HW08 #Q1-Q2 // READ BEFORE YOU START: // Please read the given Word document for the project description with an illustrartive diagram. // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, standard, and a linked list of absents. // Please read the instructions...

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
ADVERTISEMENT