注意:
1.从上图可看出,链式结构在逻辑上是连续的,但是在物理上不一定连续
2.现实中的结点一般都是从堆上申请出来的
3.从堆上申请的空间是按照一定的策略来分配的,两次申请的空间可能连续也可能不连续
#include
#include
#include
typedef int SLDataType;//元素类型
typedef struct ListNode
{
SLDataType data;
struct ListNode* next;
}ListNode;
ListNode* BuySListNode(SLDataType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
assert(newnode!=NULL);
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void SListPrint(ListNode* plist)
{
ListNode* cur = plist;
while (cur != NULL)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
void SListPushBack(ListNode** pplist, SLDataType x)
{
assert(pplist!=NULL);
ListNode* newnode = BuySListNode(x);
if (*pplist == NULL)//没有结点
{
*pplist = newnode;
}
else
{
ListNode* tail = *pplist;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SListPushFront(ListNode** pplist, SLDataType x)
{
assert(pplist!=NULL);
ListNode* newnode = BuySListNode(x);
newnode->next = *pplist;
*pplist = newnode;
}
void SListPopBack(ListNode** pplist)
{
assert(pplist!=NULL);
assert(*pplist!=NULL);
if ((*pplist)->next == NULL)
{
free(*pplist);
*pplist = NULL;
}
else
{
ListNode* tailPrev = NULL;
ListNode* tail = *pplist;
while (tail->next != NULL)
{
tailPrev = tail;
tail = tail->next;
}
free(tail);
tailPrev->next = NULL;
}
}
void SListPopFront(ListNode** pplist)
{
assert(pplist!=NULL);
assert(*pplist!=NULL);
ListNode* cur = (*pplist)->next;
free(*pplist);
*pplist = cur;
}
ListNode* SListFind(ListNode* plist, SLDataType x)
{
ListNode* p = plist;
while (p != NULL)
{
if (p->data == x)return p;
p = p->next;
}
return NULL;
}
void SListInsert(ListNode** pplist, ListNode* pos, SLDataType x)
{
assert(pplist!=NULL);
assert(pos!=NULL);
// 头插
if (pos == *pplist)
{
SListPushFront(pplist, x);
}
else
{
ListNode* prev = *pplist;
while (prev->next != pos)
{
prev = prev->next;
}
ListNode* newnode = BuySListNode(x);
prev->next = newnode;
newnode->next = pos;
}
}
void SListErase(ListNode** pplist,ListNode* pos)
{
assert(pplist != NULL);
assert(pos != NULL);
if (*pplist== pos)
{
SListPopFront(pplist);
}
else
{
ListNode* prev = *pplist;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
pos = NULL;
}
}
void SListInsertAfter(ListNode* pos, SLDataType x)
{
assert(pos != NULL);
ListNode* newnode = BuySListNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
void SListEraseAfter(ListNode* pos)
{
assert(pos != NULL);
if (pos->next == NULL)
return;
ListNode* q = pos->next;
pos->next = q->next;
free(q);
q = NULL;
}
void SListDestory(ListNode* plist)
{
assert(plist != NULL);
while (plist->next!= NULL)
{
ListNode* q = plist->next;
plist->next = q->next;
free(q);
q = NULL;
}
free(plist);
}