Ds Linked List
Ds Linked List
A linked list is a sequence of data structures, which are connected together via links. Linked List is a sequence
of links which contains items. Each link contains a connection to another link. Linked list is the second most-
used data structure after array.
Implementation in C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
};
printf(" ]");
}
link->key = key;
link->data = data;
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 1/5
14/09/2017 Linked List Program in C
int length() {
int length = 0;
struct node *current;
return length;
}
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 2/5
14/09/2017 Linked List Program in C
return current;
}
void sort() {
tempKey = current->key;
current->key = next->key;
next->key = tempKey;
}
current = current->next;
next = next->next;
}
}
}
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 3/5
14/09/2017 Linked List Program in C
*head_ref = prev;
}
main() {
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
//print list
printList();
while(!isEmpty()) {
struct node *temp = deleteFirst();
printf("\nDeleted value:");
printf("(%d,%d) ",temp->key,temp->data);
}
if(foundLink != NULL) {
printf("Element found: ");
printf("(%d,%d) ",foundLink->key,foundLink->data);
printf("\n");
} else {
printf("Element not found.");
}
delete(4);
printf("List after deleting an item: ");
printList();
printf("\n");
foundLink = find(4);
if(foundLink != NULL) {
printf("Element found: ");
printf("(%d,%d) ",foundLink->key,foundLink->data);
printf("\n");
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 4/5
14/09/2017 Linked List Program in C
} else {
printf("Element not found.");
}
printf("\n");
sort();
reverse(&head);
printf("\nList after reversing the data: ");
printList();
}
If we compile and run the above program, it will produce the following result
Output
Original List:
[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]
Deleted value:(6,56)
Deleted value:(5,40)
Deleted value:(4,1)
Deleted value:(3,30)
Deleted value:(2,20)
Deleted value:(1,10)
List after deleting all items:
[ ]
Restored List:
[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]
Element found: (4,1)
List after deleting an item:
[ (6,56) (5,40) (3,30) (2,20) (1,10) ]
Element not found.
List after sorting the data:
[ (1,10) (2,20) (3,30) (5,40) (6,56) ]
List after reversing the data:
[ (6,56) (5,40) (3,30) (2,20) (1,10) ]
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 5/5