单链表相关操作代码

#include <iostream>
#include <cassert>
using namespace std;

struct node{
    int          data;  // data 
    struct node *next; // link to next
};

int Length(struct node* head);
struct node* BuildOneTwoThree(int iNodeCount);
void Push(struct node** headRef, int newData);
struct node* AppendNode(struct node** headRef, int num);
int Count(struct node* head, int searchFor);
int GetNth(struct node* head, int index);
void DeleteList(struct node** headRef) ;
int Pop(struct node** headRef) ;
void InsertNth(struct node** headRef, int index, int data) ;

void SortedInsert(struct node** headRef, struct node* newNode);
void Append(struct node** aRef, struct node** bRef) ;
void FrontBackSplit(struct node* source,
struct node** frontRef, struct node** backRef);

void RemoveDuplicates(struct node* head) ;
void MoveNode(struct node** destRef, struct node** sourceRef);
struct node* ShuffleMerge(struct node* a, struct node* b) ;
static void Reverse(struct node** headRef) ;
void main()
{
   struct node* iLink = BuildOneTwoThree(6);
   Reverse( &iLink);
  // InsertNth( &iLink,20,18);
   int iLength = Length(iLink);
   cout << iLength<<endl;

    return ;
}

void Reverse(struct node** headRef)
{
  struct node* current = *headRef;
  struct node* next   = NULL;
  struct node* result   = NULL;

  while( current != NULL)
  {
      next    = current->next ;
      current->next  = result;
      result = current;
      current = next;
  }
  *headRef = result;
}
struct node* ShuffleMerge(struct node* a, struct node* b) 
{
    struct node* ResultNode = NULL;

    while(1)
    {
        if ( a == NULL)
        {
            ResultNode->next = b;
            break;
        }else if ( b == NULL )
        {
            ResultNode->next = a;
            break;
        }else
        {
            ResultNode = a;
            a = a->next;
            ResultNode = ResultNode->next;
            ResultNode = b;
            b = b->next;
            ResultNode = ResultNode->next;
        }
    }

    return (ResultNode);
    
}

void MoveNode(struct node** destRef, struct node** sourceRef)
{
    if ( *sourceRef == NULL )
    {
        return ;
    }

    struct node* head = *sourceRef;
    struct node* insertNode = *sourceRef;
    insertNode->next = *sourceRef;
    *destRef = insertNode;
    
}
void RemoveDuplicates(struct node* head)
{
    struct node* current = head;
    if ( current == NULL || current->next == NULL)
    {
        return ;
    }

    while( current->next != NULL)
    {
       if ( current->data == current->next->data )
       {
           struct node* freeNode = current->next;
           current->next = current->next->next;
           free(freeNode);
       }else
       {
           current = current->next;
       }
        
    }
}

void FrontBackSplit(struct node* source,
struct node** frontRef, struct node** backRef)
{
    struct node* current = source;
    int ilength = Length(source);
    if ( ilength < 2 )
    {
        *frontRef = source;
        *backRef  = NULL;
    }else
    {
        int hopcount = (ilength - 1)/2;
        for ( int i = 0;i < hopcount;i++)
        {
            current = current->next;
        }
        *frontRef = source;
        *backRef  = current->next;
        current->next = NULL;

    }

}
void Append(struct node** aRef, struct node** bRef) 
{
    if ( *aRef == NULL)
    {
       *aRef = *bRef;
    }

    struct node *aCurrent = *aRef;
    while(aCurrent)
    {
        aCurrent = aCurrent->next;
    }
    aCurrent = *bRef;
    *bRef = NULL;
}
void SortedInsert(struct node** headRef, struct node* newNode)
{
    struct node* current = *headRef;
    if ( current == NULL || current->next->data > newNode->data )
    {
        newNode->next = current;
        current = newNode ;
    }else
    {
        while( current->next != NULL && current->next->data < newNode->data)
        {
            current = current->next;
        }
        newNode->next = current->next;
        current->next = newNode;
    }

}

void InsertNth(struct node** headRef, int index, int data) 
{
    struct node* current = *headRef;
    struct node* Node = NULL;

    if ( current == NULL )
    {
        Node = (struct node*)malloc(sizeof(struct node));
        Node->data = data;
        Node->next = NULL;
        
        current = Node;
    }
    struct node* PreNode = NULL;

    int nth = 0;
    while( current != NULL )
    {
        if( nth++ == index -1 )
        {
            Node = (struct node*)malloc(sizeof(struct node));
            Node->data = data;
            Node->next = current->next;
            current->next = Node;
            break;
        }

        PreNode = current;
        current = current->next;
    }
    if ( nth < index-1 )
    {
        Node = (struct node*)malloc(sizeof(struct node));
        Node->data = data;
        Node->next = NULL;
        PreNode->next = Node;
    }
}

int Pop(struct node** headRef) 
{
    struct node* current = *headRef;
    assert(current != NULL);
    int data = current->data;
    struct node* head = current->next;
    free(current);
    *headRef = head;
    return data;
}

void DeleteList(struct node** headRef)
{
    struct node* current = *headRef;

    struct node* temp;
    while( current )
    {
        temp = current;
        current  = current->next;
        free(temp);
    }
    *headRef = NULL;
}
int GetNth(struct node* head, int index)
{
    struct node* current = head;

    int nth = 0;
    while( current )
    {
        if ( nth++ == index )
        {
            return current->data;
        }
        current = current->next;
    }
    return -1;
}


int Count(struct node* head, int searchFor)
{
    int count = 0;
    if ( head == NULL )
    {
        return count;
    }

    struct node* current = head;

    while( current )
    {
        if ( current->data == searchFor )
        {
            count++;
        }
        current = current->next;
    }

    return count;
}
int Length(struct node* head)
{
    struct node *current = head;

    int iCount = 0;
    while( current != NULL )
    {
        iCount ++;
        current = current->next;
    }

    return iCount;
}


struct node* BuildOneTwoThree( int iNodeCount )
{
    struct node *head = NULL;
    struct node *tail = NULL;

    Push(&head,1);
    tail = head;

    int iIndex = 2;
    while( iIndex <= iNodeCount )
    {
       Push(&(tail->next),iIndex);
       tail = tail->next;
       iIndex++;
    }
    return (head);
}

void Push(struct node** headRef, int newData)
{
   struct node* newNode = (struct node*)malloc(sizeof(struct node));

   newNode->data = newData;
   newNode->next = *headRef;
        *headRef = newNode;
}

struct node* AppendNode(struct node** headRef, int num)
{
    struct node* current = *headRef;
    struct node* newNode;
    newNode = (struct node*)malloc(sizeof(struct node));

    newNode->data = num;
    newNode->next = NULL;


    if ( current == NULL )
    {
        *headRef = newNode;
    }else
    {
        while( current != NULL)
        {
            current = current->next;
        }
    }
    current = newNode;

    return(*headRef);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值