Question

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;

NULL 1.0 3.0 value next value next prev prev Value next prev head tail NULL

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 function called listSize that takes two input parameters - the pointer head OR tail plus a parameter of which direction to traverse - to traverse the linked list and return the number of elements in the list. Write another function printForwardElem that prints out the value of every element of the linked list in sequence starting from head forward to the end of the list. Write a third function printBackwardElem that prints out the value of every element of the linked list in sequence starting from tail backward to the beginning of the list. Design your driver to test these three functions.

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

Here is the solution for above code:

int listSize (struct listrec* item,String direction)
{
  struct listrec* cur = item;
  int size = 0;
  if(direction == "right"){

  while (cur != null)
  {
    ++size;
    cur = cur->Next;
  }
}
else {
while (cur != null)
  {
    ++size;
    cur = cur->prev;
  }
}
  return size;
}
void printForwardElem(struct listrec* item) {
   struct node *ptr = item;

   printf("\n[head] <=>");
   //start from the beginning
   while(ptr != NULL) {        
      printf(" %d <=>",ptr->data);
      ptr = ptr->next;
   }

   printf(" [last]\n");
}
void printBackwardElem(struct listrec*start)
{
    struct listrec*current = start;  //current points to first node
    if(current==NULL)  //if empty list, return 
       return;

    //now we are sure that atleast one node exists
    while(current->link != NULL) //goes until current == last node
    {
        current = current->link; //keep on going forward till end of list
    }

    //start from last node and keep going back till you cross the first node
    while(current != NULL)
    {
        DisplayNode((struct inventory*)current->item.value);
        current = current->prev; //go one node back
    }
}
Add a comment
Know the answer?
Add Answer to:
In C++ Assume entries in a linked list are of type struct listrec: struct listrec {...
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
  • 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...

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • C LINKED LIST #include <stdlib.h> #include <stdio.h> //Struct for a polynomial term (double linked list object)...

    C LINKED LIST #include <stdlib.h> #include <stdio.h> //Struct for a polynomial term (double linked list object) typedef struct Polynomial t_Polynomial; typedef struct Term { // coefficient of the polynomial term associated with the object int coef;    // power of the polynomial term associated with the object int power;    // pointer to the next object (to facilitate a doubly linked list) struct Term *next;    // pointer to the previous object (to facilitate a doubly linked list) struct Term...

  • 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....

  • Suppose we implement a doubly linked list class template LinkedList with template type T. LinkedList has...

    Suppose we implement a doubly linked list class template LinkedList with template type T. LinkedList has fields Node *headPtr, Node *tailPtr and int length, where the struct type Node has fields prev and next of type Node* along with data of type T. The prev and next pointers of each Node points to the previous and next Nodes in the list (or are respectively null in the case of the list’s head or tail node). We wish to detect "invalid"...

  • Given the following linked list structure called node: struct node { int val; struct node *...

    Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...

  • c++ File name: 2170 107b.ee Purpose: This program demonstrates the use of multi-linked lists. //This file...

    c++ File name: 2170 107b.ee Purpose: This program demonstrates the use of multi-linked lists. //This file contains the implementation file for the 2170_10_7b.h header file. This implementation uses a dynamic linked list structure implementing the // Multi ListClass object. The implementation uses nodes which have two pointer fields one to point to the next record by name (nextName) and one to point Ito the next record by account number(nextNum). The linked list is also maintained I with a dummy header...

  • CODE IN C++ Given: typedef char ItemType; // a struct type named NodeType that includes a...

    CODE IN C++ Given: typedef char ItemType; // a struct type named NodeType that includes a ItemType and pointer // (to a NodeType) field struct NodeType {    ItemType value;    NodeType * next; }; bool Delete (NodeType * & firstPtr, ItemType value) {    NodeType * prev, * cur; cur = Search (firstPtr, value, prev); if (cur==NULL) return false; else { // remove cur node from the linked list if (prev!=NULL) { //not the first one prev->next = cur->next;...

  • c++ only Program 2: Linked List Class For this problem, let us take the linked list...

    c++ only 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 Node { int number; Node * nextNode;...

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