周末作业-10.28布置

 

 

 

 

 

 

 

 

 

#include"SList.h"
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
 
//创造结点
SListNode* BuySListNode(DateType x)
{
SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
if (newnode == NULL)
{
exit(0);
}
newnode->data = x;
newnode->next = NULL;
return newnode;
}
//尾插
void SListPushBack(SListNode** pplist, DateType x)
{
assert(pplist != NULL);
if (NULL == *pplist)
{
*pplist = BuySListNode(x);
}
else
{
// pplist的链表不为空
// 1. 找到原链表中的最后一个节点
SListNode* cur = *pplist;
while (cur->next)
{
cur = cur->next;
}
 
// 2. 插入新节点
cur->next = BuySListNode(x);
}
}
//尾删
void SListPopBack(SListNode** pplist)
{
assert(pplist != NULL);
 

if (NULL == *pplist)
return;
else if (NULL == (*pplist)->next)
{

free(*pplist);
*pplist = NULL;
}
else
{

SListNode* cur = *pplist;
SListNode* prev = NULL;   
while (cur->next)
{
prev = cur;
cur = cur->next;
}
 

free(cur);
prev->next = NULL;
 

}
 
}
//头插
void SListPushFront(SListNode** pplist, DateType x)
{
assert(pplist != NULL);
 

SListNode* newnode = BuySListNode(x);
newnode->next = *pplist;
*pplist = newnode;
 
}
//头删
void SListPopFront(SListNode** pplist)
{
assert(pplist != NULL);
if (*pplist == NULL)
{
printf("链表为空!!!\n");
return;
}
else
{
SListNode* cur = *pplist;
*pplist = cur->next;
free(cur);
cur = NULL;
}
}
SListNode* SListInsertAfter(SListNode* pos, DateType x)
{
if (pos)
{
return;
}
SListNode* newnode = BuySListNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
SListNode* SListEraseAfter(SListNode* pos)
{
if (pos||pos->next)
{
return;
}
SListNode* p = pos->next;
pos->next = p->next;
free(p);
p = NULL;
}
//查找元素
SListNode* SListFind(SListNode* plist, DateType x)
{
SListNode* cur = plist;
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
//统计链表的元素个数
int SListSize(SListNode* plist)
{
SListNode* cur = plist;
int count = 0;
while (cur)
{
count++;
cur = cur->next;
}
return count;
}
//销毁链表
void SListDestory(SListNode** pplist)
{
assert(pplist != NULL);
SListNode* cur = *pplist;
while (cur)
{
*pplist = cur->next;
free(cur);
cur = *pplist;
}
*pplist = NULL;
}
//打印单链表
void SListPrint(SListNode* plist)
{
SListNode* p = plist;
while (p != NULL)
{
printf("%d---->", p->data);
p = p->next;
}
printf("NULL\n");
}
 
void Test()
{
SListNode* s =NULL;
SListPushBack(&s,1);
SListPushBack(&s, 2);
SListPushBack(&s, 3);
SListPushBack(&s, 4);
SListPushBack(&s, 5);
 
    SListPopBack(&s);
SListPushFront(&s, 4);
SListPushFront(&s, 3);
SListPushFront(&s, 2);
SListPushFront(&s, 1);
 
SListPrint(s);
int num=SListSize(s);
printf("\nnum=%d\n", num);
SListDestory(&s);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值