Implementation of Linked List using Array in C Programming

In this source code example, we will write a code to implement the Linked List data structure using an Array in C programming language.

In this C program, we will take input from the User or console.

Implementation of Linked List using Array in C Programming

#include<stdio.h>
#define TRUE 1
#define SIZE 10
struct link {
	int info;
	int next;
};
struct link node[SIZE];
int Getnode();
void Createlist();
void Freenode(int);
void Display();
void Insert(int, int);
void Delete(int);
int p, avail = 0;
void main() {
	int ch = 1, i, n, x;
	/*Creation of available list*/
	for (i = 0; i < SIZE - 1; i++)
		node[i].next = i + 1;
	node[SIZE - 1].next = -1;
	printf("\n Create a List:");
	Createlist();
	while (ch != 4) {
		printf("\n1-DISPLAY");
		printf("\n2-INSERT");
		printf("\n3-DELETE");
		printf("\n4-QUIT");
		printf("\n Enter your choice:");
		scanf("%d", &ch);
		switch (ch) {
		case 1:
			Display();
			break;
		case 2:
			printf("\n Node insertion:after which node:");
			scanf("%d", &n);
			p = n;
			printf("\n Enter the item for insertion:");
			scanf("%d", &x);
			Insert(p, x);
			break;
		case 3:
			printf("\n Enter the node after which the node will be deleted:");
			scanf("%d", &n);
			p = n;
			Delete(p);
			break;
		case 4:
			break;
		default:
			printf("\n Wrong choice!Try again:");
		}
	}
}
int Getnode() {
	if (avail == -1) {
		printf("\n Overflow:");
	}
	p = avail;
	avail = node[avail].next;
	return p;
}
void Freenode(int q) {
	node[q].next = avail;
	avail = q;
	return;
}
void Createlist() {
	int x;
	char c;
	p = Getnode();
	printf("\n Enter an item to be inserted:");
	scanf("%d", &x);
	node[p].info = x;
	node[p].next = -1;
	while (TRUE) {
		printf("\n Enter the choice(y/n):");
		fflush(stdin);
		c = getchar();
		if (c == 'y' || c == 'Y') {
			printf("\n Enter an item to be inserted:");
			scanf("%d", &x);
			Insert(p, x);
			node[p].next = -1;
		} else
			return;
	}
}
void Display() {
	p = 0;
	while (node[p].next != -1) {
		printf("\n%d\t%d\t%d:", p, node[p].info, node[p].next);
		p = node[p].next;
	}
	printf("\n%d\t%d\t%d:", p, node[p].info, node[p].next);
}
void Insert(int r, int x) {
	int q;
	if (r == -1) {
		printf("\n void insertion:");
		return;
	}
	q = Getnode();
	node[q].info = x;
	node[q].next = node[r].next;
	node[r].next = q;
	return;
}
void Delete(int r) {
	int q;
	if (r == -1 || node[r].next == -1) {
		printf("\n void deletion:");
		return;
	}
	q = node[r].next;
	node[r].next = node[q].next;
	Freenode(q);
	return;
}

Output:


 Create a List:
 Enter an item to be inserted:10

 Enter the choice(y/n):y

 Enter an item to be inserted:20

 Enter the choice(y/n):y

 Enter an item to be inserted:30

 Enter the choice(y/n):y

 Enter an item to be inserted:40

 Enter the choice(y/n):y

 Enter an item to be inserted:50

 Enter the choice(y/n):n

1-DISPLAY
2-INSERT
3-DELETE
4-QUIT
 Enter your choice:1

0	10	1:
1	20	2:
2	30	3:
3	40	4:
4	50	-1:
1-DISPLAY
2-INSERT
3-DELETE
4-QUIT
 Enter your choice:2

 Node insertion:after which node:2

 Enter the item for insertion:60

1-DISPLAY
2-INSERT
3-DELETE
4-QUIT
 Enter your choice:1

0	10	1:
1	20	2:
2	30	5:
5	60	3:
3	40	4:
4	50	-1:
1-DISPLAY
2-INSERT
3-DELETE
4-QUIT
 Enter your choice:3

 Enter the node after which the node will be deleted:5

1-DISPLAY
2-INSERT
3-DELETE
4-QUIT
 Enter your choice:1

0	10	1:
1	20	2:
2	30	5:
5	60	4:
4	50	-1:
1-DISPLAY
2-INSERT
3-DELETE
4-QUIT
 Enter your choice:4

Related Data Structures in C


Comments