Question

a. Using C++, define a node structure of the linked list (e.g. value is an integer,...

a. Using C++, define a node structure of the linked list (e.g. value is an integer, next is a node type pointer), construct a linked list of 10 nodes and assign random numbers as the nodes’ values. Use loop to track and print from the first node to the last and output all nodes’ values. Finally, free all memories of the linked list.

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

SOLUTION:

#include<bits/stdc++.h>
using namespace std;
struct node{
   int data;
   node* next;
};
node*head;//global head of linked list

void insert(int x){
   node*temp=new node; //allocating memory
   temp->data=x;
   temp->next=head;
   head=temp; //insertion at the beginning of the linkedlist
}

void print(){
   //for printing nodes
   node*temp=head;
   while(temp!=NULL){
       cout<<temp->data<<endl;
       temp=temp->next;
   }
   //for deleting nodes in memory
   while(head!=NULL){
   temp=head;
   delete temp;
   head=head->next;
   }
}

int main(){
   head=NULL;
   cout<<"how many numbers you want to enter :"<<endl;
   int n;
   cin>>n;
  
   for(int i=0;i<n;i++){
       srand(time(0));//random numbers will be generated for every run
       insert(rand());   //calling insert function with random int as parameter
   }
   print();//calling print function
}

SCREENSHOT OF CODE:

SCREENSHOT OF OUTPUT:

NOTE:

If you are satisfied with my answer please do upvote and if you have any kind of doubts please post in the comment section. I'll surely help you there.
Thank You:)

Add a comment
Know the answer?
Add Answer to:
a. Using C++, define a node structure of the linked list (e.g. value is an integer,...
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
  • 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...

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

  • C++ Consider the following structure of node and linked list. struct Node { int key; Node...

    C++ Consider the following structure of node and linked list. struct Node { int key; Node *next; }; 10 -> 20 -> 30 -> 10 -> 10 -> 50 -> 10 -> NULL What will be the output of following pseudo-code? Consider head is the pointer to the first node of above linked list. Node *walker = head; int count = 0; while(walker!= NULL && count < 3) { if(walker->key == 10) { count = count + 1; } walker...

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

  • A linked list of integers is built using the following struct: struct node {   int data;...

    A linked list of integers is built using the following struct: struct node {   int data;   struct node *next; }; Define a function named max that returns the maximum integer in a list. The function takes one arguments, a pointer to the head of the list. The function returns an integer, which is the maximum value. If the list is empty, return zero. NOTE: You know nothing about the values in the list. They could all be negative!

  • Given the node structure and the head pointer (headptr) of a linked list write a code...

    Given the node structure and the head pointer (headptr) of a linked list write a code to move the last element to the head of the list. struct node {    int value;    struct node *next; };

  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • do the following in Node* buildLinkedList() function 1. Build linked list of 3 nodes. 2. Ask...

    do the following in Node* buildLinkedList() function 1. Build linked list of 3 nodes. 2. Ask the user for the data values in the structure. 3. Follow the example above on how to build linked list. Make sure you ask the user for the data values. 4. Return head. in Main do the following: 1. Declare a pointer to structure of type Node. 2. Call the function buildLinkedList. 3. Display the 3 nodes data values.

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

  • You are given a pointer head to the first node in a linked list. Each node...

    You are given a pointer head to the first node in a linked list. Each node points to the next node. We call the list a snail if the last node points to some node v in the list. We call the list a snake if the last node points to NULL. The list has n nodes. You are not allowed to change the list (permanently or temperately). You are allowed to use O(1) extra memory. The value of n...

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