单链表

本文介绍了一个简单的链表数据结构,并提供了基本的操作实现,包括插入、删除、打印等。通过具体的函数展示如何创建节点、在链表头部或尾部添加元素、删除指定位置的元素等。此外,还提供了一些测试函数来验证链表操作的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#pragma once
typedef int DataType;
typedef struct Node
{
 DataType data;
 struct Node* next;
}Node;
void PrintList(Node* pHead);
void PushBack(Node** ppHead, DataType x);
void PopBack(Node** ppHead);
void PushFront(Node** ppHead, DataType x);
void PopFront(Node** ppHead);
Node* Find(Node* pHead, DataType x);
void Insert(Node** ppHead, Node* pos, DataType x);
void Erase(Node** ppHead, Node* pos);
Node* BuyNode(DataType x)
{
 Node* node = (Node*)malloc(sizeof(Node));
 node->data = x;
 node->next = NULL;
 return node;
}

void PushBack(Node** ppHead, DataType x)
{
 if (*ppHead == NULL)
 {
  *ppHead = BuyNode(x);
 }
 else
 {
  Node* tail = *ppHead;
  while (tail->next)
  {
   tail = tail->next;
  }
  tail->next = BuyNode(x);
 }
}
void PopBack(Node** ppHead)
{
 if (*ppHead == NULL)
 {
  return;
 }
 else if (*ppHead->next == NULL)
 {
  free(*ppHead);
  *ppHead = NULL;
 }
 else
 {
  Node* cur = *ppHead;
  Node* prev = NULL;
  while (cur->next)
  {
   prev = cur;
   cur = cur->next;
  }
  prev->next = NULL;
  free(cur);
 }
}
void PushFront(Node** ppHead, DataType x)
{
 if (*ppHead == NULL)
 {
  *ppHead = BuyNode(x);
 }
 else
 {
  Node* tmp = BuyNode(x);
  tmp->next = *ppHead;
  *ppHead = tmp;
 }
}
void PopFront(Node** ppHead)
{
 if (*ppHead == NULL)
 {
  return;
 }
 else if (*ppHead -> next == NULL)
 {
  free(*ppHead);
  *ppHead = NULL;
 }
 else
 {
  Node* next = (*ppHead)->next;
  free(*ppHead);
  *ppHead = next;
 }
}
void Insert(Node** ppHead, Node* pos, DataType x)
{
 assert(pos);
 if (pos == *ppHead)
 {
  PushFront(ppHead, x);
 }
 else
 {
  Node* prev = *ppHead;
  while (prev->next != pos)
  {
   prev = prev->next;
  }
  Node* tmp = BuyNode(x);
  prev->next = tmp;
  tmp->next = pos;
 }
}
void Erase(Node** ppHead, Node* pos)
{
 assert(pos);
 if (pos == *ppHead)
 {
  PopFront(ppHead);
 }
 else if (pos->next == NULL)
 {
  PopBack(ppHead);
 }
 else
 {
  Node* prev = *ppHead, *next = pos->next;
  while (prev->next != pos)
  {
   prev = prev->next;
  }
  prev->next = next;
  free(pos);
 }
}
void PrintList(Node* pHead)
{
 Node* cur = pHead;
 while (cur)
 {
  printf("%d ", cur->data);
  cur = cur->next;
 }
 printf("\n");
}
void TestList1()   // PushBack  &  PopBack
{
 Node* list = NULL;
 PushBack(&list, 1);
 PushBack(&list, 2);
 PushBack(&list, 3);
 PushBack(&list, 4);
 PushBack(&list, 5);
 PushBack(&list, 6);
 PrintList(list);
 PopBack(&list, 1);
 PopBack(&list, 2);
 PopBack(&list, 3);
 PopBack(&list, 4);
 PrintList(list);
}
void TestList2()   //PushFront &  PopFront
{
 Node* list = NULL;
 PushFront(&list, 1);
 PushFront(&list, 2);
 PushFront(&list, 3);
 PushFront(&list, 4);
 PushFront(&list, 5);
 PrintList(list);
   
 PopFront(&list, 1);
 PopFront(&list, 2);
 PopFront(&list, 3);
 PopFront(&list, 4);
 PrintList(list);
}
void TestList3()   //
{
 Node* list = NULL;
 PushFront(&list, 1);
 PushFront(&list, 2);
 PushFront(&list, 3);
 PushFront(&list, 4);
 PushFront(&list, 5);
 PrintList(list);
 Node* pos = list;
 pos = pos->next;
 Insert(&list, 3, 30);
 PrintList(list);
}
#include<stdio.h>
#include<stdlib.h>
#include"test.h"
int main()
{
 //void TestList1();
 //void TestList2();
 void TestList3();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值