Question

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.

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

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

void printList(struct Node* iterator)
{
while (iterator!=NULL) {
printf(" %d ", iterator->data);
iterator = iterator->next;
}
}
  
struct Node* buildLinkedList()
{
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
struct Node* second = (struct Node*)malloc(sizeof(struct Node));
struct Node* third = (struct Node*)malloc(sizeof(struct Node));
if(head ==NULL || second ==NULL || third ==NULL )
{
return NULL;
}
  
int i = 1;
printf("Enter %d node's data: ", i++);
scanf("%d", &head->data); // assign data in first node
head->next = second; // Link first node with second
  
printf("Enter %d node's data: ", i++);
scanf("%d", &second->data); // assign data in second node
second->next = third;
  
printf("Enter %d node's data: ", i++);
scanf("%d", &third->data); // assign data in third node
third->next = NULL;
  
return head;
  
}

int main()
{
struct Node* head = NULL;
head = buildLinkedList();
  
printList(head);
  
return 0;
}

Output:

Please give thumbs up for the solution.

Add a comment
Know the answer?
Add Answer to:
do the following in Node* buildLinkedList() function 1. Build linked list of 3 nodes. 2. Ask...
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...

  • Derive a class called Stack from the linked list described in Assignment 2 (list of Dates)....

    Derive a class called Stack from the linked list described in Assignment 2 (list of Dates). This means the Stack class will inherit all the properties (data and functions) of the linked list. But, since a stack only allows pushing and popping at the front of the list only, you will need to prevent the operations at the back. To do this, derive the Stack class in such a way that the base class (LinkedList) functions become private in the...

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

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

  • true or false A linked list is a list of items, called nodes, in which the...

    true or false A linked list is a list of items, called nodes, in which the order is determined by the address, called of the nodes 1. 2. The pointer to a linked list-that is, the pointer to the first node in the list-is stored in a separate location called the head or first. 3. A linked list is a dynamic data structure.

  • Problem Statement This problem provides you with a linked list composed of node objects chained together...

    Problem Statement This problem provides you with a linked list composed of node objects chained together via node pointers. Each node has a next pointer which points to the next node in the chain. The last node is identified by having a NULL (or nullptr) next pointer. The linked lists for this problem store string data. Your task: identify the longest string stored in the linked list. If two strings are of the same length, return the string that occurred...

  • This is a recursive function to delete all even nodes from a linked list. node *deleteEven(node...

    This is a recursive function to delete all even nodes from a linked list. node *deleteEven(node *head) if (head == NULL)    return head;    if (head->data % 2 == 0) { node *temp = head; free(temp); return deleteEven(head->next); } else { head->next = deleteEven(head->next); return head; } } What is the purpose of heaving to set "head->next = deleteEven(head->next);" instead of just having "head = head->next;" in one of the lines above and just calling "deleteEven(head->next);"?

  • Language:C++ only numbers 4 and 5 please 2. 1. Use the Node class from the slides...

    Language:C++ only numbers 4 and 5 please 2. 1. Use the Node class from the slides to create a linked list of int's. Create a Node pointer called ptrHead that points to the head of the list. Make the list 5 nodes long. Generate random values to populate the list's data. All nodes should be in the heap. Add a function called addNodeFront that takes a pointer to the head of a list as an input parameter and that adds...

  • Programing C Just with #include <stdio.h> We will create a singly linked list of 7 nodes....

    Programing C Just with #include <stdio.h> We will create a singly linked list of 7 nodes. Then, the user will tell us whether to print the “odd-placed” nodes or the “even-placed” nodes. For example, if I had a list of 5 nodes like below, and the user specifies for the odd nodes to be printed, my program would print the values in nodes nl and n3. If the user instead indicated for the even nodes to be printed, my program...

  • Structure struct Node int Manth; // Mont h double dAvg: 1/ Average struct Node pNext // with, the linked İist 3hown above the function will return gven that the average is 3.8 Ptr to next -Nod...

    Structure struct Node int Manth; // Mont h double dAvg: 1/ Average struct Node pNext // with, the linked İist 3hown above the function will return gven that the average is 3.8 Ptr to next -Node; Ret (3,3.8) (4,2.5) (20pts)( Recursive function) Show the code for a function that receives a pointer to the head of an ordered singly linked list that uses the structure in the top left. The function will return the pointer node that shows the highest...

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