单链表是C语言,c++和数据结构中的很重要的一个部分,也有很多实现它的方法,这些算法代码的实现都大同小异,我在这里就用c语言和c++两种不同的方法实现一下链表的基本操作。下面是c++和C语言的两种链表结构的定义
c++方式
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int DataType;
typedef struct SListNode
{
DataType data; //数据
struct SListNode* next; //指向下一个节点的指针
}SListNode;
C语言方式
typedef int DataType;
typedef struct LinkNode
{
DataType data;
struct LinkNode* next;
}LinkNode,*pLinkNode;
typedef struct LinkList
{
LinkNode* pHead;
}LinkList,*pLinkList;
c++中有一个该鸟叫做引用&,就是C语言中取地址的那个符号,在这里既然提到了就把C语言和c++中的引用和指针粗浅的提一下
引用:给已经存在的对象起一个别名
类型 &引用变量名 = 已定义过的变量名
引用的特点:
1.可以引用一个对象即一个变量可以有多个别名
2.引用必须初始化 int a = 1; int r& = a;
3.引用只能在初始化的时候引用一次,不能改变为再引用其他的变量
不要返回一个临时变量,局部变量的引用
引用和指针的区别:
1.引用只能在定义时初始化一次,之后不能改变指向其他变量,指针值可变
2.引用必须指向有效的变量,指针可以为空
3.sizeof指针对象和引用对象的意义不一样,sizeof引用得到的是所指向变量的大小,
而sizeof指针是指向对象地址的大小
4.指针和引用自加(++)自减(--)意义不一样
5.相对而言,引用比指针更安全,指针比引用更灵活,危险系数也更高,检查指针是否为空,
指针所指地址释放后最好置为0,否则存在野指针(垂悬指针)(悬空指针)的问题
所以在c++实现链表中采用引用传值的方法更安全高效
c++实现链表的基本操作
void print(slistnode* head)
{
slistnode* cur = head;
while (cur!= null)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("null");
printf("\n");
}
slistnode* buynode(datatype x)
{
slistnode* temp;
temp = (slistnode*)malloc(sizeof(slistnode));
temp->data = x;
temp->next = null;
return temp;
}
void destoryslist(slistnode* & phead)
{
slistnode* cur = phead;
while (cur)
{
slistnode* temp = cur;
cur = cur->next;
free(temp);
temp = null;
}
}
void pushback(slistnode* & phead, datatype x)
{
//1.空
//2.不空
if (phead == null)
{
phead = buynode(x);
}
else
{
slistnode* tail = phead;
while (tail->next != null)
{
tail = tail->next;
}
tail->next = buynode(x);
}
}
void popback(slistnode* & phead)
{
//1.空
//2.一个节点
//3.多节点
if (phead == null)
{
printf("the slist is empty\n");
return;
}
else if (phead->next == null)
{
free(phead);
phead = null;
}
else
{
slistnode* tail = phead;
slistnode* prev = null;
while (tail->next != null)
{
prev = tail;
tail = tail->next;
}
free(tail);
prev->next = null;
}
}
void pushfront(slistnode* & phead, datatype x)
{
//1.空
//2.不空
if (phead == null)
{
phead = buynode(x);
}
else if (phead != null)
{
slistnode* cur = buynode(x);
cur->next = phead;
phead = cur;
}
}
void popfront(slistnode* & phead)
{
if (phead == null)
{
return;
}
else if (phead->next == null)
{
free(phead);
phead = null;
}
else if (phead != null)
{
slistnode* cur = phead;
phead = phead->next;
free(cur);
cur = null;
}
}
slistnode* find(slistnode* phead, datatype x)
{
slistnode* cur = phead;
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return null;
}
void insert(slistnode* pos, datatype x)
{
assert(pos);
slistnode* temp = buynode(x);
temp->next = pos->next;
pos->next = temp;
}
void erase(slistnode* phead,slistnode* pos)
{
assert(pos);
assert(phead);
if (phead == pos)
{
phead = phead->next;
free(pos);
pos = null;
}
slistnode* prev = phead;
while (prev)
{
if (prev->next == pos)
{
prev->next = pos->next;
free(pos);
pos = null;
break;
}
prev = prev->next;
}
}
C语言实现链表的基本操作
void InitLinkList(pLinkList list)
{
assert(list);
list->pHead = NULL;
}
void Destory(pLinkList list)//销毁链表
{
pLinkNode cur = NULL;
assert(list);
if (list->pHead == NULL)
{
return;
}
cur = list->pHead;
while (cur)
{
pLinkNode del = cur;
cur = cur->next;
free(del);
del = NULL;
}
}
pLinkNode BuyNode(DataType x)//构造节点
{
pLinkNode newNode =(pLinkNode) malloc(sizeof(LinkNode));
if (NULL == newNode)
{
printf("out of memory\n");
return NULL;
}
newNode->next = NULL;
newNode->data = x;
return newNode;
}
void PrintList(pLinkList list)//show打印
{
pLinkNode cur = NULL;
assert(list);
cur = list->pHead;
while (cur)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
void PushBack(pLinkList list, DataType x)//尾插
{
pLinkNode cur = NULL;
assert(list);
cur = list->pHead;
pLinkNode newNode = BuyNode(x);
if (list->pHead == NULL)
{
list->pHead = newNode;
}
else
{
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = newNode;
}
}
void PopBack(pLinkList list)//尾删
{
pLinkNode cur = NULL;
pLinkNode prev = NULL;
assert(list);
cur = list->pHead;
prev = NULL;
if (cur == NULL)
{
printf("NULL");
}
else if (cur->next == NULL)
{
free(list->pHead);
list->pHead = NULL;
}
else
{
while (cur->next != NULL)
{
prev = cur;
cur = cur->next;
}
free(cur);
cur = NULL;
prev->next = NULL;
}
}
void PushFront(pLinkList list, DataType x)//前插
{
pLinkNode cur = NULL;
assert(list);
cur = BuyNode(x);
cur->next = list->pHead;
list->pHead = cur;
}
void PopFront(pLinkList list)//尾删
{
pLinkNode del = NULL;
assert(list);
del = list->pHead;
list->pHead = list->pHead->next;
free(del);
del = NULL;
}
//void DestoryList(pLinkList list);
LinkNode* Find(pLinkList list, DataType x)//寻找节点
{
pLinkNode cur = NULL;
assert(list);
cur = list->pHead;
while (cur->next != NULL)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void Insert(pLinkList list,LinkNode* pos, DataType x)//插入节点
{
pLinkNode cur = NULL;
assert(list);
//assert(pos);
if (pos == NULL)
{
printf("没找到\n");
return;
}
cur = BuyNode(x);
cur->next = pos->next;
pos->next = cur;
}
void Erase(pLinkList list, LinkNode* pos)//删除pos节点
{
pLinkNode del = NULL;
assert(list);
if (pos == NULL)
{
printf("无此节点\n");
return;
}
//assert(pos);
del = pos->next;
del->next = del->next;
free(del);
del = NULL;
}