반응형
|
|
링크드 리스트 소스 구연했던 예제입니다.
구연 다안되어 있지만..
참고 하실분은 참고 하세요.
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;
}
반응형
'programming > c++' 카테고리의 다른 글
프로그램에서 변수란. (0) | 2014.01.21 |
---|---|
메세지 주의에 테두리를 그리는 프로그램 (0) | 2012.08.01 |
클레스 설계와 객체 사용 예제 (0) | 2012.04.26 |
[기초] 구조체 그리고 클래스 (0) | 2012.04.23 |
cout 간단한 출력.!! (0) | 2012.04.06 |
인터프리터와 컴파일러! 그리고 링커 (0) | 2012.03.29 |
다시 c++ 공부를 시작하게 됬어요 ... (0) | 2012.03.28 |