Question

In: Computer Science

This is a C programming problem: Construct a doubly linked list: • Read non-zero integers from...

This is a C programming problem:

Construct a doubly linked list: • Read non-zero integers from the input and insert them into the linked list. • If an input integer is 0, the program should print all the integers in the list(from tail to head) and then exit.

Solutions

Expert Solution

CODE

// A complete working C program to demonstrate all

// insertion before a given node

#include <stdio.h>

#include <stdlib.h>

// A linked list node

struct Node {

  int data;

  struct Node* next;

  struct Node* prev;

};

/* Given a reference (pointer to pointer) to the head of a list

and an int, inserts a new node on the front of the list. */

void push(struct Node** head_ref, int new_data)

{

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

  new_node->data = new_data;

  new_node->next = (*head_ref);

  new_node->prev = NULL;

  if ((*head_ref) != NULL)

    (*head_ref)->prev = new_node;

  (*head_ref) = new_node;

}

/* Given a node as next_node, insert a new node before the given node */

void insertBefore(struct Node** head_ref, struct Node* next_node, int new_data)

{

  /*1. check if the given next_node is NULL */

  if (next_node == NULL) {

    printf("the given next node cannot be NULL");

    return;

  }

  /* 2. allocate new node */

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

  /* 3. put in the data */

  new_node->data = new_data;

  /* 4. Make prev of new node as prev of next_node */

  new_node->prev = next_node->prev;

  /* 5. Make the prev of next_node as new_node */

  next_node->prev = new_node;

  /* 6. Make next_node as next of new_node */

  new_node->next = next_node;

  /* 7. Change next of new_node's previous node */

  if (new_node->prev != NULL)

    new_node->prev->next = new_node;

  /* 8. If the prev of new_node is NULL, it will be

  the new head node */

  else

    (*head_ref) = new_node;

  

}

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

void printList(struct Node* node)

{

  struct Node* last;

  while (node != NULL) {

    last = node;

    node = node->next;

  }

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

  while (last != NULL) {

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

    last = last->prev;

  }

}

/* Driver program to test above functions*/

int main()

{

  /* Start with the empty list */

  struct Node* head = NULL;

  int num;

while(1) {

printf("enter the value to be inserted: ");

scanf("%d", &num);

if (num == 0) {

break;

}

push(&head, num);

}

  printf("Created DLL is: ");

  printList(head);

  getchar();

  return 0;

}


Related Solutions

Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
so the assigment is is a data strucutre using c++ to make a doubly linked list...
so the assigment is is a data strucutre using c++ to make a doubly linked list that will be able to do math mostly addition and multiplication, subratction and division is extra and would be nice. so the program is going to to open files and read them via a argumentmanager.h in a linux server try not to worry to much about this part just getting the program to work. i was able to complete part of the given code...
Given a linked list of integers, remove any nodes from the linked list that have values...
Given a linked list of integers, remove any nodes from the linked list that have values that have previously occurred in the linked list. Your function should return a reference to the head of the updated linked list. (In Python)
Write a C++ program to read in a list of 10 integers from the keyboard. Place...
Write a C++ program to read in a list of 10 integers from the keyboard. Place the even numbers into an array called even, the odd numbers into an array called odd, and the negative numbers into an array called negative. Keep track of the number of values read into each array. Print all three arrays after all the numbers have been read. Print only the valid elements (elements that have been assigned a value). a. Use main( ) as...
Develop a C++ "doubly" linked list class of your own that can hold a series of...
Develop a C++ "doubly" linked list class of your own that can hold a series of signed shorts Develop the following functionality: Develop a linked list node struct/class You can use it as a subclass like in the book (Class contained inside a class) You can use it as its own separate class Your choice Maintain a private pointer to a node class pointer (head) Constructor Initialize head pointer to null Destructor Make sure to properly delete every node in...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT