Question

In c++, write a function that prints every other element in a linked list. Assume that...

In c++, write a function that prints every other element in a linked list. Assume that it is a member function of a doubly linked list. This means you will need to use pointer operations.

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

A doubly linked list is a linked list in which navigation is possible in both forward and backward ways. Here "prev" link points to the "next" link of previous element and "next" link points to the "previous" link of next element and each link carries a data field to contain the data.

Please find the solution below. Here we will insert the data in a doubly linked list first then we will print the data inside the linked list.

#include <iostream>
using namespace std;
// doubly linked list node
struct Node {
int data; // to hold an integer value
struct Node *next; // Pointer to next node
struct Node* prev; // Pointer to previous node
};
struct Node* head = NULL;
void insert_operation(int new_data) { // insert function will insert the data at the beginning of the doubly linked list
struct Node* newnode = (struct Node*) malloc(sizeof(struct Node)); // allocating a block of uninitialized memory
newnode->data = new_data;
newnode->prev = NULL;
newnode->next = head;   
if(head != NULL) {
head->prev = newnode ;
}
head = newnode;
}   
void print() { // print() function will print the elements of the doubly linked list
struct Node* ptr;
ptr = head;
while(ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
int main() {
int len,j;
cout<< "How many integer you want to insert in the linked list ? ";
cin >> len ;
for (int i=0; i<len;i++){
cout<< "Enter the value : ";
cin >> j ;
insert_operation(j);
}
cout<<"The doubly linked list is:\n ";
print();
return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
In c++, write a function that prints every other element in a linked list. Assume that...
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
  • In C++ Assume entries in a linked list are of type struct listrec: struct listrec {...

    In C++ Assume entries in a linked list are of type struct listrec: struct listrec {             struct listrec    *prev; float                 value;             struct listrec    *next;   }; listrec *head, *tail; Write a main() routine in which the user is asked the number of nodes to create in the list (number greater than or equal to zero) then create the following type of linked list (use a loop to initialize list) based on the number of nodes requested: Write a...

  • Submit the pseudo code. Write a member function PrintReverse that prints the elements on a list...

    Submit the pseudo code. Write a member function PrintReverse that prints the elements on a list in reverse order. For instance, for the list X Y Z, list. PrintReverse() would output element in the list. You may assume that the list is not empty.

  • Draw a sketch of a singly linked list containing the following int values: 3, 1, 4,...

    Draw a sketch of a singly linked list containing the following int values: 3, 1, 4, 1, 5. The 3 should be at the front of the list. Remember to sketch the head pointer, each node, and each node's next pointer. 2. Write the code for the following new member function for our SinglyLinkedList class. You should write out the definition of the function, but do not need to write out all of the rest of the class. Your code...

  • This is a c programming problem. Would you please help me to write this problem??? I...

    This is a c programming problem. Would you please help me to write this problem??? I really appreciate it if you add comments for explanation step by step. Thank you. Reverse a Doubly linked list using recursion: Given a doubly linked list. Reverse it using recursion. Original Doubly linked list: next pointer - DDHIHI Null prev painter Reversed Doubly linked list: next pointer Start Pointer Null prev pointer Include: a) A struct for a node of the doubly linked list....

  • Assume entries in a linked list are of type struct listrec: struct listrec {             char...

    Assume entries in a linked list are of type struct listrec: struct listrec {             char                 value;             struct listrec    *next; }; Write a main() routine in which you create the following linked list: | a |   --|---->   | c |   --|-----> | w |   --|----> NULL Write a function called printlist that takes a pointer to the start of a linked list and prints out all the nodes in the list in sequence. The inferface of this function...

  • Linked list is a type of data structure. Each item of the list holds a data...

    Linked list is a type of data structure. Each item of the list holds a data and a pointer to the next item. Head is a pointer that points to the first element. Tail is the last element of the list, which points to "NULL". Linked list can be implemented using structures, consisting of some data types and a pointer. Write a program that creates the linked list given in the following figure and prints out the data in order....

  • Write a C++ function to add a node to the beginning of a linked list. Your...

    Write a C++ function to add a node to the beginning of a linked list. Your function takes two arguments - the head of the linked list and the value num to be added. Note that the list may be empty! Your function should modify the head of the linked list to point to the new node, and set the new node to point to the rest of the list (if not empty). Example: Initial Array: 4->2->3, key = 5...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • How to write MIPS function that recursively prints out linked list that takes one argument (address...

    How to write MIPS function that recursively prints out linked list that takes one argument (address of firstNode), and is separated by commas.  

  • Write a function searchList that recursively searches a linked list for a specified value. The function...

    Write a function searchList that recursively searches a linked list for a specified value. The function should return a pointer to the value if it’s found; otherwise, NULL should be returned. Use your function in a test program that creates a list of integers. The program should prompt the user for a value to locate in the list. It's a C program.

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