Home Linked List Create a Linked List

Create a Linked List

832
0
Create a Linked List
/*C code to create and print the Linked List*/
#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
	int data;
	struct node *next;
} NODE;

NODE *head = NULL;

NODE *newNodeF(int key)
{
	NODE *temp = (NODE *)malloc(sizeof(NODE));
	temp->data = key;
	temp->next = NULL;
	return temp;
}

void createList()
{
	NODE *temp, *newNode = NULL;
	int n, data;
	printf("Enter number of elements	");
	scanf("%d", &n);
	printf("Enter elements\n");
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &data);
		newNode = newNodeF(data);
		if (head == NULL)
		{
			head = newNode;
		}
		else
		{
			temp = head;
			while (temp->next != NULL)
			{
				temp = temp->next;
			}
			temp->next = newNode;
		}
	}
}

void printList()
{
	NODE *temp = head;
	if (temp == NULL)
	{
		printf("List is empty!\n");
		return;
	}
	printf("Linked List is\t");
	while (temp != NULL)
	{
		printf("%d--> ", temp->data);
		temp = temp->next;
	}
}

int main()
{
	printList();
	createList();
	printList();
	printf("NULL\n");
	return 0;
}


$ gcc ll.c
$ ./a.out
List is empty!
Enter number of elements 5
Enter elements
1
2
3
4
5
Linked List is 1–> 2–> 3–> 4–> 5–> NULL
$

For source code visit createLL.c

also see

C Programming language
Go Programming language
Linked List Array
Stack Queue
Puzzle Reasoning
Aptitude HTML
Previous articleLinked List Traversal
Next articleLinked List Insertion Set 1

LEAVE A REPLY

Please enter your comment!
Please enter your name here