How to write a C Program Simple Linked List in C Programming Language ?
The following program shows how a simple, linear linked list can be constructed in C, using dynamic memory allocation and pointers.
Solution 1:
#include<stdlib.h> #include<stdio.h> struct list_el { int val; struct list_el * next; }; typedef struct list_el item; void main() { item * curr, * head; int i; head = NULL; for(i=1;i<=10;i++) { curr = (item *)malloc(sizeof(item)); curr->val = i; curr->next = head; head = curr; } curr = head; while(curr) { printf("%d\n", curr->val); curr = curr->next ; }Solution 2 :
/*simple linked list*/
#include <stdio.h>
int main(void)
{
struct entry
{
int value;
struct entry* next;
}n1, n2, n3, *list_pointer;
n1.value = 100;
n1.next = &n2;
n2.value = 200;
n2.next = &n3;
n3.value = 300;
n3.next = (struct entry *) 0;
list_pointer = &n1;
while (list_pointer !=n3.next)
{
printf("%i\n", list_pointer->value);
list_pointer = list_pointer->next;
}
return 0;
}