연결 리스트 - 스택 본문

Programming/기타 언어들

연결 리스트 - 스택

쩡호 2017. 4. 23. 17:21

Student.h 파일


#include <cstdio>

#include<cstdlib>

#include<cstring>

#include<iostream>

using namespace std;


const int MAX_STRING = 100;


class Student {

int id;

char name[MAX_STRING];

char dept[MAX_STRING];

public:

Student(int i = 0, char* n = "", char* d = "")

{

set(i, n, d);

}

void set(int i, char* n, char* d) {

id = i;

strcpy_s(name, n);

strcpy_s(dept, d);

}

void display() {

cout << "학번 : " << id << "  이름 : " << name 

<< "  학과 : " << dept << endl;

}

};



Node.h 파일


#include"Student.h"

class Node :public Student {

Node* link; //다음 노드를 가리킴

public:

Node(int id = 0, char* name = "", char* dept = "")

:Student(id, name, dept)

{

link = NULL;

}

~Node() {}

Node* getLink() { return link; }

void setLink(Node* p) { link = p; }

};




LinkedStack.h 파일


#include<iostream>

#include"Node.h"

using namespace std;


class LinkedStack {

Node* top;

public:

LinkedStack() { top = NULL; }

~LinkedStack() { while (!isEmpty()) delete pop(); }

bool isEmpty() { return top == NULL; }

void push(Node* p) {

if (isEmpty())

top = p;

else {

p->setLink(top);

top = p;

}

}


Node* pop() {

if (isEmpty()) return NULL;

Node* p = top;

top = top->getLink();

return p;

}


Node* peek() { return top; }

void display() {

cout << "[LinkedStack]" << endl;

for (Node* p = top; p != NULL; p = p->getLink())

p->display();

cout << endl;

}

};




'Programming > 기타 언어들' 카테고리의 다른 글

언리얼의 데이터 유형  (0) 2017.05.18
연결 리스트 - 큐  (0) 2017.04.23
C++ rand() 함수  (0) 2017.04.23
c++ 소수점 아래 고정 하기  (0) 2017.04.15
C++ 자리수 다르게 출력  (0) 2017.04.12
Comments