Question

Please explain step by step what is going on each step.please clearly explain line by line...

Please explain step by step what is going on each step.please clearly explain line by line the working of code . Also provide the output. I would rate positively.

Thank you so much .

#include <stdio.h>

#include <stdlib.h>

struct  node  {

      int data;

      struct  node  *next;  

};  

struct  node  *head = NULL;

void  printList() {

      struct  node  *ptr  = head;

      printf("\nhead:");  

  

      while(ptr !=  NULL) {

            printf("node  addr:%p   \tdata:%d   \tnext addr:%p\n”, ptr,ptr->data,ptr->next);

              

            ptr = ptr->next;  

      }

      printf("  [null]\n");

}

void  insert(int  data) {

      struct  node  *link = (struct node*)        malloc(sizeof(struct  node));

      link->data  = data;

      link->next  = head;

      head  = link;

}

int main()  {

      insert(15);

      insert(2);  

      insert(33);

      insert(5);  

      insert(75);

      insert(13);

      printList();  

      return  0;  

}

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

OUTPUT:

head:node addr:0x1bec0b0    data:13    next addr:0x1bec090
node addr:0x1bec090    data:75    next addr:0x1bec070
node addr:0x1bec070    data:5    next addr:0x1bec050
node addr:0x1bec050    data:33    next addr:0x1bec030
node addr:0x1bec030    data:2    next addr:0x1bec010
node addr:0x1bec010    data:15    next addr:(nil)
[null]

The line by line explanation of the source code is given below with comments:

//header file
#include <stdio.h>
#include <stdlib.h>

//structure declaration
struct node {
//structure member list
int data;
struct node *next;
};

//declare a structure type variable and initialize with NULL value
//'head' is the base pointer of the linked list
struct node *head = NULL;

//function to print the node value with address
void printList() {
//declare a structure type variable and initialize with head
struct node *ptr = head;
  
//display the message on the computer screen
printf("\nhead:");
  
//display the node value and address until the end of the linked list or NULL pointer
//The last node of the linked list is assigned with a NULL pointer
//if we found the address value in any node equal to NULL then it is the last node
while(ptr != NULL) {
  
//display the node address, value stored in node and the address of the next node
printf("node addr:%p \tdata:%d \tnext addr:%p\n", ptr,ptr->data,ptr->next);
  
//assign the pointer to the next node
ptr = ptr->next;
}
//display the message on the computer screen
printf(" [null]\n");

}

//function to insert a new node at the front of the linked list
void insert(int data) {

//create a new node and allocate memory by using the malloc() function
struct node *link = (struct node*) malloc(sizeof(struct node));
  
//assign the new node data field with the paramener value
link->data = data;
  
//assign the new node address field with the head pointer
link->next = head;
  
//assign the head node to the new node
head = link;

}

int main() {

//insert 15 to the linked list
insert(15);
  
//insert 2 to the linked list
insert(2);
  
//insert 33 to the linked list
insert(33);
  
//insert 5 to the linked list
insert(5);
  
//insert 75 to the linked list
insert(75);
  
//insert 13 to the linked list
insert(13);
  
//display the linked list by calling printList() function
printList();

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Please explain step by step what is going on each step.please clearly explain line by line...
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
  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • Hi, I need to make a program in C that reads the type of currency and...

    Hi, I need to make a program in C that reads the type of currency and organizes them into stacks based on currency which can be read back. This is what I have so far, can I get help finishing it? #include <stdio.h> #include <stdlib.h> const float POUND = 1.31; const float YEN = 0.0091; const float RUPEE = 0.014; const float EURO = 1.11; char c; int currValue; float exchangeValue; float finValue; int printValue; struct node {    int...

  • Please fill in the code to reverse a linked list. IN C++ #include <stdio.h> #include <stdlib.h>...

    Please fill in the code to reverse a linked list. IN C++ #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list...

  • C LANGUAGE I just need the void push() function, which inserts new node to the front...

    C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...

  • need this updated so it will delete the list and then recreate it again /***********************************************************/ /*...

    need this updated so it will delete the list and then recreate it again /***********************************************************/ /* Header files. */ /***********************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> /***********************************************************/ /* Structure definitions. */ /***********************************************************/ struct node { int data; struct node *right; struct node *left; }; struct trash { struct node *node; struct trash *next; }; /****************************************/ /* BUILD_LIST. */ /****************************************/ void BUILD_LIST(int number2Add, struct node *(*head), struct node *(*tail)) { int i; struct node *previous, *current; *head = NULL; *tail =...

  • Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h>...

    Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h> struct node_int; typedef struct node int *node; struct node_int void *data; node next; typedef struct list_int (node first;} *list; void main(int argc, char *argv[]) list shopping, t; node n; int x=25; shopping=(list) malloc(sizeof(struct list_int)); n= (node) malloc(sizeof(struct node_int)); n->data=shopping; n->next=NULL; shopping->first=n; t=(list) (shopping->first->data); // ***** t->first=NULL; // ***** n->data=&x; printf("%d\n", *((int *) (shopping->first->data))); a What will be displayed on the screen if the above...

  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • I am stuck on a data structure problem, I am just going off of Geeks for...

    I am stuck on a data structure problem, I am just going off of Geeks for Geeks for help but I need to print 100 random numbers in the range of [1-200]. I can currently only print 5 numbers. Here is my code: #include <stdio.h> #include <stdlib.h> #include <limits.h> using namespace std; //binary tree has data & left and right child struct node{ int data; struct node *left; struct node *right; }; //create a new node struct node* newNode (int...

  • Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */...

    Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */ struct myWord{    char Word[21];    int Length; }; int tokenizeLine(char line[], struct myWord wordList[]); void printList(struct myWord wordList[], int size); void sortList(struct myWord wordList[], int size); /** * main function */ int main() {    struct myWord wordList[20];    char line[100];    printf("Enter an English Sentence:\n");    gets(line);    int size = tokenizeLine(line, wordList);    printf("\n");    printf("Unsorted word list.\n");    printList(wordList, size);...

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