不出意外 ( 指沉迷开船 ) 的话,应该会出教程,以及分享一下心得什么的
索引博客已经写出,详情请见→索引.
node.h: 定义了2个东西,一个是节点 (node) ,一个是头尾指针 (lst)
#include "node.h"
#include <stdio.h>
#include <stdlib.h>
#ifndef _NODE_H_
#define _NODE_H_
typedef struct _node
{
int value;
struct _node *point;
}Node;
typedef struct _list
{
Node* head;
Node* tail;
}List;
#endif
main:
#include "node.h"
#include <stdio.h>
#include <stdlib.h>
void lstCrt(List *lstp);
void lstPrt(const List *lstp);
void lstAdd(List *lstp,const int input);
void lstSrh(const List *lstp,const int input);
void lstDel(List *lstp,const int input);
void lstClr(List *lstp);
int main(void)
{
List lst;
int input = 0;
lstCrt(&lst);
int num = 33;//debug
lstAdd(&lst,num);
lstPrt(&lst);
// printf("%p:%d",lst.head,lst.head->value);//debug
lstSrh(&lst,num);
lstDel(&lst,num);
lstPrt(&lst);
lstClr(&lst);
return 0;
}
lstCrt: 用以创建一个 linked list(也可以直接后面的 lstAdd 一个个添加)
#include "node.h"
#include <stdio.h>
#include <stdlib.h>
void lstCrt(List *lstp)
{
//[1]begin
//preparation:
Node *p = NULL;
lstp->head = (Node*) malloc(sizeof(Node));//allocate new space for the head.(lstp->head is just the point of head)
lstp->head->value = 0;//initialization
lstp->head->point = NULL;//initialization
lstp->tail = lstp->head;//when there is no node, the head of the linked list is right the tail.
printf("A new linkedlist has been created. Please input something(-1 to exit): ");
//[1]end
//[2]begin
//start writing
while(1) {
int input = 0;
scanf("%d",&input);//get a input
if( input == -1) {//check the input
break;
}
p = (Node*) malloc(sizeof(Node));//allocate new space for the next node
lstp->tail->point = p;//link the pre-node's point to the new(next) node
lstp->tail = p;//link the tail to the new node
p->value = input;//write the input into the value of the new(next) node
p->point = NULL;//wait for written into
//[2]end
}
return;
}
lstAdd: 添加一个 node 到 linked list 上
#include "node.h"
#include <stdio.h>
#include <stdlib.h>
void lstAdd(List *lstp,const int input)
{
Node *p = (Node*) malloc(sizeof(Node));
if(lstp->head) {
lstp->tail->point = p;//link the pre-node to this node
}else {
lstp->head->point = p;//link the head to this node
}
lstp->tail = p;//link the tail to this node
p->value = input;//write input to the value of this node
p->point = NULL;
}
lstSrh: 搜索 linked list 中的某一 element 或者说 node 的 值 (value)
#include "node.h"
#include <stdio.h>
#include <stdlib.h>
void lstSrh(const List *lstp,const int input)
{
int isFound = 0;
for(Node*p=lstp->head->point;p;p=p->point) {//traverse the whole linked list
if(p->value == input)
{
isFound = 1;
printf("Found!\n");
break;
}
}
if(!isFound) {
printf("Sorry! %d not found...\n",input);
}
return;
}
lstDel: 删除某一个 node
#include "node.h"
#include <stdio.h>
#include <stdlib.h>
void lstDel(List *lstp,const int input)
{
int isFound = 0;//to mark whether the input is found
Node *temp = NULL;//to point to the to-be-del node temporarily, so as to free it
Node *p = lstp->head;
for(p=lstp->head;p->point;p=p->point) {//to check the folloings
if(p->point->value == input) {
++isFound;
temp = p->point;//to point to the to-be-del node temporarily, so as to free it
p->point = p->point->point;//link the this node to the taget(the node after the next)
//[] [p] [to be freed] [taget]
// ↓ ↑
// →→→→→→→→→→→→→→→→→→→→↑
free(temp);//free it!
printf("Success! The first one of %d has been delete!\n",input);
break;
}
}
if(!isFound) {
printf("Delete failed.%d not found.\n",input);
}
return;
}
lstClr: 最后清除 linked list ,释放内存
#include "node.h"
#include <stdio.h>
#include <stdlib.h>
void lstClr(List *lstp)
{
Node*temp = lstp->head;
for(Node*p=temp->point;p;p=p->point) {//traverse all the list
free(temp);
temp = p;
}
free(temp);//free the last node
return;
}