C语言实现链表




#pragma once
#include<assert.h>
typedef int DataType;
typedef struct SListNode
{
           DataType data;
           struct SListNode * next;
}SListNode;

SListNode* _BuyNode(DataType x)
{
           SListNode* tmp = (SListNode *)malloc(sizeof( SListNode));
          tmp->data = x;
          tmp->next = NULL;

           return tmp;
}
void PushBack(SListNode * &pHead, DataType x )//引用
{
           //1.没有节点
           //有结点
           if (pHead == NULL)
          {
                    pHead = _BuyNode(x );
          }
           else
          {
                    SListNode* tail = pHead ;
                    while (tail->next != NULL )
                   {
                             tail = tail->next;
                   }
                   tail->next = _BuyNode( x);
          }
}


void PrintSlist(SListNode * pHead)
{
           SListNode* cur = pHead ;
           while (cur)
          {
                   printf( "%d->", cur->data);
                   cur=cur->next;
          }
          printf( "NULL");
}

void PopBack(SListNode *& pHead)
{
           if  (pHead ==NULL)
          {
                    return;
          }
           else if (pHead->next == NULL)
          {
                   free( pHead);
                    pHead = NULL ;
          }
           else
          {
                    SListNode* tail = pHead ;
                    SListNode*prev = NULL ;
                    while (tail->next)
                   {
                             prev = tail;
                             tail = tail->next;
                   }
                   free(tail);
                   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)
          {
                    return;
          }
           else if (pHead->next == NULL)
          {
                   free( pHead);
                    pHead = NULL ;
          }
           else
          {
                    SListNode*del = pHead ;
                    pHead = pHead ->next;
                   free(del);
          }
}

void Invert(SListNode * pos, DataType x )
{
           assert(pos );
           SListNode* tmp = _BuyNode(x );
          tmp->next = pos->next;
           pos->next = tmp;
}


SListNode* Find(SListNode * pHead, DataType x )
{
           SListNode*cur = pHead ;
           while (cur)
          {
                    if (cur->data == x )
                   {
                              return cur;
                   }
                   cur = cur->next;
          }
           return NULL ;
}




void PrintFromTailToHead(SListNode *pHead)
{
           if (pHead )
          {
                   PrintFromTailToHead( pHead->next);
                   printf( "%d", pHead ->data);
          }
}



void DelNonTailNode(SListNode * pos)
{
           assert(pos );
           assert(pos ->next);
           SListNode*del = pos ->next;
           SListNode*next = del->next;
           pos->data = del->data;
          free(del);

}



void Insert

  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值