单链表的增删查改

1.SList.c文件

#include"SList.h"

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 SListPushBack(SListNode** pphead, SListDataType x)
{
	SListNode* newNode = BuySListNode(x);

	if (*pphead == NULL)
	{
		
		*pphead = newNode;
	}
	else
	{
		//找尾
		SListNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		

		tail->next = newNode;
	}

	}
	

void SListPrint(SListNode* phead)
{
	SListNode*cur = phead;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}
2.SList.h文件

```c
#pragma once
#include<stdio.h>
#include<stdlib.h>

typedef int SListDataType;
//结点
typedef struct SListNode
{
	SListDataType data;
	struct SListNode* next;
}SListNode;


void SListPushBack(SListNode** pphead, SListDataType x);
void SListPopBack(SListNode* phead);

void SListPushFront(SListNode* phead, SListDataType x);
void SListPopFront(SListNode* phead);

void SListPrint(SListNode* phead);
  

3.test.c文件

```c
#include"SList.h"
#include<Windows.h>
int main()
{
	//SListNode* phead =NULL;
	SListNode* pList = NULL;

	SListPushBack(&pList,1);
	SListPushBack(&pList,2);
	SListPushBack(&pList,3);
	SListPushBack(&pList,4);


	SListPrint(pList);


	system("pause");
	return 0;
}
上一篇:第十周


下一篇:c语言链表从本地文件中读取和写入数据