메모리의 동적 할당을 이용한 c언어 코드
ArrayList.h
#ifndef __LINKED_LIST_H__
#define __LINKED_LIST_H__
#define TRUE 1
#define FALSE 0
typedef int LData;
typedef struct _node
{
LData data;
struct _node *next; // 다음 노드를 가르킬 노드 변수
} Node;
typedef struct __ArayList
{
Node *head; // 맨 앞 노드를 가르키는 구조체
Node *cur; // 현재 노드를 가르키는 구조체
Node *before; // 삭제시에 필요한 구조체 변수로 현재 위치 이전을 가르킨다.
int numOfData; // 데이터의 갯수파악을 위한 변수
} LinkedList;
typedef LinkedList List;
void ListInit(List *plist);
void LInsert(List *plist, LData data);
int LFirst(List *plist, LData *pdata);
int LNext(List *plist, LData *pdata);
LData LRemove(List *plist);
int LCount(List *plist);
#endif
ArrayList.c
#include <stdio.h>
#include <stdlib.h>
#include "LinkedList.h"
void ListInit(List *plist)
{
// head의 더미 노드를 삽입 함으로써 삭제 삽입시 코드의 길이를 줄여줄 수 있다.
plist->head = (Node *)malloc(sizeof(Node));
plist->head->next = NULL;
plist->numOfData = 0;
}
void LInsert(List *plist, LData data)
{
Node *newNode = (Node *)malloc(sizeof(Node)); // 메모리 할당
newNode->data = data; // 값 저장
newNode->next = plist->head->next; // 새로운 노드가 head의 다음 노드를 가르킨다.
plist->head->next = newNode; // head가 새로운 노드를 가르킨다.
(plist->numOfData)++; // 갯수 증가
}
int LFirst(List *plist, LData *pdata)
{
if (plist->head->next == NULL)
return FALSE;
// cur을 맨 앞 노드, before를 그 전 노드인 헤드를 가르키게끔 위치 시킨다.
plist->before = plist->head;
plist->cur = plist->head->next;
*pdata = plist->cur->data; // 주솟 값으로 받아온 변수의 값을 변경한다.
return TRUE;
}
int LNext(List *plist, LData *pdata)
{
if (plist->cur->next == NULL)
return FALSE;
plist->before = plist->cur;
plist->cur = plist->cur->next;
*pdata = plist->cur->data;
return TRUE;
}
LData LRemove(List *plist)
{
int Ddata = plist->cur->data; // 삭제 할 노드의 데이터 값을 갖는다.
Node *DNode = plist->cur; // 삭제 할 노드를 가르킨다.
plist->before->next = plist->cur->next; //삭제 시킬 노드를 cur이 가르키고 있기 때문에
// 다음 현재 위치 다음 노드를 가르킨다.
plist->cur = plist->before;
free(DNode); // 메모리 해제
(plist->numOfData)--;
return Ddata;
}
int LCount(List *plist)
{
return plist->numOfData;
}
메모리의 동적 할당을 이용한 c++코드
LinkedList.h
#ifndef __LINKED_LIST_CPP_H__
#define __LINKED_LIST_CPP_H__
#include <iostream>
typedef struct _node
{
int data;
struct _node *next;
} Node;
class LinkedListCpp
{
private:
int data;
static int numOfData;
Node *head;
Node *before;
Node *cur;
public:
void ListInit();
void LInsert(int data);
int LFirst(int *pdata);
int LNext(int *pdata);
int LRemove();
int LCount();
};
LinkedList.cpp
#include "LinkedListCpp.h"
#include <iostream>
int LinkedListCpp::numOfData = 0;
void LinkedListCpp::ListInit()
{
head = new Node;
head->next = NULL;
}
void LinkedListCpp::LInsert(int data)
{
Node *newNode = new Node;
newNode->data = data;
newNode->next = head->next;
head->next = newNode;
numOfData++;
}
int LinkedListCpp::LFirst(int *pdata)
{
if (head->next == NULL)
return false;
cur = head->next;
before = head;
*pdata = cur->data;
return true;
}
int LinkedListCpp::LNext(int *pdata)
{
if (cur->next == NULL)
return false;
before = cur;
cur = cur->next;
*pdata = cur->data;
return true;
}
int LinkedListCpp::LRemove()
{
int dData = cur->data;
Node *dNode = cur;
before->next = cur->next;
delete dNode;
cur = before;
numOfData--;
return dData;
}
int LinkedListCpp::LCount()
{
return numOfData;
}
LinkedList.h with template
#ifndef __LINKED_LIST_CPP_H__
#define __LINKED_LIST_CPP_H__
#include <iostream>
template <typename T>
class Node
{
private:
T data;
Node *next;
public:
T GetData() { return data; };
void SetData(T _data) { data = _data; };
Node* GetNextNode() { return next; };
// 다음 노드 주소를 받을 수 있는 노드 멤버 함수
void SetNextNode(Node *_node) { next = _node; };
// 다음 노드를 지정 해 줄수 있는 함수.
};
template <typename T>
class LinkedListCpp
{
private:
static int numOfData; // 노드 갯수를 갖기 위한 static변수
Node<T> *head;
Node<T> *before;
Node<T> *cur;
public:
void ListInit();
void LInsert(T data);
int LFirst(T *pdata);
int LNext(T *pdata);
T LRemove();
int LCount();
};
template <typename T>
int LinkedListCpp<T>::numOfData = 0;
template <typename T>
void LinkedListCpp<T>::ListInit()
{
head = new Node<T>;
head->SetNextNode(NULL);
}
template <typename T>
void LinkedListCpp<T>::LInsert(T data)
{
Node<T> *newNode = new Node<T>;
newNode->SetData(data);
newNode->SetNextNode(head->GetNextNode());
head->SetNextNode(newNode);
numOfData++;
}
template <typename T>
int LinkedListCpp<T>::LFirst(T *pdata)
{
if (head->GetNextNode() == NULL)
return false;
cur = head->GetNextNode();
before = head;
*pdata = cur->GetData();
return true;
}
template <typename T>
int LinkedListCpp<T>::LNext(T *pdata)
{
if (cur->GetNextNode() == NULL)
return false;
before = cur;
cur = cur->GetNextNode();
*pdata = cur->GetData();
return true;
}
template <typename T>
T LinkedListCpp<T>::LRemove()
{
int dData = cur->GetData();
Node<T> *dNode = cur;
before->GetNextNode()->SetNextNode(cur->GetNextNode());
// before->next = cur->next;
delete dNode;
cur = before;
numOfData--;
return dData;
}
template <typename T>
int LinkedListCpp<T>::LCount()
{
return numOfData;
}
#endif
.h에 정의 한 이유는 template을 사욜 했을 경우 cpp을 include 해줘야 하는데, 디렉토리를 나누지 않고 사용하기위해 이런식으로 구성했다. 전체적인 방향성은 먼저 작성해 보았던 c를 기반으로 작성 했다. 나만의 방식으로 작성을 했는데, c++의 지식이 많지 않다보니 코드에 자신이 없다.
static변수 .h에서 선언 했을 때의 error
Undefined symbols for architecture arm64: "LinkedListCpp::numOfData", referenced from: LinkedListCpp::LInsert(int) in LinkedList-3988b9.o ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
해결 방법