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

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

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

  • A linked list is constructed of nodes described by the following structure: struct node{ char data;...

    A linked list is constructed of nodes described by the following structure: struct node{ char data; struct node *next; }; Assume a linked list containing a sentinel node is constructed from the above nodes. Write a function named "count"-prototyped as int count(struct node*sent)- that accepts a pointer to the sentinel node; counts the number of data (non-sentinel) nodes containing the character 'A'; and returns that count as the function value.

  • Codelab question! This is c++. Consider the following code: // Linked Lists: TRAVERSE struct ListNode {...

    Codelab question! This is c++. Consider the following code: // Linked Lists: TRAVERSE struct ListNode { int data; struct ListNode *next; }; Assume that a linked list has been created and head points to a sentinel node. A sentinel node is an empty data node in the beginning of the list. It sometimes holds a sentinel value. The use of sentinel nodes is a popular trick to simplify the insert and delete operations. You may also assume that the list...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

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