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;
}
Learn More :
Dynamic Memory Allocation
- C Program to accept n numbers from user & find out the maximum element out of them by using dynamic memory allocation
- C program to reverse an array elements using Dynamic Memory Allocation
- C Program to calculate the sum of elements of upper triangle of a n*n matrix using DMA
- Calculate sum of element of upper triangle of m*n matrix by using dynamic memory allocation
- Calculate sum of element of lower triangle of m*n matrix by using dynamic memory allocation
Linked List
- C Program to Demonstrates a linked list for numbers.
- C Program to Implementation of List ADT as linked-list
- Pre Order, Post order, In order Implement Binary Tree using linked list
- C program allocates new nodes and creates a four element list with fixed values
- Program to Add Two Polynomials Using Linked List C Program
- Linked List For Getting Employee Details, Display and Search For Salary C Program
- Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list
Pointer
- C Program Pointer Example
- C Program To Reverse The String Using Pointer
- C Program to Find max and min in array using pointer concept
- C Program to Adds literal to list of literals
- C Program to find smallest in an array using pointer
- C Program A simple demonstration of using pointers to determine which function to use
- C Program LEXICAL ANALYSER
- File Handling (console to file) in C Program