1.单链表的创建(带头结点)
#include<stdlib.h>
#define ElemType int
typedef struct {//定义一个结点
ElemType data;
struct STU* next;
}STU,*LinkList;
bool InitList(LinkList& L) {
L = (STU*)malloc(sizeof(STU));//创建头结点
if (L == NULL) {//以防内存不足创建失败
return false;
}
L->next = NULL;
return true;
}
int main() {
LinkList L;
InitList(L);
}
2.单链表的创建(不带头结点)
#include <cstddef>//使用NULL需要的头文件
#define ElemType int
typedef struct {
ElemType data;
struct STU* next;
}STU,*LinkList;
bool InitList(LinkList& L) {
L = NULL;//空表,没有结点
return true;
}
int main() {
LinkList L;//声明一个指向单链表的指针
InitList(L);//初始化一个空的单链表
}
3.单链表(带头结点)--按位序插入
#include<stdlib.h>
#define ElemType int
#include <cstddef>
typedef struct {//定义一个结点
ElemType data;
struct STU *next;
}STU, *LinkList;
bool InitList(LinkList& L) {
L = (STU*)malloc(sizeof(STU));//创建头结点
if (L == NULL) {//以防内存不足创建失败
return false;
}
L->next = NULL;
return true;
}
bool ListInsert(LinkList& L, int i, ElemType e) {
if (i < 1) {
return false;
}
STU *p;//定义一个指针让其指向当前扫描到的结点
int j = 0;//表示p当前指向第几个结点
p = L;//L指向头结点,头结点是第零个结点,不存数据
while (p != NULL && j < i - 1) {//找到第i-1个结点
p = p -> next;
j++;
}
if (p == NULL) {
return false;
}
STU* s = (STU*)malloc(sizeof(STU));//定义一个新结点放插入的元素
s->data = e;
s->next = p->next;//不要搞反这两行的顺序
p->next = s;
return true;
}
int main() {
LinkList L;
InitList(L);
ListInsert(L, 2, 3);//在第二个位置插入数据3
}
4.单链表(不带头结点)--按位序插入
#include <cstddef>//使用NULL需要的头文件
#include "ConsoleApplication1.h"
#include <corecrt_malloc.h>
#define ElemType int
typedef struct {
ElemType data;
struct STU* next;
}STU,*LinkList;
bool InitList(LinkList& L) {
L = NULL;//空表,没有结点
return true;
}
bool ListInsert(LinkList &L, int i, ElemType e) {
if (i < 1) {
return false;
}
if (i == 1) {//特殊情况
STU* s = (STU*)malloc(sizeof(STU));
s->data = e;
s->next = L;
L = s;
return true;
}
STU* p;//定义一个指针让其指向当前扫描到的结点
int j = 1;//表示p当前指向第几个结点
p = L;//L指向头结点,头结点是第零个结点,不存数据
while (p != NULL && j < i - 1) {//找到第i-1个结点
p = p -> next;
j++;
}
if (p == NULL) {
return false;
}
STU* s = (STU*)malloc(sizeof(STU));//定义一个新结点放插入的元素
s->data = e;
s->next = p->next;//不要搞反这两行的顺序
p->next = s;
return true;
}
int main() {
LinkList L;//声明一个指向单链表的指针
InitList(L);//初始化一个空的单链表
ListInsert(L, 2, 3);
}
5.单链表--指定结点的后插操作(带头结点)
#include<stdlib.h>
#define ElemType int
typedef struct {//定义一个结点
ElemType data;
struct STU* next;
}STU,*LinkList;
bool InitList(LinkList& L) {
L = (STU*)malloc(sizeof(STU));//创建头结点
if (L == NULL) {//以防内存不足创建失败
return false;
}
L->next = NULL;
return true;
}
bool InsertNextNode(STU* p, ElemType e) {
if (p == NULL) {
return false;
}
STU* s = (STU*)malloc(sizeof(STU));
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
int main() {
LinkList L;
InitList(L);
STU *p;
InsertNextNode(p, 3);
}
6.单链表--指定结点的前插操作(带头结点)
#include<stdlib.h>
#define ElemType int
typedef struct {//定义一个结点
ElemType data;
struct STU* next;
}STU,*LinkList;
bool InitList(LinkList& L) {
L = (STU*)malloc(sizeof(STU));//创建头结点
if (L == NULL) {//以防内存不足创建失败
return false;
}
L->next = NULL;
return true;
}
bool InsertPriorNode(STU* p, ElemType e) {//前插操作就是先后插,再调转两个数据的位置
if (p == NULL) {
return false;
}
STU* s = (STU*)malloc(sizeof(STU));
s->next = p->next;
p->next = s;
s->data = p -> data;
p->data = e;
return true;
}
int main() {
LinkList L;
InitList(L);
STU* p;
InsertPriorNode(p, 3);
}
7.单链表--按位序删除(带头结点)
#include<stdlib.h>
#define ElemType int
typedef struct {//定义一个结点
ElemType data;
struct STU* next;
}STU,*LinkList;
bool InitList(LinkList& L) {
L = (STU*)malloc(sizeof(STU));//创建头结点
if (L == NULL) {//以防内存不足创建失败
return false;
}
L->next = NULL;
return true;
}
bool ListDelete(LinkList& L, int i, ElemType e) {
if (i < 1) {
return false;
}
STU* p;
int j = 0;
p = L;
while (p != NULL && j < i - 1) {
p = p->next;
j++;
}
if (p == NULL||p->next==NULL) {
return false;
}
STU* q = p->next;
e = q->data;
p->next = q->next;
free(q);
return true;
}
int main() {
LinkList L;
InitList(L);
ElemType e;
ListDelete(L, 2, e);
}