LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   concepts a linked list program in c (https://www.linuxquestions.org/questions/programming-9/concepts-a-linked-list-program-in-c-354821/)

ssg14j 08-19-2005 05:11 AM

concepts a linked list program in c
 
Tell me the concepts of Linked List program using c.......
Give me one example..

Hko 08-19-2005 06:25 AM

Re: concepts a linked list program in c
 
Quote:

Originally posted by ssg14j
Tell me the concepts of Linked List program using c.......
Give me one example..

Why? Didn't google take orders from you?

jtshaw 08-19-2005 06:54 AM

The concept is pretty simple. Define a struct for your link list node. A node comprises of data, which can be anything, and a pointer to another node typically called next.

When creating a link list node initially you want to set the next pointer to null and the data pointer to whatever you want it to be. The first node is typically called head, and you never want to have a situation where you don't have a pointer to this node anymore. I also like to always have a pointer to the end of the list I call tail. When creating the first node of a linked list I set both the head and tail pointer to it.

Say N1 is a pointer to a node.

N1->next = null;
head = N1;
tail= N1;

Now... to add a node to the beginning of the list you simply create the node, set the new node->next = head, and then set head = node;

N2->next = head;
head = N2;
our list now: head (aka N2)->next = tail( aka N1)->next = NULL

To add a node to the end of the list create the node, set tail->next equal to the node and move the tail pointer.

N3->next = NULL;
tail->next = N3;
tail = N3;
our list now: head (aka N2)->next = N1->next = tail (aka N3)->next = NULL;

I'll leave the rest as an exercise for the reader.

NCC-1701&NCC-1701-D 08-19-2005 06:56 AM

http://cslibrary.stanford.edu/103/

Everything about linked lists in C (and in C++)
Hope it helped!

itsme86 08-19-2005 10:10 AM

This is so obviously homework...


All times are GMT -5. The time now is 03:12 PM.