一般面试题中会考虑无头节点的单链表下面是一些实现:
声明Sqlist.h文件
#pragma once
#include <assert.h>
#include <stdio.h>
typedef int Datatype;
typedef struct SList
{
Datatype data;
struct SList* next;
}SList, *PSlist;
//无头结点
// 初始化单链表(对于无头结点单链表,该函数没有意义)
void InitList(PSlist* pHead);
// 销毁单链表
void DestroyList(PSlist* pHead);
// 尾插
void PushBack(PSlist* pHead, Datatype data);
// 尾出
void PopBack(PSlist* pHead);
// 头插
void PushFront(PSlist* pHead, Datatype data);
// 头出
void PopFront(PSlist* pHead);
// 在链表中查找元素data
PSlist Find(PSlist pHead, Datatype data);
// 删除pos位置的结点(注意不能用那种替换形式)
void Erase(PSlist* pHead, PSlist pos);
// 在链表的pos位置插入元素data
void Insert(PSlist* pHead, PSlist pos, Datatype data);
void PrintList(PSlist pHead);
函数实现:
#pragma once
#include "Sqlist.h"
void InitList(PSlist* pHead)
{
assert(pHead);
*pHead = NULL;
return;
}
void PrintList(PSlist pHead)?????
{
while (pHead)
{
printf("%d->", pHead->data);
pHead = pHead->next;
}
printf("NULL\n");
}
PSlist BuyNode(Datatype data)
{
PSlist pNew = (PSlist)malloc(sizeof(SList));
if (pNew == NULL)
{
return NULL;
}
pNew->next = NULL;
pNew->data = data;
return pNew;
}
void PushBack(PSlist* pHead, Datatype data)
{
assert(pHead);
PSlist pNewNode = BuyNode(data);
PSlist pTemp = *pHead;
if (pTemp == NULL)
{
*pHead =pNewNode;
return;
}
while (pTemp->next != NULL)
{
pTemp = pTemp->next;
}
pTemp->next = pNewNode;
}
void PopBack(PSlist* pHead)
{
assert(pHead);
PSlist pPre = *pHead;
if (pPre == NULL)
{
return;
}
if (pPre->next == NULL)
{
free(pPre);
pPre = NULL;
*pHead = NULL;
}
if (pPre != NULL)
{
PSlist pDel = pPre->next;
while (pDel->next != NULL)
{
pPre = pPre->next;
pDel = pDel->next;
}
pPre->next = NULL;
pDel->next = NULL;
free(pDel);
}
return;
}
void PushFront(PSlist* pHead, Datatype data)
{
assert(pHead);
PSlist pNew = BuyNode(data);
if (pNew == NULL)
{
return;
}
pNew->next = *pHead;
*pHead = pNew;//
}
void PopFront(PSlist* pHead)
{
assert(NULL != pHead);
PSlist pCur = *pHead;
if (pCur == NULL)
{
return;
}
else
{
*pHead = pCur->next;
free(pCur);
pCur->next = NULL;
}
}
PSlist Find(PSlist pHead, Datatype data)
{
assert(pHead);
while (pHead)
{
if(pHead->data == data)
return pHead;
pHead = pHead->next;
}
return NULL;
}
void Erase(PSlist* pHead, PSlist pos)//1无节点 //2有节点
{
PSlist pPrepos = *pHead;
assert(NULL != pos);
assert(NULL != pHead);
if (pos == *pHead)
{
PSlist pCur = *pHead;
*pHead = pos->next;
free(pCur);
pCur->next = NULL;
}
else
{
while (pPrepos->next!= pos)
{
pPrepos = pPrepos->next;
}
pPrepos->next = pos->next;
free(pos);
pos->next = NULL;
}
}
void Insert(PSlist* pHead, PSlist pos, Datatype data)//后插
{
assert(NULL != pHead);
assert(NULL != pos);
PSlist pNew = BuyNode(data);
if (pNew == NULL)
{
return;
}
pNew->next = pos->next;
pos->next = pNew;
}
测试函数:
#include "SList.h"
//Init Print Pushback
void Fun1()
{
PSlist slist;
InitList(&slist);
PushBack(&slist,1);
PushBack(&slist,2);
PushBack(&slist, 3);
PushBack(&slist, 4);
PopBack(&slist);
PopBack(&slist);
PopBack(&slist);
PopBack(&slist);
PopBack(&slist);
PrintList(slist);
}
//PushFront PopFront
void Fun2()
{
PSlist slist;
InitList(&slist);
PushFront(&slist, 1);
PushFront(&slist, 2);
PushFront(&slist, 3);
PushFront(&slist, 4);
PopFront(&slist);
PopFront(&slist);
PopFront(&slist);
PopFront(&slist);
PrintList(slist);
}
//Erase Find
void Fun3()
{
PSlist slist;
InitList(&slist);
PushFront(&slist, 1);
PushFront(&slist, 2);
PushFront(&slist, 3);
PushFront(&slist, 4);
//Erase(&slist, Find(slist, 4));
Insert(&slist, Find(slist, 4), 8);//找不到该位置就是NULL,assert(pos)会报错
PrintList(slist);
}