Yes, a linked list can be implemented in an array. Instead of pointers (or references in JAVA) you can use element indexes in the "next" and "last" items.
This does not stop you from using pointers or references. They still work. You can take the address of an element of an array, such as &a[217], and place that in the .next or .last pointers, and manage the relationships in the normal way. Using an array is simply one way to manage memory.
The following contains a simple singly-linked list C implementation, with usage examples in the main function. While I believe this implementation to be memory-leak-free, it ispossible that I have missed something.
#include // for mem functions
#include // for example io
#ifndef NULL
#define NULL 0L
#endif
// struct defs
struct linked_list_node {
int data;
struct linked_list_node *next;
};
struct linked_list {
int size;
struct linked_list_node *root;
};
// deletes all nodes starting at node
void deleteNodes(struct linked_list_node *node) {
if( node != NULL ) {
// deallocate next, if it exists
if( node->next != NULL ) {
deleteNodes(node->next);
// lose the pointer
node->next = NULL;
}
// deallocate node
free(node);
}
}
// deletes the given linked list
void deleteLinkedList(struct linked_list *list) {
if( list != NULL ) {
// delete nodes
deleteNodes(list->root);
// lose the pointer
list->root = NULL;
// delete actual list
free(list);
}
}
// returns a pointer to a new linked list node with the given data
struct linked_list_node* createLinkedListNode(const int data) {
// allocate memory for new node
struct linked_list_node *newNode = malloc(sizeof(struct linked_list_node));
// initialize
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// adds a new node to the end of list with the given data
void addNode(struct linked_list *list, const int data) {
if( list != NULL ) {
if( list->root == NULL ) {
// with an empty list, replace root
list->root = createLinkedListNode(data);
}else {
// find last node to append to
struct linked_list_node *current = list->root;
while(current->next != NULL) {
current = current->next;
}
current->next = createLinkedListNode(data);
}
++(list->size);
}
}
// prints list to stdout
void printLinkedList(const struct linked_list *list) {
if( list == NULL ) {
printf("null list\n");
}else {
printf("%d:[", list->size);
if( list->root != NULL ) {
struct linked_list_node *current = list->root;
printf("%d", current->data);
while( current->next != NULL ) {
current = current->next;
printf(", %d", current->data);
}
}
printf("]\n");
}
}
// returns a pointer to a new linked list
struct linked_list* createLinkedList() {
// allocate memory for new list
struct linked_list *newList = malloc(sizeof(struct linked_list));
// initialize
newList->size = 0;
newList->root = NULL;
return newList;
}
// returns the data located at node i
// returns -1 if list is null/empty or if i is outside the bounds of list
int getDataAt(const struct linked_list *list, const int i) {
if( list 0) (i < 0) (i >= list->size) ) {
return -1;
}
struct linked_list_node *current = list->root;
int currentIndex;
// move to list[i]
for(currentIndex = 0; currentIndex < i; ++currentIndex) {
current = current->next;
}
return current->data;
}
// inserts a node at position i with the given data
// does nothing if list is null or if i is outside the bounds of list
void insertNode(struct linked_list *list, const int i, const int data) {
if( list 0 ) {
// create new node
struct linked_list_node *newNode = createLinkedListNode(data);
newNode->next = list->root;
list->root = newNode;
++(list->size);
return;
}
struct linked_list_node *current = list->root;
int currentIndex;
// move to list[i]
for(currentIndex = 0; currentIndex < i; ++currentIndex) {
current = current->next;
}
// create new node
struct linked_list_node *newNode = createLinkedListNode(data);
newNode->next = current->next;
current->next = newNode;
++(list->size);
}
// removes the node at position i and returns the data from that node
// returns -1 if list is null/empty or if i is outside the bounds of list
int removeNode(struct linked_list *list, const int i) {
if( list 0 ) {
// store data
const int data = list->root->data;
struct linked_list_node *temp = list->root;
// cut out the root
list->root = list->root->next;
// cleanup
temp->next = NULL;
deleteNodes(temp);
--(list->size);
return data;
}
struct linked_list_node *current = list->root;
int currentIndex;
// get last node before the specified one
for(currentIndex = 1; currentIndex < i; ++currentIndex) {
current = current->next;
}
// store data
const int data = current->next->data;
struct linked_list_node *temp = current->next;
// cut out the node
current->next = current->next->next;
// cleanup
temp->next = NULL;
deleteNodes(temp);
--(list->size);
return data;
}
// example of use
int main(int argc, char** argv) {
// create a new linked list
struct linked_list *list = createLinkedList();
// print out the empty list
printLinkedList(list);
// fill it with some numbers, printing as we go
int i;
for( i = 0; i < 10; ++i ) {
addNode(list, i);
printLinkedList(list);
}printf("\n");
// get some data
printf("list[0] = %2d\n", getDataAt(list, 0));
printf("list[5] = %2d\n", getDataAt(list, 5));
printf("list[9] = %2d\n", getDataAt(list, 9));
printf("list[10] = %2d\n", getDataAt(list, 10));
printf("list[11] = %2d\n", getDataAt(list, 11));
printf("list[-1] = %2d\n", getDataAt(list, -1));
printf("\n");
// remove some data
printf("remove[9]: %2d\t", removeNode(list, 9));
printLinkedList(list);
printf("remove[5]: %2d\t", removeNode(list, 5));
printLinkedList(list);
printf("remove[0]: %2d\t", removeNode(list, 0));
printLinkedList(list);
printf("remove[11]:%2d\t", removeNode(list, 11));
printLinkedList(list);
printf("remove[-1]:%2d\t", removeNode(list, -1));
printLinkedList(list);
printf("\n");
// insert some data
insertNode(list, 3, 5);
printf("\t\t"); printLinkedList(list);
insertNode(list, 0, 0);
printf("\t\t"); printLinkedList(list);
printf("\n");
// dealloc all memory assigned to list
deleteLinkedList(list);
list = NULL; // REMEMBER: lose our pointer so we don't try to use it again by accident
// print null list
printLinkedList(list);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct Details{
int psno;
char name[20];
int marks;
struct Details *ptr;
};
typedef struct Details Node;
Node *createNode();
void add();
void disp();
Node *start= NULL;
int main()
{
int conf;
Node *createNode();
do {
printf("enter details psno, name and marks: \n");
add();
printf("do u want to continue (y/n=0)");
scanf("%d",&conf);
}while(conf);
disp();
}
Node *createnode() {
Node *New;
New=malloc(sizeof(Node));
printf("enter details psno, name and marks: \n ");
scanf("%d %s %d",&New->psno,New->psno,&New->marks);
New->ptr=NULL;
return New;
}
void add(){
Node *New;
New=createNode();
if(start==NULL)
start=New;
else{
New->ptr=start;
start=New;
}
}
void disp()
{
Node *temp;
temp=start;
//printf("%d %s %d",psno,name,marks);
while(temp){
printf("%d %s %d",temp->psno,temp->psno,temp->marks);
temp=temp->ptr;
}
}
The only built-in data structure in C is the array, but this has a size that is fixed when you declare it, so it won't grow to accommodate new data.
A linked list can grow as needed and so is ideal when you don't know in advance how much data you are going to store.
It has nothing to do with C: it is a list implemented with pointers.
link list is a method in c language to store dinamic value in computer system.
In C programming, a double linked-list refers to a linked data structure that contains a set of links that have been linked sequentially.
The enum keyword means enumeration.
Which of the following data structures can be randomly accessed giving loc?A. linked list implemented using arrayB. singly linked listC. double linked listD. both single and double linked listThe answer is A.
void pointer
Use a linked-list.
In C programming, a double linked-list refers to a linked data structure that contains a set of links that have been linked sequentially.
For programming only in ASM is too hard and the OS is in fact at some places that can be programmed by C linked to ASM.
The enum keyword means enumeration.
It would be a list of five programming languages.
Not in C, no.
sorry
A linked list is a collection of items, often nodes, that are sequentially linked by some kind of index or pointer contained within each item.
Which of the following data structures can be randomly accessed giving loc?A. linked list implemented using arrayB. singly linked listC. double linked listD. both single and double linked listThe answer is A.
"the" is not a keyword in the C Programming Language. Perhaps you meant "const" HTH Richard Wolf Software Architect
#include<iostream.h>
Because the C programming language leaves the responsibility for memory allocation and pointers entirely with the programmer, the disadvantage of linked lists over some other linear data structures (such as arrays) is that the bear a risk or memory leaks and invalid pointers. The fact that the size of a linked list is generally not deterministic is also commonly viewed a disadvantage over statically linked linear containers (e.g. arrays) in some systems, primarily in embedded systems. Compared to containers of higher order (such as trees or hash tables), search operations in a linked list are generally slower. Compared to a double linked list, removal and insertion of items (except head and tail) is generally more expensive.
loops execute a set of insructions repeatedly for a certain numbers of times..