其他分享
首页 > 其他分享> > 单链表的增删查改

单链表的增删查改

作者:互联网

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;
}

标签:单链,void,phead,查改,SListNode,增删,newNode,SListPushBack,NULL
来源: https://blog.csdn.net/weixin_46031570/article/details/110920060