Linked list Program

Thread Starter

Parth786

Joined Jun 19, 2017
642
I am having problem to implement linked list program. it may be the lake of basics knowledge. Before try to write linked list program I studied pointer and structure. I know pointer and structure use to make linked list program. I don't understand the example given in my book.

structure is use to store the multiple type of data type

Program code
C:
#include<stdio.h>
#include<string.h>

struct Student
{
    char Name[20];
    char Section;
    int Roll_Number;
    float percentage;
} record;

int main (void)
{
    strcpy(record.Name, "Parth");
    record.Section = 'A';
    record.Roll_Number = 786;
    record.percentage = 70.20;
 
    printf("Name        : %s \n", record.Name);
    printf("Section     : %C \n", record.Section);
    printf("Roll_Number : %d \n", record.Roll_Number);
    printf("percentage  : %f \n", record.percentage);
  
    return 0;
}
Program Output
Name : Parth
Section : A
Roll_Number : 786
percentage : 70.199997

Linked list code
C:
struct list
{
  int Number;
  struct list *next;
}
I have seen some program on internet but I feel difficult to understand. I have decided to make program into small pieces. I need help to implement simple linked list program.
 
Last edited:

WBahn

Joined Mar 31, 2012
29,979
Do you know how to dynamically allocate memory for new instances of your structure?

The name "list" is not a very good name for that structure, because an instance of that structure is not a list, it is merely one node in a list. A better name would be "node" or even "Number".

Sketch out of paper what a linked list containing four elements would look like.
 

spinnaker

Joined Oct 29, 2009
7,830
Haven't we been down this road before?

Your example is incomplete and incorrect.

To make use of linked lists you would need to dynamically allocate memory for the stucture. In C you would use malloc. You dynamically allocate a your link list "list" structure and store that pointer in a pointer variable called first (for example). You would also set another pointer available called current (for example) to the same value as "first"

Start your loop. You would allocate more memory to current->next, set the values of your structure, set current to current->next, go back to the top of the loop.

This really isn't a difficult concept and I think you are trying to make it harder than it needs to be.

Here is a simple example just off the top of my head so it might not be 100% correct. And there is no loop to keep it simple.

Code:
struct list
{
  int Number;
  struct list *next;
}

struct  list *first;
struct list *current;


first = malloc(sizeof(struct list));
current = first;

list->number = 1;

current->next  = malloc(sizeof(struct list));
current->number = 2;
current = current->next
 

WBahn

Joined Mar 31, 2012
29,979
@spinnaker: Some typos to fix:

Line 15 should be: first->Number = 1;
Line 18 should be: current->Number = 2;

You're also leaving a dangling pointer.

The first thing that should be done upon allocating a new struct is to verify that it actually allocated.
The next thing that should be done is to set all uninitialized pointers in it to NULL.

@Parth786: By the time you get to working with structures, you should be proficient at working with pointers and working with user-defined functions. So use those skills. If you aren't proficient with them yet, then step back away from structures and go become proficient at those skills.

Wouldn't it be nice to be able to write your main() something like:

C:
int main(void)
{
   NODE *list = NULL;

   for (int i = 1; i <= 10; i++)
      list = NODE_ADD(list, i);

   printf("List values: ");
   NODE_printList(list);

   list = NODE_deleteList(list);

   return EXIT_SUCCESS;
}
And have it produce something like:

C:
List values: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
And be able to use these functions without worrying about whether memory has been allocated or whether they will behave if you happen to send them a NULL pointer?

So how might you write those functions?

EDIT: Cleaned up the prose a bit.
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
@spinnaker: Some typos to fix:

Line 15 should be: first->Number = 1;
Line 18 should be: current->Number = 2;

You're also leaving a dangling pointer.

The first thing that should be done upon allocating a new struct is to verify that it actually allocated.
The next thing that should be done is to set all uninitialized pointers in it to NULL.

@Parth786: By the time you get to working with structures, you should be proficient at working with pointers and working with user-defined functions. So use those skills. If you aren't proficient with them yet, the step back away from structures and go become proficient at them.

Wouldn't it be nice to be able to write your main() something like:

C:
int main(void)
{
   NODE *list = NULL;

   for (int i = 1; i <= 10; i++)
      list = NODE_ADD(list, i);

   printf("List values: ");
   NODE_printList(list);

   list = NODE_deleteList(list);

   return EXIT_SUCCESS;
}
And have it produce something like:

C:
List values: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
And be able to use these functions without worry about whether memory allocated or whether they will behave if you happen to send them a NULL pointer?

So how might you write those functions?

Like I mentioned. A quick example just off the top of my head to give the basic idea. Thanks of catching the typos though. Was thinking structure and not pointer. ;)
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
What to do to write further code
C:
#include <stdio.h>

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

int main (void)
{
    struct  Node *first;
    struct  Mode *current;

    first = malloc(sizeof(struct Node));           // first dynamically allocated variable

    current = first;

    first->number = 1;                               //assign data in first node

    current->next  = malloc(sizeof(struct Node));    // second  dynamically allocated variable

    current->number = 2;                              //assign data in second node

    current = current->next                          //Link first node with the next node
}
 

WBahn

Joined Mar 31, 2012
29,979
What to do to write further code
C:
#include <stdio.h>

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

int main (void)
{
    struct  Node *first;
    struct  Mode *current;

    first = malloc(sizeof(struct Node));           // first dynamically allocated variable

    current = first;

    first->number = 1;                               //assign data in first node

    current->next  = malloc(sizeof(struct Node));    // second  dynamically allocated variable

    current->number = 2;                              //assign data in second node

    current = current->next                          //Link first node with the next node
}
Why don't YOU try to "write further code"?

All it seems you have done here is take someone else's (spinnaker's) code snippet, put it into a main() (without the required return statement), and try to change "list" to "Node" (and introducing an error in the process). Since you didn't catch the typo, you clearly didn't even try to compile the code. Now, instead of YOU trying to extend it, you want someone else to do it for you (as usual).
 

OBW0549

Joined Mar 2, 2015
3,566
What to do to write further code
Figure it out yourself.

There's something you need to learn that's more important than how to write code: you need to learn how to teach yourself instead of constantly asking other people to instruct you, coach you, check your work every step of the way, and correct your coding mistakes. Looking at your various posts over the last few weeks, I've gotten the impression that you are either unable to do anything-- ANYTHING at all-- on your own, or are afraid to do the work yourself, or perhaps are too lazy.

A few days ago you posted about interviewing for a job as an embedded developer. Although I think it's extremely unlikely you'll get that job or any other job in the programming field because your skills fall far short of what any employer is going to expect, I have to tell you that if some miracle occurs and you actually are hired, you won't last very long if your behavior at work is anything like your behavior here: you absolutely CANNOT be constantly asking your co-workers for help. Any employer is going to expect, and demand, that you "pull your own weight" by working on your own and that you refrain from wasting your co-workers' time helping you; after all, they've got their own work to do. If your neediness prevents them from doing it, you'll be fired.

Also, you need to learn how to focus on one learning task at a time, rather than flitting randomly among unrelated topics as you have done so far in your various threads. Decide what you want to learn; work on learning it; and don't move on until you've mastered the subject.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Why don't YOU try to "write further code"?
Now, instead of YOU trying to extend it, you want someone else to do it for you (as usual).
That was one reason I did not start thread with the linked list program. I said hat I have only knowledge of structure and pointer. you can see last paragraph in my first post #1. I do not know why it so tough to understand.
Now, instead of YOU trying to extend it, you want someone else to do it for you (as usual).
I tried myself and I spent some time on internet but I couldn't move on further step. As I said before that I want to make code in small pieces. I am getting really tough to make whole code. I was looking way to make one thing at one time
 

WBahn

Joined Mar 31, 2012
29,979
But your idea of making code in small pieces seems to be to get someone to write a small piece of code for you and then to get someone to write a somewhat bigger piece of code for you.

The whole idea of building up slowly is for YOU to gain the necessary skills in order for YOU to move on to the next larger concept.

If YOU can't even begin to move onto the next step, then it's telling you that you aren't READY to move on to the next step. Insisting on doing so just means that you are putting on blinders and continuing to dig the hole you are in deeper and deeper.

I asked you to draw a diagram of what you wanted your linked list to look like and you wouldn't even try.

Will you at least TRY to draw the linked list that you think the one I built up in Post #4 should look like before it was printed?
 

WBahn

Joined Mar 31, 2012
29,979
How about the following approach?

I will give you a specification for a function related to a linked list and YOU write the function.

Let's start with the first function I called in Post #4:

NODE_ADD()

This function adds a new element, which is an integer, to the end of a singly-linked list.

This function takes two arguments.
The first is a pointer to a NODE structure that happens to be the first element in a singly-linked list.
The second is an integer value that is to be associated with the new node.

This function returns a pointer to a NODE structure.
If the NODE pointer passed to the function is NULL, the function creates a new list and returns a pointer to its head if successful.
If the NODE pointer is not NULL, it is assumed that the pointer is the head of the list. If the operation succeeds, the pointer returned is the one that was passed to it.
If the operation fails, the function returns a NULL pointer.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
A few days ago you posted about interviewing for a job as an embedded developer. Also, you need to learn how to focus on one learning task at a time, rather than flitting randomly among unrelated topics as you have done so far in your various threads
I am sorry but I am completely disagree with your thinking. I don't think that question was unrelated. I had to ask whether my answer was right or wrong because I couldn't verify the answer myself so I asked here. Is there anything wrong?

I attend interview and I got rejected many times. but still I am attending interviews because of that I know what's lacking in me for interview. so I just remember that things and prepared that very well for next. I am fresher and it's vary hard to get job as fresher

Now My focus are only on two things c programming and programming for Microconroller. I am working on both c programming and programming for Microconroller. I write c ode and test them on my PC and I am writing program for mirocontroller and testing on simulator.

If you have seen my threads, you may have seen this latest also https://forum.allaboutcircuits.com/threads/interrupt-in-project.145334/page-3
I am trying one after the one but still I couldn't solve the problem. I am still trying to figure out what's the problem.
 

miniwinwm

Joined Feb 2, 2018
68
I attend interview and I got rejected many times. but still I am attending interviews...
You have no chance at all getting a job as a programmer yet. There are 2 reasons as said above, first your level of programming ability is way way below what is required, and secondly because you are unable to either figure out things yourself or do the research to find a resource to help you. For example, I just went to Google, typed in 'how to write a linked list', and the first hit was this...

http://www.learn-c.org/en/Linked_lists

You don't even need to download a freebie compiler, you can run it on that tutorial site. Now why couldn't you do that? Until you can, you are wasting your time applying for jobs as a developer.
 

spinnaker

Joined Oct 29, 2009
7,830
I attend interview and I got rejected many times. but still I am attending interviews because of that I know what's lacking in me for interview. so I just remember that things and prepared that very well for next. I am fresher and it's vary hard to get job as fresher
And you aren't seeing a pattern? I can think of 2-3 times in my whole career I have been rejected for an interview.

The one time I was far too young for what the company wanted. The interviewer knew it up front. And I knew it up front that I would likely not get the job. But I was granted the interview anyway since the company had a extremely interesting product. I was granted the interview just so I could learn a bit more of the company. And I was initially rejected as I expected but later got the oddest call I ever got. The person that interviewed me called and told me they were impressed with me but were still concerned about my youth. He waited to know if they could not find someone a bit older if I would still be interested in taking the job.

Then he asked me if I would rather go to Bangkok or Seattle for my first assignment. I thought about it for a minute and thought about it being so close to the end of the Vietnam war and my friends telling me how wild Bangkok was. I figured an 18 yo boy could get into an awful lot of trouble in Bangkok . ;) So I was honest and answered Seattle but that I would travel anywhere afterward. Maybe my answer should have been "Anywhere". But I will never know since I did not get the job. I really regret not getting that job because it would have been the most interesting job I had ever worked.

I have always done well at interviews because I have always been prepared and more important qualified for the job.

You might want to take a job as an intern where you can actually get to learn something on the job. You won't get paid but better than doing what you have been doing.
 
Last edited:

WBahn

Joined Mar 31, 2012
29,979
And you aren't seeing a pattern? I can think of 2-3 times in my whole career I have been rejected for an interview.
Before I got my PhD I almost never didn't get an interview and it almost never didn't lead to an offer. Like you, maybe 2-3 times.

I didn't start not getting interviews until after I got my PhD. I realized then that a PhD tends to pigeonhole you as being a deep expert in a narrow field and, in the eyes of many potential employers, wanting more compensation than the generalist they want is worth. Unfortunately, I'm a generalist and the people that WANT a to hire a PhD want someone that IS a deep expert in something very close to what they want that person to do.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
you are wasting your time applying for jobs as a developer.
Now I am really scared. I did not lost my hope yet but I am losing my hope now. I am passing through the bitter truth of life. I am on the way of learning . There are few people who encourage you. there are many people they always discourage you. someone learn a quickly, someone take more time. Not all are the same,
 

spinnaker

Joined Oct 29, 2009
7,830
Now I am really scared. I did not lost my hope yet but I am losing my hope now. I am passing through the bitter truth of life. I am on the way of learning . There are few people who encourage you. there are many people they always discourage you. someone learn a quickly, someone take more time. Not all are the same,

You might need to face the fact that you will never be a programmer. At least not someone that can make money at it. It is not just you. Some people just never get it. All the study and encouragement in the world isn't going to change that.
 

OBW0549

Joined Mar 2, 2015
3,566
Now I am really scared. I did not lost my hope yet but I am losing my hope now. I am passing through the bitter truth of life. I am on the way of learning . There are few people who encourage you. there are many people they always discourage you. someone learn a quickly, someone take more time. Not all are the same,
No prospective employer is going to care about that, nor about any other excuse you make. They care about only one thing: can you do the programming work that they need done. And right now, for any imaginable programming job, the answer to that question is going to be "absolutely not." Not even close.
 
Top