单链表的增删操作

单链表是一种链式的存储结构,由若干个结点构成,每个结点包含数据域和指针域两个部分,下面对单链表实现头插,尾插,头删,尾删操作。

....SList.h
#pragam once

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <malloc.h>

typedef int SLDataType;

typedef struct SListNode
{
 SLDataType _data;
 struct SListNode* _next;
}SListNode;

typedef struct SList
{
  SListNode *_head;
}SList;

void SListInit (SList *plist,SLDataType x);

SListNode *Buynode (x);
void SListpushback (Slist *plist,SLDataType x);
void SListpushfront (SList *plist,SLDataType x);
void SListpopfront (SList *plist)void SListpopback (SList *plist);
void SListPrint (SList *plist);
void SListDestory (SList *plist);



....list.c
#include "SList.h"
SList plist;

void SListInit (SList *plist,SLDataType x)//单链表的初始化
{
  assert (plist);
  plist->_head =NULL;
}

void SListPrint (SList *plist)//单链表的打印
{
  assert (plist);
  SListNode *cur = plist->_head;
  while (cur != NULL)
  {
   printf ("%d->",cur->_data);
   cur=cur->_next;
  }
  printf ("NULL\n");
}

void SListDestory (SList *plist)//单链表的销毁
{
 assert (plist);
 SListNode *cur = plist->_head;
 SListNOde *dst=NULL;
 while (cur!=NULL)
 {
  dst=cur->_next;
  free(cur);
  cur=dst;
 }
 plist->_head=NULL;
}

SListNode *Buynode (x)//设置一个产生新结点的函数
{
 SListnode *pnode = (SListNode *)malloc (sizeof (SListNode));//该结点为指针类型
 pnode->_data=x;
 pnode->_next=NULL;
 return pnode;
 }

void SListpushback (Slist *plist,SLDataType x)//单链表的尾插
{//分为有结点和无结点两种情况
 assert (plist);
 SListNode *newnode = Buynode (x);
 //无结点
 SListNode *tail =plist->_head;
 if (tail==NULL)
 {
   plist->_head=newnode;
 }
else//有结点
 {
  while (tail->_next!=NULL)
  {
   tail=tail->_next;
  }
  tail->_next=newnode;
  newnode->_next = NULL;
 }
}

void SListpushfront (SList *plist,SLDataType x)//单链表的头插
{
 assert (plist);
 SListNode *newnode =Buynode (x);
 SListNode *cur =plist->_head;
 newnode->_next=cur;
 plist->_head=newnode;
}
 
void SListpopfront (SList *plist)//单链表的头删
{
 assert (plist);
 SListNode *cur = plist->_head;
 SlistNode *dst=NULL;
 dst=cur->_next;
 free (cur);
 cur=dst;
 plist->_head=cur;
 }

void SListpopback (SList *plist)//单链表的尾删
{
 assert (plist);
 SListNode *tail =plist->_head;
 if (tail->_next==NULL)
 {
  free (tail);
  plist->_head = NULL;
 }
 while (tail->_next->_next!=NULL)
 {
  tail=tail->_next;
 }
 free (tail->_next);
 tail->_next=NULL;
}

void test ( )
{
 void SListInit (plist,8);
 
 void SListpushback (plist,1);
 void SListpushback (plist,2);
 void SListpushback (plist,3);
 void SListpushback (plist,4);
 void SListpushback (plist,5);
 void SListpushfront (plist,6);
 void SListpopfront (plist)void SListpopback (plist);
 
 void SListPrint (plist);
 void SListDestory (plist);
 
 
....test.c
#include "SList.h"
int main ( )
{
 test ( );
 systrm ("pause");
 return 0;
}

上述运行结果为:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单链表是一种常用的数据结构,它由一系列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。单链表操作包括增加节点、删除节点、修改节点和查找节点。下面是C语言实现单链表增删改查示例代码: ```c #include <stdio.h> #include <stdlib.h> // 节点结构体 typedef struct node { int data; // 数据元素 struct node* next; // 指向下一个节点的指针 } Node; // 在单链表末尾增加节点 void addNode(Node** head, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; if (*head == NULL) { *head = newNode; } else { Node* current = *head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } // 在单链表指定位置插入节点 void insertNode(Node** head, int index, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; if (index == 0) { newNode->next = *head; *head = newNode; } else { Node* current = *head; for (int i = 0; i < index - 1 && current != NULL; i++) { current = current->next; } if (current != NULL) { newNode->next = current->next; current->next = newNode; } else { printf("Index out of range\n"); } } } // 在单链表指定位置删除节点 void deleteNode(Node** head, int index) { if (*head == NULL) { printf("List is empty\n"); return; } Node* current = *head; Node* previous = NULL; if (index == 0) { *head = current->next; free(current); } else { for (int i = 0; i < index && current != NULL; i++) { previous = current; current = current->next; } if (current != NULL) { previous->next = current->next; free(current); } else { printf("Index out of range\n"); } } } // 修改单链表指定位置节点的值 void modifyNode(Node* head, int index, int data) { if (head == NULL) { printf("List is empty\n"); return; } Node* current = head; for (int i = 0; i < index && current != NULL; i++) { current = current->next; } if (current != NULL) { current->data = data; } else { printf("Index out of range\n"); } } // 查找单链表指定位置节点的值 int searchNode(Node* head, int index) { if (head == NULL) { printf("List is empty\n"); return -1; } Node* current = head; for (int i = 0; i < index && current != NULL; i++) { current = current->next; } if (current != NULL) { return current->data; } else { printf("Index out of range\n"); return -1; } } // 打印单链表 void printList(Node* head) { if (head == NULL) { printf("List is empty\n"); return; } Node* current = head; printf("List: "); while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { Node* head = NULL; // 在单链表末尾增加节点 addNode(&head, 1); addNode(&head, 2); addNode(&head, 3); printList(head); // 在单链表指定位置插入节点 insertNode(&head, 1, 4); insertNode(&head, 0, 5); insertNode(&head, 6, 6); printList(head); // 在单链表指定位置删除节点 deleteNode(&head, 2); deleteNode(&head, 0); deleteNode(&head, 4); printList(head); // 修改单链表指定位置节点的值 modifyNode(head, 1, 7); modifyNode(head, 3, 8); modifyNode(head, 5, 9); printList(head); // 查找单链表指定位置节点的值 printf("Value at index 2: %d\n", searchNode(head, 2)); printf("Value at index 5: %d\n", searchNode(head, 5)); printf("Value at index 7: %d\n", searchNode(head, 7)); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值