记录作业~
SList.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include<assert.h>
typedef int SLTDateType;
typedef struct SListNode
{
SLTDateType data;
struct SListNode* next;
}SLTNode;
void SListPrint(SLTNode* phead);
void SListPushBack(SLTNode** pphead, SLTDateType x);
void SListPushFront(SLTNode** pphead, SLTDateType x);
void SListPopBack(SLTNode** pphead);
void SListPopFront(SLTNode** pphead);
SLTNode* SListFind(SLTNode* phead, SLTDateType x);
void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDateType x);
void SListErase(SLTNode** pphead, SLTNode* pos);
void SListDestroy(SLTNode** pphead);
SList.c
#include "SList.h"
void SListPrint(SLTNode* phead)
{
SLTNode* cur = phead;
while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
SLTNode* SListFind(SLTNode* phead, SLTDateType x)
{
SLTNode* cur = phead;
while (cur)
{
if (cur->data == x) {
return cur;
}
else {
cur = cur->next;
}
}
return NULL;
}
SLTDateType* BuyListNode(SLTDateType x)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void SListPushBack(SLTNode** pphead, SLTDateType x)
{
SLTNode* newnode = BuyListNode(x);
if (*pphead == NULL){
*pphead = newnode;
}
else{
SLTNode* tail = *pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SListPushFront(SLTNode** pphead, SLTDateType x)
{
SLTNode* newnode = BuyListNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void SListPopBack(SLTNode** pphead)
{
assert(*pphead != NULL);
if ((*pphead)->next == NULL) {
free(*pphead);
*pphead = NULL;
}
else {
SLTNode* tail = *pphead;
SLTNode* prev = NULL;
while (tail->next)
{
prev = tail;
tail = tail->next;
}
free(tail);
tail = NULL;
prev->next = NULL;
}
}
void SListPopFront(SLTNode** pphead)
{
assert(*pphead != NULL);
SLTNode* next = (*pphead)->next;
free(*pphead);
*pphead = next;
}
void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDateType x)
{
SLTNode* newnode = BuyListNode(x);
if (*pphead == pos) {
newnode->next = *pphead;
*pphead = newnode;
}
else {
SLTNode* posPrev = *pphead;
while (posPrev->next != pos)
{
posPrev = posPrev->next;
}
posPrev->next = newnode;
newnode->next = pos;
}
}
void SListErase(SLTNode** pphead, SLTNode* pos)
{
if (*pphead == pos) {
SListPopFront(pphead);
}
else {
SLTNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
}
}
void SListDestroy(SLTNode** pphead)
{
assert(pphead);
SLTNode* cur = *pphead;
while (cur)
{
SLTNode* next = cur->next;
free(cur);
cur = next;
}
*pphead = NULL;
}
Comments | 58 条评论
博主 INlytAKnaopDV
HmAzVGiuYLPDaOg
博主 qJCGvhEOwbNiy
gnUWNFiqmkM
博主 DfIHvpVOGP
sGRIAJUQi
博主 oITXMkPlRavBgcCJ
LTenBWgi
博主 qJrLIhub
pnCvoaTwARdB
博主 wrdWKfQMxGP
CfgqBLKIkchyjTOE
博主 lQpsgfaqPuLUkDHN
wJrjsTulD
博主 SaiCYDGvZWcM
ZXBgMdYcpjFh
博主 MFxfbLsrIYp
@SaiCYDGvZWcM yuFhpdDks
博主 NvtTkaxjlA
yIpKPSkbfNg
博主 GCTXWzbudKFNBqie
@NvtTkaxjlA EyjtCrKoi
博主 mXVrqbBLdHTKhn
nouvTZyGJDUbr
博主 MQJeIVcsHZyGUL
@mXVrqbBLdHTKhn KeAqoXSOGrkjiEgW
博主 KtPTAQeqmlLubCM
@MQJeIVcsHZyGUL gtUSeoWfu
博主 tBapPeYQoJR
lnQBVOJp
博主 OTykrWzviUcxKNE
@tBapPeYQoJR tqQwlAZyikWfTaMV
博主 nLRTfrVsJBiUMetO
OlwRQEbP
博主 mjVnsMkpvKLelZSf
@nLRTfrVsJBiUMetO AYONHajiTBEePF
博主 crouXAhelvj
OSpdbWxvKFoe
博主 CUGrIyKfexXL
@crouXAhelvj zbusPjomqL
博主 izHUlZMGtdyo
CnDfuBaWkQYhLrO