Single Linked List

struct Node

typedef struct Node{
	int v;
	struct Node* next;
}Node;

struct Single Linked List

typedef struct LinkedList{
	Node *head;
	Node *tail;
}LinkedList;
Single Linked List

Function initial

void initial(LinkedList *l){
	l->head = NULL;
	l->tail = NULL;
}

Function isEmpty

bool isEmpty(const LinkedList* l){
	return l->head == NULL;
}
Single Linked List

Function getNode

Node* getNode(int v){
	Node* p = (Node*)malloc(sizeof(Node));
	if (p == NULL){
		exit(0);
	}
	p->v = v;
	p->next = NULL;
	return p;
}
Single Linked List

Function addHead

void addHead(LinkedList *l, Node *p){
	if (isEmpty(l)){
		l->head = l->tail = p;
	}else{
		p->next = l->head;
		l->head = p;
	}	
}
Single Linked List

Function insertHead

void insertHead(LinkedList *l, int v){
	Node* p = getNode(v);
	addHead(l, p);
}
Single Linked List

function traverse

void traverse(const LinkedList *l){
	Node* p = l->head;
	while(p != NULL){
		printf("%d ", p->v);
		p = p->next;
	}
	printf("\n");
}
Single Linked List

function addTail

void addTail(LinkedList *l, Node* p){
	if (isEmpty(l)){
		l->head = l->tail = p;
	}else{
		l->tail->next = p;
		l->tail = p;
	}
}
Single Linked List

function insertTail

void insertTail(LinkedList *l, int v){
	Node *p = getNode(v);
	addTail(l, p);
}
Single Linked List

Function find

Node* find(const LinkedList *l, int v){
	Node* p = l->head;
	while(p != NULL && p->v != v){
		p = p->next;
	}
	return p;
}
Single Linked List

Function addAfter

void addAfter(LinkedList *l, Node* q, Node* p){
	if (q == NULL){
		addTail(l, p);
	}else{
		p->next = q->next;
		q->next = p;
		if (q == l->tail){
			l->tail = p;
		}
	}
}
Single Linked List

Function insertAfter

void insertAfter(LinkedList *l, int u, int v){
	Node* q = find(l, u);
	Node* p = getNode(v);
	addAfter(l, q, p);
}
Single Linked List

function removeHead

Node* removeHead(LinkedList *l){
	if (isEmpty(l)){
		return NULL;
	}
	Node* saved = l->head;
	l->head = saved->next;
	if (saved->next == NULL){
		l->tail = NULL;
	}
	//or
	if (l->head == NULL){
		l->tail = NULL;
	}
	//or
	if (saved == l->tail){
		l->tail = NULL;
	}
	return saved;
}
Single Linked List

Function deleteHead

void deleteHead(LinkedList *l){
	Node* p = removeHead(l);
	if (p != NULL){
		free(p);
	}
}
Single Linked List

function clear

void clear(LinkedList *l){
	Node* p;
	while(l->head != NULL){
		p = l->head;
		l->head = p->next; 
		free(p);
	}
	l->tail = NULL;
}
Single Linked List

Function removeAfter

Node* removeAfter(LinkedList *l, Node *q){
	if (q == NULL){
		return NULL;
	}
	Node *saved = q->next;
	if (saved != NULL){
		q->next = saved->next;
		if (saved->next == NULL){
			l->tail = q;
		}
		//or
		if (saved == l->tail){
			l->tail = q;
		}
	}
	return saved;
}
Single Linked List

Function removeTail

Node* removeTail(LinkedList *l){
	if (isEmpty(l)){
		return NULL;
	}
	Node* saved = l->tail;
	if (l->head == l->tail){
		initial(l);
	}else{
		Node* q = l->head;
		while(q->next != l->tail){
			q = q->next;
		}
		q->next = NULL;
		l->tail = q;
	}
	return saved;
}
Single Linked List

Function deleteTail


		
Single Linked List

Function removeAt


		
Single Linked List

Function deleteAt


		
Single Linked List

Function sort


		
Single Linked List

Function reverse