单链表的基本操作

来源:互联网 发布:淘宝上100多的丛发气瓶 编辑:程序博客网 时间:2024/06/07 06:19
#pragma once#include<stdlib.h>#include<stdio.h>#include<assert.h>#include<malloc.h>typedef int Datatype;typedef struct SListNode{Datatype data;SListNode *next;}SListNode;SListNode* BuyNode(Datatype x);//申请空间void PrintNode(SListNode*& pHead);//打印链表void CreatNode(SListNode*& pHead);//创建表void DestoryNode(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);//指定位置插入void Delete(SListNode*& pHead, SListNode*& pos);//删除指定位置void Remove(SListNode*& pHead, Datatype x);//删除xvoid Search(SListNode*& pHead, Datatype x);//查找X

#include"SNode.h"SListNode* BuyNode(Datatype x){SListNode* Node = (SListNode*)malloc(sizeof(SListNode));Node->data = x;Node->next = NULL;return Node;}void CreatNode(SListNode*& pHead){pHead = NULL;}void PrintNode(SListNode*& pHead)//打印链表{SListNode* cur = pHead;while (cur){printf("%d", cur->data);cur = cur->next;}printf("\n");}void DestoryNode(SListNode*& pHead)//销毁{SListNode* cur = pHead;while (cur){SListNode* temp = cur;cur = cur->next;free(temp);temp = 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){if (pHead == NULL){printf("The List is Empty\n");return;}else if (pHead->next == NULL){free(pHead);pHead = NULL;}else{SListNode* pre=NULL;SListNode* cur=pHead;while (cur->next){pre = cur;cur = cur->next;}free(cur);pre->next = NULL;}}void PushFront(SListNode*& pHead, Datatype x){if (pHead == NULL)pHead = BuyNode(x);else{SListNode* temp;temp = BuyNode(x);temp->next = pHead;pHead = temp;}}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* temp = pHead->next;free(pHead);pHead = temp;}}void Insert(SListNode*& pHead, SListNode*& pos, Datatype x)//指定位置插入{assert(pos);assert(pHead);if (pos == pHead){PushFront(pHead, x);}else{SListNode* pre = NULL;SListNode* cur = NULL;cur = pHead;pre = pHead;while (cur != pos){pre = cur;cur = cur->next;}SListNode* temp = NULL;temp = BuyNode(x);temp->next = cur;pre->next = temp;}}void Delete(SListNode*& pHead, SListNode*& pos)//删除指定位置{assert(pos);assert(pHead);if (pos == pHead){free(pHead);pHead = NULL;}else{SListNode* pre, *cur;cur = pHead;while (cur!=pos){pre = cur;cur = cur->next;}pre->next = cur->next;free(cur);}}void Remove(SListNode*& pHead, Datatype x)//删除x{if (pHead == NULL){printf("The List is Empty\n");}else if (pHead->data == x){SListNode* next = pHead->next;free(pHead);pHead = next;}else{SListNode* pre, *cur;cur = pHead;while (cur->data != x){pre = cur;cur = cur->next;}pre->next = cur->next;free(cur);}}SListNode* Search(SListNode*& pHead, Datatype x)//查找X{SListNode* cur = pHead;while (cur){if (cur->data == x){return cur;}cur = cur->next;}return NULL;}

0 0
原创粉丝点击