Thread: Does code result match with diagram

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    61

    Does code result match with diagram

    I wrote code for linked list and drawn diagram for list to show behavior. I just want to verify that description shown in picture match with code. Does it really match ?


    Code:
    #include<stdio.h>#include<stdlib.h>
    
    
    struct Node
    {
      int Marks;
      struct Node *next;
    };
    
    
    struct Node* newStudent(int n, struct Node *Ps) {
        struct Node *new = malloc(sizeof(*new));
    	printf(" Location new : %p \n", (void*)new);
               new->Marks = n ;
               new->next = Ps;
    		   
        return new;
    }
    
    
    void Display(struct Node *Ps){
         struct Node *c;
         c = Ps;
         while (c!=NULL){
               printf(" %d\n",c->Marks);
               c = c->next;
               }
    
    
         }
    
    
    int main (void )
    {
        struct Node *Ps = NULL;  
        printf(" Location Ps : %p \n", (void*)Ps);
        Ps = newStudent(75, Ps);  
    	printf(" Location Ps : %p \n", (void*)Ps);
    	Ps = newStudent(85, Ps);  
    	printf(" Location Ps : %p \n", (void*)Ps);
        Ps = newStudent(72, Ps);  
    	printf(" Location Ps : %p \n", (void*)Ps);
    	Ps = newStudent(68, Ps); 
    	printf(" Location Ps : %p \n", (void*)Ps);
    	
        Display(Ps);
    
    
        return 0;
    }
    Result
    Code:
     Location Ps : 00000000 Location new : 00641448
     Location Ps : 00641448
     Location new : 00641478
     Location Ps : 00641478
     Location new : 00641488
     Location Ps : 00641488
     Location new : 00641498
     Location Ps : 00641498
     68
     72
     85
     75
    Attached Images Attached Images Does code result match with diagram-linked-list-jpg 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,892
    Looks good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-17-2019, 05:39 PM
  2. UML class diagram to code translation
    By Shafiul in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2019, 10:56 PM
  3. Replies: 2
    Last Post: 03-15-2019, 12:57 PM
  4. C Code for a flow diagram
    By unity006 in forum C Programming
    Replies: 4
    Last Post: 11-08-2015, 04:24 PM
  5. Replies: 5
    Last Post: 02-17-2015, 12:23 PM

Tags for this Thread