单链表
头文件
#include<stdio.h>
#include<stdlib.h> //malloc函数和free()
代码实现
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
/* 和上边一样的作用
struct LNode{
int data;
struct LNode *next;
};
typedef struct LNode LNode;
typedef struct LNode *LinkList;
//双链表
typedef struct DNode{
int data;
struct DNode *next,*prior; //前指针和后指针
}DNode,*DLinkList;
*/
bool InitList_1(LinkList &L){
//不带头节点的链表
L = NULL;
return true;
}
bool InitList_2(LinkList &L){
//带头节点的链表
L =(LNode *)malloc(sizeof(LNode));
if(L==NULL)
return false;
L -> next = NULL;
return true;
}
bool Empty_1(LinkList L){
if(L==NULL)
return true;
else
return false;
}
bool Empty_2(LinkList L){
if(L -> next == NULL)
return true;
else
return false;
}
bool InsertList_1(LinkList &L,int i,int x){
//带头节点插入
if(i<1)
return false;
LNode *p; //指针p当前扫描到的节点
int j=0; //当前p指向的第几个节点
p = L; //将p指向头节点
while(p != NULL && j < i-1){
//i-1限制P
p = p -> next