单链表各个接口的实现

注意单链表的方向不能逆转
//接口声明
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef Seqlist_H
#define Seqlist_H

#include
#include
#include
#include
#include

typedef int SLDataType;
typedef struct SlistNode
{
SLDataType _data;
struct SlistNode* _next;
}SlistNode;
typedef struct Slist
{
SlistNode* _head;
}Slist;

void SlistInit(Slist* plist);
void SlistPrint(Slist* plist);
SlistNode* BuySlistNode(SLDataType x);
void SlistPushFront(Slist* plist, SLDataType x);
void SlistPushBack(Slist* plist, SLDataType x);
void SlistPopBack(Slist* plist);
void SlistPopFront(Slist* plist);
SlistNode* SlistFind(Slist* plist, SLDataType x);
void SlistInsertAfter(SlistNode* pos, SLDataType x);
void SlistEraseAfter(SlistNode* pos);
void SlistRemove(Slist* plist, SLDataType x);
typedef int SLDataType;
void test();
void SlistDesory(Slist* plist);
void removeElements(SlistNode* _head, int val);
SlistNode* reverseList(SlistNode* _head);
SlistNode* middleNode(SlistNode* _head);
SlistNode* FindKthToTil(SlistNode* _head, unsigned int k);
SlistNode* mergeTwoLists(SlistNode* l1, SlistNode* l2);

#endif __
//各个接口的实现
#include “Sqlist.h”
void SlistInit(Slist* plist)
{
assert(plist);
plist->_head = NULL;
}
void SlistPrint(Slist* plist)
{
assert(plist);
SlistNode* cur = plist->_head;
while (cur)
{
printf("%d->", cur->_data);
cur = cur->_next;
}
printf(“NULL”);
}
SlistNode* BuySlistNode(SLDataType x)
{
SlistNode* newNode = (SlistNode*)malloc(sizeof(SlistNode));
newNode->_data = x;
newNode->_next = NULL;//NULL指针指的是内存为0的地址
return newNode;
}
void SlistPushFront(Slist* plist, SLDataType x)
{
assert(plist);
SlistNode* newhead= BuySlistNode(x);
newhead->_next = plist->_head;//newhead的指针指向原来的头
plist->_head = newhead;//原来的头指向newhead
}
void SlistPushBack(Slist* plist, SLDataType x)
{
assert(plist);
SlistNode* cur = plist->_head;
if (plist->_head == NULL)
{
plist->_head = BuySlistNode(x);
}
else
{
while (cur->_next)
{
cur = cur->_next;
}
SlistNode* newNode = BuySlistNode(x);
cur->_next = newNode;
}
}
void SlistPopFront(Slist* plist)
{
assert(plist);
SlistNode* next = plist->_head->_next;
free(plist->_head);
next = plist->_head;
}
void SlistPopBack(Slist* plist)
{
assert(plist);
SlistNode* cur = plist->_head;
SlistNode* prev = NULL;
if (plist->_head == NULL)
{
free(plist->_head);
plist->_head = NULL;
}
else
{
while (cur->_next)
{
prev = cur;//prev用来保存cur的前一个位置的节点,如果不保存的话,
//直接free了最后一个节点,那么倒数第二个节点就会变成野指针
cur = cur->_next;
}
free(cur);
prev->_next = NULL;
while (cur->_next->_next)
{
cur = cur->_next;
}
free(cur->_next);
cur->_next = NULL;
}
}
SlistNode* SlistFind(Slist* plist, SLDataType x)
{
assert(plist);
SlistNode* cur = plist->_head;
while (cur)
{
if (cur->_data == x)
{
return cur;
}
cur = cur->_next;
}
return cur;
}
void SlistInsertAfter(SlistNode* pos, SLDataType x)
{
assert(pos);
SlistNode* newNode = BuySlistNode(x);
SlistNode* next = pos->_next;
pos->_next = newNode;
newNode->_next = next;
}
void SlistEraseAfter(SlistNode* pos)
{
assert(pos);
SlistNode* next = pos->_next;
SlistNode* nextNext = next->_next;
pos->_next = nextNext;
free(next);
next = NULL;
}
void SlistRemove(Slist* plist, SLDataType x)
{
assert(plist);
SlistNode* prev = plist->_head;
SlistNode* cur = prev->_next;
while (cur)
{
if (cur->_data == x)
{
prev->_next = cur->_next;
free(cur);
cur = prev->_next;
}
else
{
prev = cur;
cur = cur->_next;
}
}
if (plist->_head == x)
{
plist->_head = plist->_head->_next;
}
}
void SlistDesory(Slist* plist)
{
assert(plist);
SlistNode* cur = plist->_head;
while (cur)
{
SlistNode* next = cur->_next;
free(cur);
cur = next;
}
plist->_head = NULL;
}

void test()
{
Slist* list;
SlistInit(&list);
SlistPushFront(&list, 1);
SlistPushFront(&list, 2);
SlistPushFront(&list, 3);
SlistPushFront(&list, 5);
SlistPrint(&list);

SlistPushBack(&list, 5);
SlistPushBack(&list, 6);
SlistPrint(&list);

SlistFind(&list, 7);
SlistNode* pos = SlistFind(&list, 3);
SlistInsertAfter(pos,7);
SlistPrint(&list);
SlistEraseAfter(1);
SlistPrint(&list);
SlistDesory(&list);

}

你可能感兴趣的:(数据结构)