看:
#include<stdio.h>
#include<stdlib.h>
typedef int SListDataType;
//结点
typedef struct SListNode
{
SListDataType data;
struct SListNode* next;
}SListNode;
SListNode* BuySListNode(SListDataType x)
{
SListNode* newNode = (SListNode*)malloc(sizeof(SListNode));//申请结点并存储数据
if (newNode == NULL)
{
printf("申请结点失败\n");
exit(-1);
}
newNode->data = x;
newNode->next = NULL;
return newNode;
}
void SListPrint(SListNode* phead)
{
SListNode* cur = phead;
while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL");
printf("\n");
}
void SListPushBack(SListNode** pphead, SListDataType x)//尾加
{
if (*pphead == NULL)//原链表为空
{
SListNode* newNode = BuySListNode(x);
*pphead = newNode;
}
else//原链表不为空,找尾
{
SListNode* tail = *pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
SListNode* newNode = BuySListNode(x);
tail->next = newNode;
}
}
void SListPopBack(SListNode** pphead)
{
//(1)空链表
if (*pphead == NULL)
{
return;
}
//一个结点
else if ((*pphead)->next == NULL)
{
free(*pphead);//释放地址为*pphead的空间;
*pphead = NULL;//让指针*pphead指向NULL;
}
//一个以上结点
else
{
SListNode* prev = NULL;//用于存放删减结点前面的结点的地址
SListNode* tail = *pphead;
while (tail->next != NULL)
{
prev = tail;
tail = tail->next;
}
free(tail);
//tail = NULL;
prev->next = NULL;
}
}
void SListPushFront(SListNode** pphead, SListDataType x)//头加
{
SListNode* newNode = BuySListNode(x);
newNode->next = *pphead;
*pphead = newNode;
}
void SListPopFront(SListNode** pphead)//头删
{
if (*pphead == NULL)
{
return;
}
else
{
SListNode* next = (*pphead)->next;
free(*pphead);
*pphead = next;
}
}
SListNode* SListFind(SListNode* phead, SListDataType x)//查找数据x,并返回它的指针;
{
SListNode* cur = phead;
while (cur != NULL)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
/*
void SListInserAfter(SListNode* pos, SListDataType x)//在pos处后面插入数据
{
SListNode* newNode = BuySListNode(x);
SListNode* next=pos->next;
pos->next=newNode;
newNode->next=next;//原理和交换两个数的值差不多(在给一个变量赋另一个值时,如果原来的值有用,就得事先保存);
}
void SListEraseAfter(SListNode* pos, SListDataType x)//删除pos处后面数据
{
SListNode* next = pos->next;
SListNode* nextnext = next->next;
free(next);
pos->next = nextnext;
}
*/
void test1()
{
SListNode* pList = NULL;//永远指向链表的第一个结点;
SListPushBack(&pList, 1);
SListPushBack(&pList, 2);
SListPushBack(&pList, 3);
SListPushBack(&pList, 4);
SListPrint(pList);
SListPopBack(&pList);
SListPopBack(&pList);
SListPopBack(&pList);
SListPopBack(&pList);
SListPopBack(&pList);
SListPrint(pList);
SListPushFront(&pList, 1);
SListPushFront(&pList, 2);
SListPushFront(&pList, 3);
SListPushFront(&pList, 4);
SListPushFront(&pList, 5);
SListPrint(pList);
SListPopFront(&pList);
SListPopFront(&pList);
SListPopFront(&pList);
SListPopFront(&pList);
SListPopFront(&pList);
SListPrint(pList);
SListPushBack(&pList, 1);
SListPushBack(&pList, 2);
SListPushBack(&pList, 3);
SListPushBack(&pList, 4);
SListPrint(pList);
SListNode* pos = SListFind(pList, 3);
if (pos !=NULL)
{
pos->data = 30;
}
SListPrint(pList);
}
int main()
{
test1();
//system("pause");
return 0;
}
无语!