반응형





링크드 리스트 소스 구연했던 예제입니다. 


구연 다안되어 있지만.. 


참고 하실분은 참고 하세요.


list.h

#ifndef LIST_H

#define LIST_H
#include <node.h>
class List
{
public:
    List(){
        std::cout<<"list :1";
       _head=NULL;
    }

    List(Node *appendNode){
        std::cout<<"list :2";
    }

    Node *headNode() const;
    Node *appendNode(const std::string &text);
    Node *insertNode(const std::string &text, Node *beforeNode);
    Node *deleteNode(Node *node); //삭제

    void print();


private:
    Node *_head;
};


#endif // LIST_H


list.cpp


#include "list.h"
#include <iostream>



Node *List::appendNode(const std::string &text)
{

    Node *newNode = new Node(text);

    if (!_head) {
        std::cout<<"\nappendNode : _head=null";
        _head=newNode;
        _head->text();

    } else {
        std::cout << "_head : NULL 아님 ";
        Node *ptr =_head;
        while(ptr->next())
            ptr =ptr->next();
        ptr->_next=newNode;
        newNode->_prev=ptr;
    }
    return newNode;
}

Node *List::headNode() const{

    return _head;

}
void List::print(){

    for(Node *ptr=_head;ptr;ptr=ptr->next()){
        ptr->text();
    }

}

 Node *List::deleteNode(Node *node){
     Node *next = node->next();
     if(node->_prev)
     {
        node->_prev->_next=node->_next;

     }
     if(node->_next){
         node->_next->_prev=node->_prev;
     }
     if(!node->prev()){
         _head=next;
     }
     delete node;

     return next;

 }




node.h

#ifndef NODE_H
#define NODE_H
#include <iostream>
#include <string>


class Node
{
    friend class List;
private:

    Node(){
        this->setText("");
        std::cout<<"\nin?";
        this->text();
        _prev=0;
        _next=0;
    }

    Node(std::string str){
        _prev=0;
        _next=0;
        _str=str;
        std::cout<< "\n 기본생성자 아님"<< _str;
    }

public:

    std::string text() const;
    void setText(const std::string &text);

    Node *next() const;
    Node *prev() const;

private:

    Node *_prev;
    Node *_next;
    std::string _str;
};

#endif // NODE_H



node.cpp


#include "node.h"

void Node::setText(const std::string &text){

//    if(text==""){
//        std::cout<<"없음";
        _str=text;
//    }

}


std::string Node::text() const
{


    std::cout<< "\nNode str  :"<< _str<<"\n";
    return _str;


}




Node *Node::prev() const
{
    return this->_prev;
}


Node *Node::next() const
{
    return this->_next;
}







main.cpp


#include <iostream>
#include <list.h>
#include <node.h>

using namespace std;

int main()
{
    int select=0;
    std::string data="";

    List lt,lt2;

    while(true){

        cout << "================"<<"\n";
        cout << "1   노드 추가 \n";
        cout << "2   노드 보기 \n";
        cout << "3   노드 삽입 \n";
        cout << "4   노드 삭제 \n";
        cout << "5   다음 노드 \n";
        cout << "6   이전 노드 \n";
        cout << "================"<<"\n";
        cout << "?  ";
        cin >>select;

        cout << "\n";

        switch(select){

        case 1:{
            cout << " 추가할 노드의 이름 ?   ";
            cin >>data;
            cout <<"\n";

            lt.appendNode(data);
            break;
        }
        case 2:

            lt.print();
            break;

        case 3:
        {

            break;
        }

        case 4:{
            int _data;
            std::cout << "삭제할 노드 ?";
            std::cin >> _data;
            Node *node = lt.headNode();
            if(!node){
                std::cout<<"null ";
                break;
            }
            for(int i = 1; i<_data;i++)
            {

                node = node->next();
                if(!node){
                    std::cout<<"null ";
                    break;
                }
            }

            lt.deleteNode(node);
            break;
        }
        case 5:
            break;
        case 6:
            break;

        default:
            break;

        }



    }

    return 0;
}






반응형
반응형

#include <iostream>

using namespace std;

//간략... 네임 스페이스는 명칭의 선언 영역을 분리하여 충돌을 방지한다.

//  자세한 설명은 http://www.winapi.co.kr/clec/cpp3/34-1-3.htm

자세히 설명되어 있네요 .


//과일이라는 클래스를 만들었습니다.
class fruit{

private :       // 공개안함!!!

   int apple;         
   int strawberry;

public :     //공개

   void Setfruit(int apple_arg,int strawberry_arg);  // 선언정도 우선 생각하세요 ..
   int getApple(void);     //  사과의 개수를 가지고 오기 만들었어요
   int getstrawberry(void);  //딸기의 개수를 가지고 오기 만들었어요..
};

//위에 선언한 void Setfruit를 사용할꺼에요 .

//값을 넣는곳! 이라고  우선 단순히!!  생각하세요!

void fruit::Setfruit(int apple_arg,int strawberry_arg){

//받은 값을 사과라는 변수에 넣는 작업!!
   apple=apple_arg;

//받은 값을 딸기 라는 변수에 넣는 작업!!
   strawberry=strawberry_arg;
}

//사과의 개수가 몇개인지 그냥 리턴해 주는 곳
int fruit ::getApple(void)
{

   return apple;

}

//딸기의 개수가 몇개인지 그냥 리턴해 주는 곳
int fruit::getstrawberry(void){
   return strawberry;
}

int main()
{


   fruit box1,box2; //과일의 객체생성!!!

//box1 이라는  박스와 box2 이라는 곳에 과일을 담을 꺼에요


   box1.Setfruit(20,30); 

// box1 박스에 사과 20개와 딸기 30개를 담는다고 하였습니다.

//그러면 Setfruit(int apple_arg,int strawberry_arg)   멤버함수에 들어가  

//apple=apple_arg;    사과의 개수가 20개가 됩니다.   

   box2.Setfruit(50,0);

//위와 동일합니다. box2에 설정한 값입니다.


   cout<< "박스1\n";
   cout<<"사과 개수" <<box1.getApple()<< " , "<< box1.getstrawberry();

//box1에 getApple() 하여 사과개수가 몇개 인지 불러오고 있습니다.

//나머지도 동일합니다.

   cout <<"\n";

   cout<< "박스2\n";
   cout<<"사과 개수" <<box2.getApple()<< " , "<< box2.getstrawberry();

   cout <<"\n";


   return 0;
}




반응형
반응형

*구조체란
 관련된 데이터를 묶어 놓은것


*클래스란
 구조체를 확장시켜 놓은것



클래스 = 멤버변수 + 멤버함수



class 선언 형식
class  클래스이름
{

    [액세스 지정자 : ]

    자료 멤버 변수 ;
   

     [액세스 지정자:]
       자료형 멤버 함수;
};
자료형 클래스 이름 :: 멤버함수(){
}


디폴트 접근 지정자

클래스내 멤버 변수를 선언할때 생략할 경우 디폴트로 private 멤버가 된다.
class point
    int x;
    int y;
}


액세스 지정자 권한 이라고 보면된다 .

액세스지정자     현재 클래스   클래스 밖
 public                      ㅇ               ㅇ
 private                      ㅇ               X





반응형
반응형



이젠 마음을 굳게 먹고 끝나지 않는 길을 걷을 생각 입니다. 

과연 내가 잘해낼 수 있을지 . 

앞으로 어떤 역경과 고난이 올지 .



보다는..

평생 즐기면서 하고 싶네요...


반응형

+ Recent posts