c++单链表的基本操作

来源:互联网 发布:ubuntu源文件在哪 编辑:程序博客网 时间:2024/06/07 06:17

#include<iostream>

#include<stdlib.h>

#include<stdio.h>

using namespace std;

typedef struct node{

    int value;

    struct  node* next;

}Node;

typedef struct{

   Node* head;

   Node*  tail;

   int size;

}LinkedList;

LinkedList* NewLinkedLIst(){

        LinkedList* tList=new LinkedLIst;//!!!LinkedList* tList=0;记住无论什么类型空指针都好,都不配拥有任何属性

       /*tList->head=0;
tList->tail=0;如果是这样的话不好插入*/

        tList->head=new Node;//!!!!0->next=0是大错特错的

        tList->head->next=0;

        tList->tail=tList->head;//方便尾插法

        tList->size=0;

        return tList;

}

bool BubbleSortList(LinkedList*& list){

       Node* p=list->head->next;

      if(0==p)return false;

      if(0==p->next)return true;

      Node*  tag=0;

      for(;p->next;p=p->next){//n-1次冒泡 

               for(Node* q=p->next;q;q=q->next){

                        if(tag->value>q->value)tag=q;

              }

              if(tag==p)continue;

              int tmp=p->value;

              p->value=tag->value;

              tag->value=tmp;

     }

      return false;

}

void Combine(LinkedList list1,LinkedList list2){

       Node* p=list1->tail;

       Node* q=list2->head->next;

       p->next=q;

}

bool Reverse(LinkedList*& list){//传递指针的引用过来是为了控制这个指针

          if(0==list->head->next)return false;

          if(0==list->head->next->next)return true;

          Node* pre=list->head->next;

          Node* mid=list->head-next->next;

          Node* pro=mid->next;

          while(mid){

                mid->next=pre;

                pre=mid;

                mid=pro;

                if(pro)

                          pro=pro->next;

        }

        list->head->next->next=0;

        list->tail=list->head->next;

        list->head->next=pre;

         return true;

}

bool Remove(LinkedList*& list,int value){

    if(0==list->size)goto END;//转向语句不允许跳同层含初始化区域

        {//加了一层无意义的花括号以便使goto能编译过去 

                Node*p=list->head;

                Node*q=list->head->next;

                while(q){

                              if(value==q->value){

                                       p->next=q->next;

                                       if(0==q->next) 

                                       list->tail=p;

                                       delete q;

                                       q=0;//请百度野指针

                                       list->size--;

                                       return true;

                            }

                            q=q->next;

               }

         }

END:printf("Did't find this value in the list!\n");

return false;

}

LinkedList* insert(LinkedList* list,int value){

      Node* tNode=new Node;//new 的三部曲:申请内存,调用构造函数,返回指针//Node* tNode=new Node();都行

      tNode->value=value;

      tNode->next=0;

      list->tail->next=tNode;

      list->size++;

       return list;

}

void Print(LinkedList* list){

     Node* p=list->head->next;

     if(0==p)return;

     while(p){

             printf("%d ",p->value);

             p=p->next;

      }

      printf("\n");

}

int main(){

     LinkedList* lList=0;

     lList=NewLinkedList();

     lList=Insert(lList,100);

     lList=Insert(lList,1);

     lList=Insert(lList,10);

     lList=Insert(lList,80);

     Print(lList);

     Remove(lList,99);

     Reverse(lList);

     Print(lList);

     BubbleSortList(lList);

     Print(lList);

     return 0;

}




































0 0
原创粉丝点击