List------Linked 链表
1、Definition
Linked list consists of a series of nodes. Each nodes contains the element and a pointer which points to the next node. The last node's next link points to NULL.
Linked=data+pointer
?
use the pointer to describe the logical relationship
?
?
2、Implementation
template<class List_entry> class List {public:List(); int size();bool empty();bool full();...protected:int count;Node<List_entry> *head; //當(dāng)聲明一個linked時,只會擁有一個頭指針 }template<class List_entry> struct Node {List_entry entry; // dataNode<List_entry> *next; // point to the next node Node();Node(List_entry, Node<List_entry>*link=NULL); }3、Operations
(1)two types of insert
①在p節(jié)點所在位置插入
New(S)S->data=a;q->next=S;S->next=p;②在p節(jié)點之后插入
New(S)S->data=a;S->next=p->next;p->next=S;(2) create a linked list
In order to create a linked list, we have this algorithm
①To create a head
②To create new node S
③Insert S
?
?
void CreateList(Head) {new(Head);Head->next=NULL;scanf("%c",ch);while(ch<>'#')do{new(S);S->data=ch; S->next=Head->next;Head->next=S;} }上述構(gòu)造方式為從頭構(gòu)造,在頭元素出一個一個地插入
下面一種是從鏈尾增添
void CreateList(Head) {new(Head);Head->next=NULL;Last=Head;scanf("%c",ch);while(ch<>'#')do{new(S);S->data=ch;S->next=NULL;Last->next=S;Last=S;} }(3)insert an element at location i
Status ListInsert L(LinkList &L, int i, DataType e) {LinkList p, s;p=L;int j=0;while(p&&j<i-1){p=p->next;j++;}if(!p) return ERROR;s=(LinkList)malloc(sizeof(LNode));s->data=e;s->next=p->next;p->next=s;return OK; }(4)Delete
Status ListDelete(LinkList &L, int i) {LinkList p, q;p=L;int j=0;while(p&&j<i-1){p=p->next;j++;}if(!p) return ERROR;q=p->next;p->next=q->next;free(q); }(5)Search
①按位查找
Status ListSearch(LinkList &L, int i) {LinkList p;int j=0;while(p&&j<i-1){p=p->next; j++;}if(!p)return ERROR;e=p->next;return OK; }③按值查找
Status ListSearch(LinkList &L, DataType e) {LinkList p;p=L;int j=0;while(p&&(p->next)!=e){p=p->next;j++;}if(!p){cout<<"Not found"<<endl;return ERROR;} else{return (j);} }3、circular linked list
4、Doubly linked list
(1)Insert
p->next=current->next; p->prior=current; current->next->prior=p; current->next=p;(2)Delete
current->next->prior=current->prior; current->prior->next=current->next;?
C++style code
//C++style構(gòu)建鏈表,有頭結(jié)點 //operations /* LenList(L)鏈長 GetElem(i, e)換值 SearchElem(e, i)按值查找 InertElem(i, e)插值 DeleteElem(i) 刪值 */template<typename Type> struct Node {Type data;Node<Type>*next; };template<typename Type> class LinkList { public:LinkList(){head = new Node<Type>;head->next = NULL;len = 0;Type ch;Node<Type>*p;while (cin >> ch){p = new Node<Type>;p->data = ch;p->next = head->next;head->next = p;len++;}}~LinkList(){Node<Type>*p = head;while (p->next){DeleteElem();}delete head;}//LenList(L)鏈長int LenList(){return len;}//InertElem(e)從尾插值void InsertElem(Type e){Node<Type>*p = head;while (p->next){p = p->next;}Node<Type>*q = new Node<Type>;q->data = e;q->next = p->next;p->next = q;len++;}//InertElem(i, e)從第i位插值void InsertElem(int i, Type e){Node<Type>*p = head;int j = 0;while (p->next && j<i - 1){j++;p = p->next;}Node<Type>*q = new Node<Type>;q->data = e;q->next = p->next;p->next = q;len++;}//GetElem(i, e)換值void GetElem(int i, Type e){int j = 0;Node<Type>*p = head;while (p->next && j<i - 1){j++;p = p->next;}p->next->data = e;}//DeleteElem(i) 第i位刪值void DeleteElem(int i){int j = 0;Node<Type>*p = new Node;while (p->next && j<i - 1){j++;p = p->next;}Node<Type>*q = p->next;p->next = q->next;delete q;len--;}//DeleteElem() 尾刪值void DeleteElem(){Node<Type>*p = head;while (p->next->next){p = p->next;}Node<Type>*q = p->next;p->next = q->next;delete q;len--;}//SearchElem(e)按值查找int SearchElem(Type e){Node<Type>*p = head;int i = 0;while (p->next && p->data != e){i++;p = p->next;}if (i == 0)return -1;else return i;}//SearchElem(i)按位查找Type SearchElem(int i){Node<Type>*p = head;int j = 0;while (p->next && j<i){j++;p = p->next;}return p->data;}private:Node<Type>*head;int len; };
?
轉(zhuǎn)載于:https://www.cnblogs.com/KennyRom/p/5879716.html
總結(jié)
以上是生活随笔為你收集整理的List------Linked 链表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring的Hello, world,
- 下一篇: 转Java 理论与实践: 正确使用 Vo