链表 - 功能 - 遍历 - 创建 - 插入 - 长度 - 删除

2017-08-28 17:36:28

writer:pprp

这是我第二次完全独立的去写链表,除了链表的排序部分以外,

感觉比较顺利,这种模块化的写法比较令人舒畅

这个链表储存的是一个工人的工号和他的工资,进行一系列操作

代码和说明如下:

/*
@theme: PrintList
@writer:pprp
@start:17:20
@end:17:32
@declare:链表的删除一个节点
@date:2017/8/28
*/

#include <bits/stdc++.h>

using namespace std;

struct worker
{
    int num;//worker's number
    double wage; // worker's wage
    worker(int nn, double ww):num(nn), wage(ww)
    {
        next = NULL;
    }
    worker* next;
    bool operator < (worker wk)
    {
        return num < wk.num ? true:false;
        //        else
//        {
//            return wage < wk.wage? true:false;
//        }

    }
};

//function 1: create a PrintList
//test : ok
worker* create()
{
    worker * head = NULL;
    worker *transe = NULL;
    int nn;
    double ww;

    for(int i = 0 ; i < 5; i++)
    {
        cin >> nn >> ww;
        transe = head;
        worker* tmp = new worker(nn,ww);
        if(head == NULL)
        {
            head = tmp;
        }
        else
        {
            while(transe->next != NULL)
            {
                transe = transe->next;
            }
            transe->next = tmp;
        }
    }
    return head;
}

//function 2: show the whole PrintList
//test :ok
void PrintList(worker* head)
{
    worker * tmp;
    tmp = head;
    while(tmp->next != NULL)//果然因为这里的原因才导致少输出一行
    {
        cout << tmp->num << "->" << tmp->wage << endl;
        tmp = tmp->next;
    }
    cout << tmp->num << "->" << tmp->wage << endl;

    return ;
}

//function 3: insert a new node
//test:ok
worker* Insert(worker * head)
{
    int nn;
    double ww;

    cin >> nn >> ww;

    worker* tmp = new worker(nn, ww);

    if(head == NULL)
    {
        head = tmp;
    }
    else
    {
        worker * transe = head;
        while(transe->next != NULL)
        {
            transe = transe->next;
        }
        transe->next = tmp;
    }
    return head;
}

int SizePrintList(worker* head);
//function 3 sort
// test :ok
worker * bubble_sort(worker*head)
{
    int cnt = SizePrintList(head);
    worker * pMove = head->next;
    while(cnt > 1)
    {
          while(pMove->next != NULL)
          {
                if(pMove->num > pMove->next->num)
                {
                      int tmp = pMove->num;
                      pMove->num =pMove->next->num;
                      pMove->next->num = tmp;
                }
                
                if(pMove->next->wage == pMove->wage)
                {
                      double tmp = pMove->wage;
                      pMove->wage = pMove->next->wage;
                      pMove->next->wage = tmp;
                }
                pMove = pMove->next;
          }
          cnt--;
          pMove = head->next;
    }
    
    return head;
}

//function 4 size
//test:ok
int SizePrintList(worker* head)
{
       int cnt = 0;
       worker *it = head;
       while( it != NULL)
       {
             cnt++;
             it = it->next;
       }
       return cnt;
}

//function 5 delete 
//根据num的值比对进行删除,一开始忽略了对头结点的判断,所以现在加上单独判断头结点
//test
worker* delete_List(worker * head, int value)
{
     worker * fast = head->next;
     worker * slow = head;
     
     if(head->num == value)
     {
           head = fast;
           delete(slow);
           return head;
     }
     
     while(fast != NULL)
     {
           if(fast->num == value)
           {
                 slow->next = fast->next;
                 delete(fast);
                 break; 
           }           
           fast = fast->next;
           slow = slow->next;
     }
     return head;
}


int main()
{
    freopen("in.txt","r",stdin);
    worker *head = create(); //head iterator
    PrintList(head);
    cout << "-----" << endl;
    head = Insert(head);
    PrintList(head);
    cout << "-----" << endl;
//  sort(head, head + 6);
//  PrintList(head);
    bubble_sort(head);
    PrintList(head);
    cout << "-----" << endl;
    head = delete_List(head,101);
    PrintList(head);
    cout << "-----" << endl;

    return 0;
}

/*
数据
101 580.50
103 1000.08
105 888.88
107 1888.88
109 518.23
插入的新数据
102 100.123
*/

 

posted @ 2017-08-28 17:40  pprp  阅读(232)  评论(0编辑  收藏  举报