关闭

C语言实现链表

77人阅读 评论(0) 收藏 举报
分类:




#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

  


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23331次
    • 积分:1866
    • 等级:
    • 排名:千里之外
    • 原创:154篇
    • 转载:25篇
    • 译文:0篇
    • 评论:0条
    文章分类