(DSA) Singly linked list implementation in C programming language.

Ashutosh Kumar
By -
0


Hello guys, 
                  Let have a look.
Singly liked list implementation in c programming language.

I have created this program to learn DSA (Data Structure and Algorithm). 

For now:
I have create only 3 method. 

void createLinkedList(list *start) :
This Create linked list. Recursively call this function till we don't enter -1111 to stop filling item in linked list.

void traverse(list *start) :
This function outputs all the items of singly liked list.

int count(list *start) :
It return the number of item contain by linked list.

Latter I will add insertion and removal function too.

#include <stdio.h>
#include <stdlib.h>

struct linked_list
{
	int data;
	struct linked_list* next;
};


typedef struct linked_list list;

void createLinkedList(list *start){
	printf("%s\n", "Enter element.    type -1111 to come out of loop." );

	scanf("%d", &start->data);

	if( start->data == -1111 ){
		start->next = NULL;
	}else{

		start->next = (list*) malloc( sizeof(list));
		createLinkedList(start->next);
	}
}

void traverse(list *start){
	if(start->next != NULL){
		printf("%d --> ", start->data);
		traverse(start->next);
	}
}

int count(list *start){

	
	if(start->next == NULL ){
		return 0;
	}else{
		return 1 + count(start->next);
	}
}

int main(int argc, char const *argv[])
{

	list* head;
	// Dynamically locate memory for first element 
	head = (list*) malloc( sizeof(list) ); 
	createLinkedList(head);

	printf("Element count in LikedList: %d\n", count(head));

	// traverse
	traverse(head);


	
	return 0;
}

This c program is created for learning the the DSA (Data Structure and Algorithm). I have already build a program that list ADT implement with Array. 

You may read this also:


Post a Comment

0Comments

✍️ Only article/post centric comments are allowed.
❌**No Spam**
❌**Advertisement**
❌**No abuse**
Rules strictly applied ✅

Post a Comment (0)