单链表的实现。

来源:互联网 发布:数据库设计说明书实例 编辑:程序博客网 时间:2024/06/16 20:22

单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。

SListNode.h#define _CRT_SECURE_NO_WARNINGS 1#pragma once#include<stdio.h>#include<stdlib.h>#include<malloc.h>#include<assert.h>typedef int DataType;typedef struct SListNode{DataType data;struct SListNode* next;}SListNode;SListNode* BuyNode(DataType x);//申请节点void PrintSlist(SListNode* pHead);//打印链表void InitSList(SListNode*& pHead);//初始化链表void PushBack(SListNode*& pHead, DataType x);//尾插void PopBack(SListNode*& pHead);//尾删void PushFront(SListNode*& pHead, DataType x);//前插void PopFront(SListNode*& pHead);//头删void Insert(SListNode*& pHead,SListNode*& pos, DataType x);//在pos前插入一个数据void Erase(SListNode*& pHead,SListNode* pos);//删除指定位置void Remove(SListNode*& pHead,DataType x);//删除xSListNode* Find(SListNode*& pHead, DataType x);//寻找xvoid DestoryList(SListNode*& pHead);//销毁
Test.cpp#define _CRT_SECURE_NO_WARNINGS 1#include"SListNode.h"SListNode* BuyNode(DataType x)//申请节点{SListNode* node = (SListNode*)malloc(sizeof(SListNode));node->data = x;node->next = NULL;return node;}void PrintSlist(SListNode* pHead)//打印链表{SListNode*cur = pHead;while (cur){printf("%d ", cur->data);cur = cur->next;}printf("\n");}void InitSList(SListNode*& pHead)//初始化链表{pHead = NULL;}void PushBack(SListNode*& pHead,DataType x)//尾插{if (pHead == NULL){pHead = BuyNode(x);}else{SListNode* cur = pHead;while (cur->next){cur = cur->next;}cur->next = BuyNode(x);}}void PopBack(SListNode*& pHead)//尾删{//1.没有节点if (pHead == NULL){printf("The List is empty\n");return;}//2.一个节点else if (pHead->next == NULL){free(pHead);pHead = NULL;}//多个节点else{SListNode* prev=NULL, *cur=NULL;cur = pHead;while (cur->next){prev = cur;cur = cur->next;}free(cur);prev->next = NULL;}}void PushFront(SListNode*& pHead, DataType x)//头插{if (pHead==NULL){pHead = BuyNode(x);}else{SListNode* tmp = BuyNode(x);tmp->next = pHead;pHead = tmp;}}void PopFront(SListNode*& pHead)//头删{if (pHead == NULL)//判断是否为空链表{printf("The List is empty\n");return;}else if (pHead->next == NULL)//判断是否只有一个节点{free(pHead);pHead = NULL;}else//多个节点{SListNode* next = pHead->next;free(pHead);pHead = next;}}SListNode* Find(SListNode*& pHead, DataType x)//寻找x{SListNode* cur = pHead;while (cur){if (cur->data == x){return cur;}cur = cur->next;}return NULL;}void Insert(SListNode*& pHead, SListNode*& pos, DataType x)//在pos前插入一个数据{assert(pos);assert(pHead);if (pos == pHead){PushFront(pHead, x);}else{SListNode* prev = NULL, *cur = NULL;cur = pHead;while (cur != pos){prev = cur;cur = cur->next;}SListNode* tmp = BuyNode(x);tmp->next = cur;prev->next = tmp;}}void Erase(SListNode*& pHead, SListNode* pos)//删除指定位置{assert(pos);assert(pHead);if (pos == pHead){PopFront(pHead);}else if (pHead->next==NULL){SListNode* prev = NULL, *cur = NULL;cur = pHead;while (cur->next){prev = cur;cur = cur->next;}free(cur);prev->next = NULL;}else{SListNode* prev = NULL, *cur = NULL;cur = pHead;while (cur != pos){prev = cur;cur = cur->next;}prev->next = cur->next;free(cur);}}void Remove(SListNode*& pHead, DataType x)//删除x{if (pHead == NULL)//判断是否为空链表{printf("The List is empty\n");return;}else if (pHead->data == x){SListNode* next = pHead->next;free(pHead);pHead = next;}else{SListNode* prev = NULL, *cur = NULL;cur = pHead;while (cur->data != x){prev = cur;cur = cur->next;}prev->next = cur->next;free(cur);}}void DestoryList(SListNode*& pHead)//销毁{SListNode* cur = pHead;while (cur){SListNode* tmp = cur;cur = cur->next;free(tmp);tmp = NULL;}}



0 0