Program : Player list with score

Thread Starter

anukalp

Joined Jul 28, 2018
158
I'm practicing on linked list program. I'm taking example of player's

My program is supposed to list player's name with scores.

Player name :---------- Score
Tendulakar ------------ 99
Ganguly -------------- 86
Dravid --------------- 94

I understand basic program for linked list

I have created only three nodes. I can create eleven node's


any help How to make proper program ?

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

struct Node
{
  int data;
  struct Node *next;
};
  int main()
{
  struct Node* head = NULL;
  struct Node* second = NULL;
  struct Node* third = NULL;
  struct Node *temp = NULL;


  head  = (struct Node*)malloc(sizeof(struct Node));
  second = (struct Node*)malloc(sizeof(struct Node));
  third  = (struct Node*)malloc(sizeof(struct Node));
 
  head->data = 1;       //assign data in first node
  head->next = second;  // Link first node with second
 
  second->data = 2;     //assign data to second node
  second->next = third;
 
  third->data = 3;     //assign data to third node
  third->next = NULL;

   temp = head;
  while (temp != NULL)
  {
     printf(" %d ", temp->data);
     temp = temp->next;
  }
  
  return 0;
}
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
Sounds like homework to me.

How do you think you can create 11 nodes? You already created 3.

Lookup for loop on the internet. It would be a more efficient way to create the nodes.

Hint: Your for loop is going to need a temporary variable to store the previously allocated pointer. You will also need to know when you are setting the last allocated node.

Look up linked lists. There are many, many examples that can be found.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
Sounds like homework to me.

Look up linked lists. There are many, many examples that can be found.
What list would you make if you get the chance

My favorite sport is cricket, so I have taken this example

I think I have selected difficult task for me, How to break with small steps ?
 

spinnaker

Joined Oct 29, 2009
7,830
What list would you make if you get the chance

My favorite sport is cricket, so I have taken this example

I think I have selected difficult task for me, How to break with small steps ?
"Linked lists" is a programming concept. Is it what you are doing. Look it up

Not sure how you can break it up any further than you have already done.
 

dl324

Joined Mar 30, 2015
16,943
The type of linked list you're proposing isn't very efficient if the list gets large.

In a case where you could have hundreds or thousands of players, it would be more efficient to create an array of struct Node and use the hashed name as an array index. You'd only need to use the pointer to Next in cases where multiple names hashed to the same integer.
 

spinnaker

Joined Oct 29, 2009
7,830
The type of linked list you're proposing isn't very efficient if the list gets large.

In a case where you could have hundreds or thousands of players, it would be more efficient to create an array of struct Node and use the hashed name as an array index. You'd only need to use the pointer to Next in cases where multiple names hashed to the same integer.

Its homework. The TS needs to start somewhere. The "most efficient" solution isn't the best solution right now. Far better to start with baby steps. Please don't confuse the TS.
 

MrSoftware

Joined Oct 29, 2013
2,202
Hint: Make a function that adds a new node to the list and and returns a pointer to the new node. Such as:

Code:
struct node
{
  int data;
  struct node *next;
};
// Make your node a "type", less typing later
typdef struct node Node;

// This function takes a pointer to the last node on the list and does 3 things: 
// (1) Create a new node 
// (2) Add it to the list.  
// (3) Return a pointer to the new node, which is also now the last node on the list
Node* AddNewNode(Node* lastNodeOnList)
{
    // Create your new node in here and add it to the end of the list. Update your pointer to the last node

    return lastNodeOnList;   // Return the last node on the list here, it is your new node
}

// Now just call the above function from within the loop where you're adding new nodes
 

spinnaker

Joined Oct 29, 2009
7,830
Baby steps. Functions are a more advanced concept. No need to worry abou it now.

Come to think of it a for loop that I recommended might be a bit too advanced. ;)
 

MrSoftware

Joined Oct 29, 2013
2,202
Surely anyone working with structs, pointers, dynamic memory allocation and linked lists has already learned about function calls!?
 

dl324

Joined Mar 30, 2015
16,943
OP is an electronics student who is trying to increase his knowledge of C. I don't recall any of his programming related threads being homework.
 

MrSoftware

Joined Oct 29, 2013
2,202
To the OP; after you get your linked list working, don't forget that you need to "free" any memory that you "malloc". So at the end of your program add a small function to run through your linked list and free any memory that you allocated dynamically. :)
 

dl324

Joined Mar 30, 2015
16,943
Looks like a typical homework assignment to me.
The questions have all been basic, but there was no specific coding objective. He's pulling objectives out of his head, not from a homework assignment; otherwise there would be some specific objective. Someone would have noticed, asked if it was homework, and, if it was, would have flagged the post to be moved to Homework Help.
 
Top