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

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...
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...
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,...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev = NULL;    next = NULL; } DNode(string s, string a, int lenmin, int lensec){    song = Song(s,a,lenmin,lensec);    prev = NULL;    next = NULL; } for each node. Write the method: void moveUp(string t); This method moves a song up one in the playlist. For instance, if you had the following: Punching in a Dream, The Naked And Famous................3:58 Harder To...
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT