Creating source Files List.cpp
#include "List.h" int main () {Test (); System ("pause"); return 0;}
Creating a header file List.h
#ifndef __LIST_H__#define __LIST_H__#include<stdio.h> #include <assert.h> #include < Stdlib.h>typedef int datatype;typedef struct listnode{ datatype data; struct listnode* next;} listnode;//Create node Listnode* _buynode (datatype x) { listnode* tmp = (listnode*) malloc (sizeof (ListNode)); tmp->data = x; tmp->next = null; return tmp;} Tail plug Void pushback (listnode* & head, datatype x) { //0 node not 0-node /*assert (head);*/ if (head == null) { head = _buynode (x); head->next = null; } else { listnode* newnode = _buynode (x); ListNode* cur = head; while ( Cur->next) { cur = cur->next; } ListNode* tail = cur; tail->next = newNode; tail = newnode; tail->next = null; }}//tail Void popback (listnode* & head) { //0 node, 1 node, multi-node if (head == null) { return; } ListNode* cur = head; while (cur) { if (Cur->next == null) &NBSP;&NBSP;&NBSP;&NBSP;//1 node status { delete cur; cur = NULL; head = NULL; } else { listnode* next = cur->next; if (next->next == null) { ListNode* tail = next; free (tail); tail = NULL; tail = cur ; tail->next = NULL; break; } cur = cur->next; } }}//Head plug Void pushfront (listnode* & head, datatype x) { //0 node 1 node, multi-node if (head == null) //0 node { pushback (head,x); } else { ListNode* cur = head; listnode* newnode = _buynode (x); newNode->next = head; head = newnode; }}//Head Delete Void popfront ( Listnode* & head) { //0 node 1 node Multi-node if (Head == null) //0 node { popback (head); } else if ((head != null) && (head->next == null)) { free (head); head = NULL; } else { ListNode* del = head; ListNode* cur = head->next; head = cur; free (del); del = null; }}//Delete node Void erase (ListNode* & Head, listnode* pos) { assert (POS); if (pos = = head) { at the first node of // popfront (head); } else { ListNode* cur = head; while (cur) { listnode* next = cur- >next; ListNode* nextnext = next->next; if (Cur->next == null) { at //tail node popback (cur); break; } else if (Next == pos) //at Middle node { cur->next = nextnext; free (next); next = NULL; break; } cur = cur->next; } }}//Find node Listnode* find (listnode* & head, datatype x) { listnode* cur = head; while (cur) { if (cur->data == x) { return cur; } cur = cur->next; } return null;} Print node Void printlist (listnode* & head) { listnode* cur = head; while (cur) { printf ("%d->", cur->data); cur = cur->next; } printf ("NULL\n");} Void test () { listnode* s = null; pushback (s), 1); pushback (s, 2); pushback (s, 3); pushback (s, 4); pushback (S,&NBSP;5); printlist (s); popback (s); Printlist (s); pushfront (s, 0); printlist (s); popfront (s); printlist (s); listnode* p = s->next; erase (s, p); printlist (s); listnode* ret = find (s, 3); printf ("%d\n", ret- >data);} #endif //__list_h__
This article is from the "C language 100-200 Prime" blog, please be sure to keep this source http://10740184.blog.51cto.com/10730184/1747913
"C" implements all operations of single-linked list in C language