Skip to content

Instantly share code, notes, and snippets.

@tanaykumarbera
Last active May 15, 2019 14:01
Show Gist options
  • Save tanaykumarbera/550cde1f85f99165cad2 to your computer and use it in GitHub Desktop.
Save tanaykumarbera/550cde1f85f99165cad2 to your computer and use it in GitHub Desktop.
Linked List

###Linked List - A heir of List :: Linear Data Structure, Abstract in nature

A Data Structure formed by group of nodes, each containing some data attribute along with a reference to another node of similar kind. A node is an elementary block of linked-list and is dynamically allocated. Thus unlike arrays, linked-list have no fixed length and is only restricted to amount of memory pool available to the system (which of-course is virtually unlimited in modern days system).

####Some Assumptions

  • The variable START always points to the start of the list, i.e. First Element.
  • The method new_node allocates a memory space for a new node.
  • The attribute next of a node holds a reference(link) to another node.

Since abstract, the implementation can vary and so can the algorithm. One provided here is not meant for efficiency, but for basic implementation.

####Linked List :: Insertion

INSERT_AT_FIRST(LINKED_LIST p, element_to_insert):

	IF START == NULL:

		/* There's no element in the list */
		START = new_node()
		START.data = element_to_insert
		START.next = NULL /* there's no more node after this */

	ELSE

		/* In case there are elements in the list */
		q = new_node()
		q.data = element_to_insert

		/* append the existing list to this node */
		q.next = START
		/* update starting node */
		START = q

	END IF
END INSERT_AT_FIRST


INSERT_AT_LAST(LINKED_LIST START, element_to_insert):

	IF START == NULL:

		/* There's no element in the list */
		START = new_node()
		START.data = element_to_insert
		START.next = NULL /* there's no more node after this */

	ELSE

		/* In case there are elements in the list */
		q = START

		/* Traverse to the last element of the list */
		WHILE q.next != NULL :
			q = q.next
		END WHILE
		
		q.next = new_elements() /* allocate a space */
		q = q.next /* Move to new node */

		q.data = element_to_insert
		q.next = NULL /* there's no more node after this */

	END IF
END INSERT_AT_LAST


INSERT_AT_POSITION(LINKED_LIST p, element_to_insert, position):

	IF START == NULL OR position == 1:

		p = START
		/* If there are no previous element, p will contain NULL */
		/* If there are some, then p will be holding their initial address */

		START = new_node()
		START.data = element_to_insert
		START.next = p /* append the content of p */

	ELSE

		p = START
		i = 1

		WHILE p != NULL && i < position :
			q = p
			p = p.next
			i = i + 1
		END WHILE

		IF p == NULL AND i < position:
			PRINT "Invalid index. Current List size: " + (i - 1)
		ELSE

			r = new_node()
			r.data = element_to_insert

			q.next = r /* append this node to the node at position - 1 */
			r.next = p /* store the rest of the list(after position) in new node's next */

		END IF
	END IF
END INSERT_AT_POSITION

####Linked List :: Deletion

DELETE_FROM_FIRST(LINKED_LIST START):

	IF START != NULL:
		
		p = START /* save the node to delete */
		element_deleted = p.data
		START = p.next /* update starting node */
		FREE(p) /* deallocates the assigned memory space */

		RETURN element_deleted

	ELSE
		/* List's empty. No elements to delete. */
		PRINT "Underflow"
	END IF
END DELETE_FROM_FIRST

DELETE_FROM_LAST(LINKED_LIST START):

	IF START != NULL:
	
		IF START.next == NULL:
			/* Only element in the list */
			
			element_deleted = START.data
			FREE(START) /* deallocates the assigned memory space */
			START = NULL

		ELSE
			p = START 

			WHILE p.next != NULL:
				q = p
				p = p.next
			END WHILE
			/* At the end of loop, p will hold the last node */
			/* and q will be holding the node prior to the last node */

			element_deleted = p.data
			q.next = NULL /* de-link the last node */
			FREE(p) /* deallocates the assigned memory space */

		END IF
		RETURN element_deleted

	ELSE
		/* List's empty. No elements to delete. */
		PRINT "Underflow"
	END IF
END DELETE_FROM_LAST


DELETE_FROM_POSITION(LINKED_LIST START, position):

	IF START != NULL:
		
		p = START 

		IF position == 1:
			START = START.next
		ELSE

			i = 1

			WHILE p.next != NULL && i < position:
				q = p
				p = p.next
				i = i + 1;
			END WHILE
			/* After the loop, p will hold the node at position defined */
			/* and q will be holding the node prior to the node p, i.e. position - 1 */

			IF p.next == NULL AND i < position:
				PRINT "Invalid index. Current list size: " + i
			ELSE

				q.next = p.next /* de-link this node from the list */
			END IF
		END IF

		element_deleted = p.data
		FREE(p) /* deallocates the assigned memory space */
		RETURN element_deleted

	ELSE
		/* List's empty. No elements to delete. */
		PRINT "Underflow"
	END IF
END DELETE_FROM_POSITION

####Linked List :: Traversal - visiting each elements individually

TRAVERSE(LINKED_LIST START):
	IF START != NULL :
		/* We can easily iterate over the nodes to access them */
		FOR p = START TO p.next != NULL:
			PRINT node p
		END FOR
	ELSE
		/* List's empty. No elements to visit. */
		PRINT "No elements in the list"
	END IF
END TRAVERSE

####Linked List :: Reversal - Reverse the order of list

REVERSE(LINKED_LIST START):

	IF START != NULL :
		
		p = START
		r = NULL

		WHILE p.next != NULL :
			r = q
			q = p
			p = p.next
			q.next = r
		END WHILE

		START = p
	ELSE
		/* List's empty. No elements to reverse. */
		PRINT "No elements in the list"
	END IF
END REVERSE

For most of the operations here, the complexity will be in order of O(k) [0 < k <= n], where n denotes the Stack size during the concerned moment. Specifically,

  • INSERT_AT_LAST, DELETE_FROM_LAST, TRAVERSE and REVERSE will be strictly of O(n)
  • On a sound condition, INSERT_AT_FIRST and DELETE_FROM_FIRST should be of O(1)
  • while INSERT_AT_POSITION, DELETE_FROM_POSITION will be of O(k) [k is the position, 0 < k <= n].
/*
Linked List
dynamic memory allocation
** for TURBO C, remove // from commented statements
*/
#include<stdio.h>
#include<malloc.h>
//#include<conio.h>
struct node{
int data;
struct node *next;
};
typedef struct node LINKED_LIST;
LINKED_LIST * new_node(){
return (LINKED_LIST *) malloc(sizeof(LINKED_LIST));
}
/* LINKED LIST OPERATIONS -------------------------------------------*/
void INSERT_AT_FIRST(LINKED_LIST **START, int element_to_insert){
LINKED_LIST *q;
if(*START == NULL){
*START = new_node();
(*START)->data = element_to_insert;
(*START)->next = NULL;
}else{
q = new_node();
q->data = element_to_insert;
q->next = *START;
*START = q;
}
}
void INSERT_AT_LAST(LINKED_LIST **START, int element_to_insert){
LINKED_LIST *q;
if(*START == NULL){
*START = new_node();
(*START)->data = element_to_insert;
(*START)->next = NULL;
}else{
q = *START;
while(q->next != NULL){
q = q->next;
}
q->next = new_node();
q = q->next;
q->data = element_to_insert;
q->next = NULL;
}
}
void INSERT_AT_POSITION(LINKED_LIST **START, int element_to_insert, int position){
LINKED_LIST *p, *q, *r;
int i;
if(*START == NULL || position == 1){
p = *START;
*START = new_node();
(*START)->data = element_to_insert;
(*START)->next = p;
}else{
p = *START;
i = 1;
while(p != NULL && i < position){
q = p;
p = p->next;
i = i + 1;
}
if(p == NULL && i < position){
printf("Invalid index. Current size: %d. Current limit: %d", i-1, i);
}else{
r = new_node();
r->data = element_to_insert;
q->next = r;
r->next = p;
}
}
}
int DELETE_FROM_FIRST(LINKED_LIST **START){
LINKED_LIST *p;
int element_deleted;
if(*START != NULL){
p = *START;
element_deleted = p->data;
*START = p->next;
free(p);
return element_deleted;
}else{
printf("Underflow");
}
return -1;
}
int DELETE_FROM_LAST(LINKED_LIST **START){
LINKED_LIST *p, *q;
int element_deleted;
if(*START != NULL){
if((*START)->next == NULL){
element_deleted = (*START)->data;
free(*START);
(*START) = NULL;
}else{
p = *START;
while(p->next != NULL){
q = p;
p = p->next;
}
element_deleted = p->data;
q->next = NULL;
free(p);
}
return element_deleted;
}else{
printf("Underflow");
}
return -1;
}
int DELETE_FROM_POSITION(LINKED_LIST **START, int position){
LINKED_LIST *p, *q;
int i, element_deleted;
if(*START != NULL){
p = *START;
if(position == 1){
*START = (*START)->next;
}else{
i = 1;
while(p->next != NULL && i < position){
q = p;
p = p->next;
i = i + 1;
}
if(p->next == NULL && i < position){
printf("Invalid index. Current list size: %d", i);
return -1;
}else{
q->next = p->next;
}
}
element_deleted = p->data;
free(p);
return element_deleted;
}else{
printf("Underflow");
}
return -1;
}
void TRAVERSE(LINKED_LIST *START){
LINKED_LIST *p;
if(START != NULL){
for(p = START; p != NULL; p = p->next){
printf(" %d ->", p->data);
}
printf(" X");
}else{
printf("No elements in the list");
}
}
void REVERSE(LINKED_LIST **START){
LINKED_LIST *p, *q, *r;
if(*START != NULL){
p = q = *START;
r = NULL;
while(p != NULL){
p = p->next;
q->next = r;
r = q;
q = p;
}
*START = r;
}else{
printf("No elements in the list");
}
}
/* ------------------------------------------------------------------ */
int main(){
int ch = 0, element, pos;
LINKED_LIST *START = NULL;
//clrscr();
do{
printf("\n\nAvailable Operations:");
printf("\nINSERTION\n\t1. INSERT AT BEGINING\n\t2. INSERT AT END\n\t3. INSERT AT SPECIFIC POSITION");
printf("\nDELETION\n\t4. DELETE FROM BEGINING\n\t5. DELETE FROM END\n\t6. DELETE FROM SPECIFIC POSITION");
printf("\nOTHERS\n\t7. REVERSE LIST\n\t8. TRAVESE LIST\n\t9. QUIT");
printf("\nEnter your choice (1 - 9): "); scanf("%d", &ch);
switch(ch){
case 1:
printf("\nEnter an element to insert: "); scanf("%d", &element);
INSERT_AT_FIRST(&START, element);
printf("\nLIST STATUS :: \n\n\t"); TRAVERSE(START);
break;
case 2:
printf("\nEnter an element to insert: "); scanf("%d", &element);
INSERT_AT_LAST(&START, element);
printf("\nLIST STATUS :: \n\n\t"); TRAVERSE(START);
break;
case 3:
printf("\nEnter an element to insert: "); scanf("%d", &element);
printf("\nInsert in position? : "); scanf("%d", &pos);
INSERT_AT_POSITION(&START, element, pos);
printf("\nLIST STATUS :: \n\n\t"); TRAVERSE(START);
break;
case 4:
element = DELETE_FROM_FIRST(&START);
if(element != -1) printf("\nDeleted %d", element);
printf("\nLIST STATUS :: \n\n\t"); TRAVERSE(START);
break;
case 5:
element = DELETE_FROM_LAST(&START);
if(element != -1) printf("\nDeleted %d", element);
printf("\nLIST STATUS :: \n\n\t"); TRAVERSE(START);
break;
case 6:
printf("\nEnter position to delete from: "); scanf("%d", &pos);
element = DELETE_FROM_POSITION(&START, pos);
if(element != -1) printf("\nDeleted %d", element);
printf("\nLIST STATUS :: \n\n\t"); TRAVERSE(START);
break;
case 7:
printf("\nCURRENT STATUS :: \n\n\t"); TRAVERSE(START);
REVERSE(&START);
printf("\nAFTER REVERSE :: \n\n\t"); TRAVERSE(START);
break;
case 8:
printf("\nLIST TRAVERSAL :: \n\n\t"); TRAVERSE(START);
break;
case 9: break;
default:
printf("\nInvalid options! Choose between [1,2,3,4,5,6,7,8,9]");
}
}while(ch > 0 && ch < 9);
printf("\n");
//getch();
return 0;
}
/*
Linked List
dynamic memory allocation
Save File as LinkedMain.java
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/* LINKED LIST OPERATIONS -------------------------------------------*/
class LINKED_LIST{
/* Node defination and related methods */
int data;
LINKED_LIST next;
public static LINKED_LIST START;
public static void INSERT_AT_FIRST(int element_to_insert){
LINKED_LIST q = null;
if(START == null){
START = new LINKED_LIST();
START.data = element_to_insert;
START.next = null;
}else{
q = new LINKED_LIST();
q.data = element_to_insert;
q.next = START;
START = q;
}
}
public static void INSERT_AT_LAST(int element_to_insert){
LINKED_LIST q = null;
if(START == null){
START = new LINKED_LIST();
START.data = element_to_insert;
START.next = null;
}else{
q = START;
while(q.next != null){
q = q.next;
}
q.next = new LINKED_LIST();
q = q.next;
q.data = element_to_insert;
q.next = null;
}
}
public static void INSERT_AT_POSITION(int element_to_insert, int position){
LINKED_LIST p = null, q = null, r = null;
int i;
if(START == null || position == 1){
p = START;
START = new LINKED_LIST();
START.data = element_to_insert;
START.next = p;
}else{
p = START;
i = 1;
while(p != null && i < position){
q = p;
p = p.next;
i = i + 1;
}
if(p == null && i < position){
System.out.print("Invalid index. Current size: " + (i-1) + ". Current limit: " + i);
}else{
r = new LINKED_LIST();
r.data = element_to_insert;
q.next = r;
r.next = p;
}
}
}
public static int DELETE_FROM_FIRST(){
LINKED_LIST p = null;
int element_deleted;
if(START != null){
p = START;
element_deleted = p.data;
START = p.next;
p = null; //free(p);
return element_deleted;
}else{
System.out.print("Underflow");
}
return -1;
}
public static int DELETE_FROM_LAST(){
LINKED_LIST p = null, q = null;
int element_deleted;
if(START != null){
if(START.next == null){
element_deleted = START.data;
START = null; //free(START);
}else{
p = START;
while(p.next != null){
q = p;
p = p.next;
}
element_deleted = p.data;
q.next = null;
p = null; //free(p);
}
return element_deleted;
}else{
System.out.print("Underflow");
}
return -1;
}
public static int DELETE_FROM_POSITION(int position){
LINKED_LIST p = null, q = null;
int i, element_deleted;
if(START != null){
p = START;
if(position == 1){
START = START.next;
}else{
i = 1;
while(p.next != null && i < position){
q = p;
p = p.next;
i = i + 1;
}
if(p.next == null && i < position){
System.out.print("Invalid index. Current list size: " + i);
return -1;
}else{
q.next = p.next;
}
}
element_deleted = p.data;
p = null; //free(p);
return element_deleted;
}else{
System.out.print("Underflow");
}
return -1;
}
public static void TRAVERSE(){
LINKED_LIST p = null;
if(START != null){
for(p = START; p != null; p = p.next){
System.out.print(" " + p.data + " ->");
}
System.out.print(" X");
}else{
System.out.print("No elements in the list");
}
}
public static void REVERSE(){
LINKED_LIST p = null, q = null, r = null;
if(START != null){
p = q = START;
r = null;
while(p != null){
p = p.next;
q.next = r;
r = q;
q = p;
}
START = r;
}else{
System.out.print("No elements in the list");
}
}
}
/* ------------------------------------------------------------------ */
public class LinkedMain{
/* Main class for Execution */
public static void main(String s[]) throws NumberFormatException, IOException{
int ch = 0, element, pos;
LINKED_LIST.START = null;
BufferedReader ip = new BufferedReader(new InputStreamReader(System.in));
do{
System.out.print("\n\nAvailable Operations:");
System.out.print("\nINSERTION\n\t1. INSERT AT BEGINING\n\t2. INSERT AT END\n\t3. INSERT AT SPECIFIC POSITION");
System.out.print("\nDELETION\n\t4. DELETE FROM BEGINING\n\t5. DELETE FROM END\n\t6. DELETE FROM SPECIFIC POSITION");
System.out.print("\nOTHERS\n\t7. REVERSE LIST\n\t8. TRAVESE LIST\n\t9. QUIT");
System.out.print("\nEnter your choice (1 - 9): "); ch = Integer.parseInt(ip.readLine());
switch(ch){
case 1:
System.out.print("\nEnter an element to insert: "); element = Integer.parseInt(ip.readLine());
LINKED_LIST.INSERT_AT_FIRST(element);
System.out.print("\nLIST STATUS :: \n\n\t"); LINKED_LIST.TRAVERSE();
break;
case 2:
System.out.print("\nEnter an element to insert: "); element = Integer.parseInt(ip.readLine());
LINKED_LIST.INSERT_AT_LAST(element);
System.out.print("\nLIST STATUS :: \n\n\t"); LINKED_LIST.TRAVERSE();
break;
case 3:
System.out.print("\nEnter an element to insert: "); element = Integer.parseInt(ip.readLine());
System.out.print("\nInsert in position? : "); pos = Integer.parseInt(ip.readLine());
LINKED_LIST.INSERT_AT_POSITION(element, pos);
System.out.print("\nLIST STATUS :: \n\n\t"); LINKED_LIST.TRAVERSE();
break;
case 4:
element = LINKED_LIST.DELETE_FROM_FIRST();
if(element != -1) System.out.print("\nDeleted " + element);
System.out.print("\nLIST STATUS :: \n\n\t"); LINKED_LIST.TRAVERSE();
break;
case 5:
element = LINKED_LIST.DELETE_FROM_LAST();
if(element != -1) System.out.print("\nDeleted " + element);
System.out.print("\nLIST STATUS :: \n\n\t"); LINKED_LIST.TRAVERSE();
break;
case 6:
System.out.print("\nEnter position to delete from: "); pos = Integer.parseInt(ip.readLine());
element = LINKED_LIST.DELETE_FROM_POSITION(pos);
if(element != -1) System.out.print("\nDeleted " + element);
System.out.print("\nLIST STATUS :: \n\n\t"); LINKED_LIST.TRAVERSE();
break;
case 7:
System.out.print("\nCURRENT STATUS :: \n\n\t"); LINKED_LIST.TRAVERSE();
LINKED_LIST.REVERSE();
System.out.print("\nAFTER REVERSE :: \n\n\t"); LINKED_LIST.TRAVERSE();
break;
case 8:
System.out.print("\nLIST TRAVERSAL :: \n\n\t"); LINKED_LIST.TRAVERSE();
break;
case 9: break;
default:
System.out.print("\nInvalid options! Choose between [1,2,3,4,5,6,7,8,9]");
}
}while(ch > 0 && ch < 9);
System.out.print("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment