摘要:
//标题节点#include #include<malloc h>TypedefstructLNode{//定义单个链接列表intdata;structLNode*next;}节点,*LinkList;boolInitList;//初始化单个链接列表boolListInsert//在指定位置插入数据boolEmpty//判断链接列表是否为空boolHListInsert//将数据boolRListInsert插入到指定节点之前//在指定节点之后插入数据void输出//输出链接的boolListDelete//删除指定位置的数据boolHeadInsert//头插入节点*GetElem//查找指定位置的值intLocateElem//查找其中指定值为intmain{LinkListL;InitList;HeadInsert(L,5);HeadInsert(L,2);TailInsert(L,7);Tail Insert(1,4);RListInsert;printf;output;printf(“”);Node*r=GetElem(L,3);printf,printf(”);intx=LocaleElem(L,5),printf;printf f(“”);如果{printf(“当前链接列表为空!=NULL&&jnext;j++;}如果{returnfalse;}节点*q=p-˃next;e=q-˃数据;p-˃next=q-˃next;自由的returntrue;}//头插入方法boolHeadInsert{Node*s=malloc;//L-˃next=NULL;//使用头插入方法创建单个链接列表时,需要初始化nexts-˃data=e;s-˃next=L-˃next;L-˃next=s;returntrue;}//查找值Node*GetElem{if(i˂1){returnNULL;}在指定位置节点*p=L;intj=0;而(p!
//带头节点的单链表
#include <stdio.h>
#include <malloc.h>
typedef struct LNode{ //定义单链表
int data;
struct LNode *next;
}Node , *LinkList;
bool InitList(LinkList &); //初始化单链表
bool ListInsert(LinkList & , int , int); //在指定位置插入数据
bool Empty(LinkList); //判断链表是否为空
bool HListInsert(LinkList & , int , int); //在指定节点前面插入数据
bool RListInsert(LinkList * , int , int); //在指定节点后面插入数据
void output(LinkList); //输出链表
bool ListDelete(LinkList & , int , int &); //删除指定位置的数据
bool HeadInsert(LinkList , int); //头插法
Node* GetElem(LinkList , int); //查找指定位置的值
int LocateElem(LinkList , int); //查找指定的值在哪个位置
int main(void){
LinkList L;
InitList(L);
HeadInsert(L,5);
HeadInsert(L,2);
TailInsert(L,7);
TailInsert(L,4);
RListInsert(&L,2,3);
printf("当前单链表中的数据是:");
output(L);
printf("
");
Node* r = GetElem(L,3);
printf("你要查找的值是%d",r->data);
printf("
");
int x = LocateElem(L,7);
printf("你要查找的值在第%d位",x);
printf("
");
int e = -1;
ListDelete(L,2,e);
printf("删除的哪个元素是:%d",e);
printf("
");
printf("当前单链表中的数据是:");
output(L);
printf("
");
if(Empty(L)){
printf("当前链表为空!");
}else{
printf("当前链表不为空! ");
}
return 0;
}
//初始化单链表
bool InitList(LinkList &L){
L = (Node*)malloc(sizeof(Node));
if(L == NULL){
return false;
}
L->next = NULL;
return true;
}
//遍历单链表
void output(LinkList L){
Node *p = L->next;
while(p != NULL){
printf("%d ",p->data);
p = p->next;
}
}
//判断单链表是否为空
bool Empty(LinkList L){
if(L->next == NULL){
return true;
}else{
return false;
}
}
//在指定的位置插入数据
bool ListInsert(LinkList &L ,int i , int e){
if(i < 1){
return false;
}
Node *p = L;
int j = 0;
while(p != NULL && j < i-1){
p = p->next;
j ++;
}
if(p == NULL){
return false;
}
Node *s = (Node*)malloc(sizeof(Node));
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
//在指定节点前面插入数据
bool HListInsert(LinkList &L , int i , int e){
if(i < 1){
return false;
}
Node *p = L;
int j = 0;
while(p != NULL && j < i){
p = p->next;
j ++;
}
if(p == NULL){
return false;
}
Node *s = (Node *)malloc(sizeof(Node));
s->data = p->data;
p->data = e;
s->next = p->next;
p->next = s;
return true;
}
//在指定节点后面插入数据
bool RListInsert(LinkList *L , int i , int e){
if(i < 1){
return false;
}
Node *p = *L;
int j = 0;
while(p != NULL && j < i){
p = p->next;
j ++;
}
if(p == NULL){
return false;
}
Node *s = (Node *)malloc(sizeof(Node));
s->next = p->next;
p->next = s;
s->data = e;
}
//删除指定位置的数据
bool ListDelete(LinkList &L , int i , int &e){
if(i < 1){
return false;
}
Node *p = L;
int j = 0;
while(p != NULL && j < i-1){
p = p->next;
j ++;
}
if(p == NULL || p->next == NULL){
return false;
}
Node *q = p->next;
e = q->data;
p->next = q->next;
free(q);
return true;
}
//头插法
bool HeadInsert(LinkList L , int e){
Node *s = (Node *)malloc(sizeof(Node));
//L->next = NULL; //使用头插法建立单链表时需要初始化next
s->data = e;
s->next = L->next;
L->next = s;
return true;
}
//查找指定位置的值
Node* GetElem(LinkList L , int i){
if(i < 1){
return NULL;
}
Node *p = L;
int j = 0;
while(p != NULL && j < i){
p = p->next;
j ++;
}
return p;
}
//查找指定的值在哪个位置
int LocateElem(LinkList L , int e){
Node *p = L->next;
int j = 1;
while(p != NULL && p->data != e){
p = p->next;
j ++;
}
if(p == NULL){
return -1;
}else{
return j;
}
}