zoukankan      html  css  js  c++  java
  • 2.链表的插入

    insert.h
    #include <iostream>
    #include <ostream>
    using std::ostream;
    #define OK 1
    #define ERROR 0
    class myList
    {
    public:
       myList(){}
       myList(int v_data)
       {
          data = v_data;
       }
       void setNextValue(myList* nextValue)
       {
          next = nextValue;
       }
       friend ostream& operator <<(ostream &output, myList myList);
       //friend bool ListInsert(myList *l, int i, int e);
      friend bool ListInsert(myList *&l, int i, int e);
    //member variable myList* next; int data; };
    insert.cpp
    
    #include "insert.h"
    ostream& operator <<(ostream &output, myList myList)
    {
       output << myList.data;
       return output;
    }
    bool ListInsert(myList *L, int i, int e)//insert Node to position of Node 'i'
    {
       int j;
       myList* p, *s;
       p = L;
       j = 0;
       /*while (p && j < i - 1)// seek position of Node 'i'
       {
          p = p->next;
          j++;
       }*/
      if (i > 1)
       {
          while (p && j < i - 2)// seek position of Node 'i'
          {
             p = p->next;
             j++;
          }
       }
    if (!p || j > i) { return ERROR; } s = new myList(); s->data = e; if (0 == i - 1) { s->next = p; //p = s;
        L = s }
    else { s->next = p->next; p->next = s; } return OK; }
    main.cpp
    #include "insert.h"
    int main()
    {
       myList l1(1);
       myList l2(2);
       myList l3(3);
       l1.setNextValue(&l2);
       l2.setNextValue(&l3);
       l3.next = nullptr;
      // myList* firstPtr = new myList();
       //firstPtr = &l1;
      myList* firstPtr = &l1;
    if (ListInsert(firstPtr,1, 4)) { for (int i = 0; i < 4; ++i) { if (firstPtr != nullptr) { std::cout << *firstPtr << std::endl; firstPtr = firstPtr->next; } } } else { std::cout << "Error!"; } return 0; }
    CMakeLists
    cmake_minimum_required(VERSION 2.8)
    project (test1)
    aux_source_directory(. DIR_SRC)
    add_executable(insert ${DIR_SRC})
    install (TARGETS insert DESTINATION ${PROJECT_BINARY_DIR}/install)
    #install (FILES "${PROJECT_BINARY_DIR}/*.cpp ${PROJECT_BINARY_DIR}/*.h" DESTINATION ${PROJECT_BINARY_DIR}/build)
    set(CMAKE_BUILD_TYPE "Debug")
    set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")

     之前错误分析:

    #ifndef MYLIST_H
    #define MYLIST_H
    
    struct Node
    {
        int data;
        struct Node* next;
    };
    class mylist
    {
    public:
        void initList();
        void insertList(int data);
        Node* searchList();
        void traverseList();
        void destroyList();
    private:
        Node *head;
    };
    
    #endif // MYLIST_H
    #include "mylist.h"
    #include <iostream>
    void mylist::initList()
    {
        head = new Node;
        head->next = nullptr;
    }
    
    void mylist::insertList(int data)
    {
        Node * cur = new Node;
        cur->data = data;
        cur->next = head->next;
        head->next = cur;
    }
    
    void mylist::traverseList()
    {
        Node *sh = head->next;
        while(sh)
        {
            std::cout << sh->data << std::endl;
            sh = sh->next;
        }
    }
    
    void mylist::destroyList()
    {
        Node *t;
        while(head)
        {
            t = head;
            head = head->next;
            delete t;
        }
    
    }
    #include <iostream>
    #include "mylist.h"
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        mylist l;
        l.initList();
        for (int i = 0; i < 10; i++)
            l.insertList(i);
    
        l.traverseList();
        l.traverseList();
        l.destroyList();
        return 0;
    }
  • 相关阅读:
    关于form表单的相同name问题
    MySQL数据库视图
    Blazor
    查看Oracle正在执行的任务
    比较不错的几款开源的WPF Charts报表控件
    Raft算法
    EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right outer join)、多表查询
    systemd、upstart和system V 枯木
    MRTG生成首页报错解决方法 枯木
    dd备份和恢复 枯木
  • 原文地址:https://www.cnblogs.com/aelite/p/9484748.html
Copyright © 2011-2022 走看看