List implementation in Array Data Structure - C Programming Language

Ashutosh Kumar
By -
0

Let's create list implementation in Array Data Structure. Actually we are improvising our array data structure by applying list interface ADT. so we can use array as list.

Why we need to create such Abstract Data Structure (ADT)?

This is simple implementation of list interface over C Programming Array. Will help Array to  the the insertion and removal like action which is not possible in Array.
We can use this for small set of data. Where the add, update and delete is needed. Like customers of small company.

 
#include <stdio.h>

typedef struct {
 int count;
 int data[10];
} list;


void create(list*);
void insert(list*, int, int);
void traverse(list*);

void main(){

	list l;

	create(&l);

	insert(&l, 2, 100);//list, index, element
	insert(&l, 3, 120);

	traverse(&l);



}



void create(list* start){
	int i = 0, temp = 1;

	while(temp){
		fflush(stdin);
		printf("Number for element: %d (Zero to out list)\n", i  );

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

		if( start->data[i] == 0){
			temp=0; // stop the loop or break;
		}else{
			i++; // next value
		}
	}

	start->count = i;

	printf(":::create function end:::\n");
}

void insert(list* start, int position, int element){
	puts("Insert element: ");
	int temp = start->count, i = position;

	while(temp >= position ){

		start->data[temp+1] = start->data[temp];
		temp--;
	}

	start->data[position] = element;
	start->count = start->count + 1;

	printf(":::insert function end:::\n");
}

void
traverse(list* start)
{

	puts("List elements: ");
	int temp = 0; 
	while(temp < (start->count) ){

		printf("value for item %d: %d\n",temp, start->data[temp]); 

		temp++;
	}
	printf(":::traverse function end:::\n");

}


For now, I have added three following function:
void create(list*); void insert(list*, int, int); void traverse(list*);

void remove(list* start, int position): I will add soon to this algorithm. Then removal element will be possible.

Thanks

Post a Comment

0Comments

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

Post a Comment (0)