CS Study/C

10. 포인터 - 자료구조

Ryannn 2022. 4. 6. 20:40

1. Stack에 정수 저장하기

#include <stdio.h>
#include <stdlib.h>
struct stackNode {
	int data;
	struct stackNode *nextPtr;
};
typedef struct stackNode StackNode;
typedef StackNode* StackNodePtr;
void push(StackNodePtr *topPtr, int info);
int pop(StackNodePtr *topPtr);
int isEmpty(StackNodePtr topPtr);
void printStack(StackNodePtr currentPtr);
void instructions();
void instructions() {
	printf("Enter choice : \n");
	printf("\t 1 to push a value on the stack. \n");
	printf("\t 2 to pop a value off the stack. \n");
	printf("\t 3 to end program. \n");
}
int isEmpty(StackNodePtr topPtr) {
	return topPtr == NULL;
}
void printStack(StackNodePtr currentPtr) {
	if (currentPtr == NULL) {
		printf("Stack is empty. \n\n");
	}
	else {
		printf("The stack is : \n");

		while (currentPtr != NULL) {
			printf("%d -->", currentPtr->data);
			currentPtr = currentPtr->nextPtr;
		}
		printf("NULL \n\n");
	}
}
void push(StackNodePtr *topPtr, int info) {
	StackNodePtr newPtr;

	newPtr = malloc(sizeof(StackNode));
	if (newPtr != NULL) {
		newPtr->data = info;
		newPtr->nextPtr = *topPtr;
		*topPtr = newPtr;
	}
	else {
		printf("%d not inserted. No memory available.\n");
	}
}
int pop(StackNodePtr *topPtr) {
	StackNodePtr tempPtr;
	int popValue;
	
	tempPtr = *topPtr;
	popValue = (*topPtr)->data;
	*topPtr = (*topPtr)->nextPtr;
	free(tempPtr);
	return popValue;
}
void main() {
	StackNodePtr stackPtr = NULL;
	int choice;
	int value;
	
	instructions();
	printf("? ");
	scanf("%d", &choice);

	while (choice != 3) {
		switch (choice) {
		case 1 :
			while (!getchar());
			printf("Enter an integer : ");
			scanf("%d", &value);
			push(&stackPtr, value);
			printStack(stackPtr);
			break;

		case 2:
			while (!getchar());
			if (!isEmpty(stackPtr)) {
				printf("The popped value is %d. \n", pop(&stackPtr));
				printStack(stackPtr);
			}
			break;
			
		default :
			while (!getchar());
			printf("Invalid choice \n\n");
			instructions();
			break;
		}
		printf("? ");
		scanf("%d", &choice);
		
	}
	printf("End of run.\n");
	return 0;
}

2. Queue에 정수 넣기

#include <stdio.h>
#include <stdlib.h>

struct queueNode {
	char data;
	struct queueNode *nextPtr;
};
typedef struct queueNode QueueNode;
typedef QueueNode *QueueNodePtr;

void printQueue(QueueNodePtr currentPtr);
int isEmpty(QueueNodePtr headPtr);
char dequeue(QueueNodePtr *headPtr, QueueNodePtr *tailPtr);
void enqueue(QueueNodePtr *headPtr, QueueNodePtr *tailPtr, char value);
void instructions();
void instructions() {
	printf("Enter your choice : \n");
	printf("\t1 to add an item to the queue \n");
	printf("\t2 to remove an item from the queue \n");
	printf("\t3 to end\n");
}
int isEmpty(QueueNodePtr headPtr) {
	return headPtr == NULL;
}
void printQueue(QueueNodePtr currentPtr) {
	if (isEmpty(currentPtr)) {
		printf("Queue is empty\n\n");
	}
	else {
		printf("The queue is : \n");
		while (currentPtr != NULL) {
			printf("%c --> ", currentPtr->data);
			currentPtr = currentPtr->nextPtr;
		}
		printf("NULL \n\n");
	}
}

char dequeue(QueueNodePtr *headPtr, QueueNodePtr *tailPtr) {
	char value;
	QueueNodePtr tempPtr;

	value = (*headPtr)->data;
	tempPtr = *headPtr;
	*headPtr = (*headPtr)->nextPtr;

	if (*headPtr == NULL) {
		*tailPtr = NULL;
	}
	free(tempPtr);
	return value;
}

void enqueue(QueueNodePtr *headPtr, QueueNodePtr *tailPtr, char value) {
	QueueNodePtr newPtr;
	newPtr = malloc(sizeof(QueueNode));

	if (newPtr != NULL) {
		newPtr->data = value;
		newPtr->nextPtr = NULL;

		if (isEmpty(*headPtr)) {
			*headPtr = newPtr;
		}
		else {
			(*tailPtr)->nextPtr = newPtr;
		}
		*tailPtr = newPtr;
	}
	else {
		printf("%c not inserted. No memory available.\n", value);
	}
}

int main(void) {
	QueueNodePtr headPtr = NULL;
	QueueNodePtr tailPtr = NULL;
	int choice;
	char item;

	instructions();
	printf("? ");
	scanf("%d", &choice);

	while (choice != 3) {
		switch (choice) {
		case 1:
			while (!getchar());
			printf("Enter a character : ");
			item = getchar();
			enqueue(&headPtr, &tailPtr, item);
			printQueue(headPtr);
			break;
		case 2:
			while (!getchar());
			if (!isEmpty(headPtr)) {
				item = dequeue(&headPtr, &tailPtr);
				printf("%c has been dequeued \n", item);
			}
			printQueue(headPtr);
			break;

		default:
			while(!getchar());
			printf("Invalid choice. \n\n");
			instructions();
			break;
		}
		printf("? ");
		scanf("%d", &choice);
	}
	printf("End of run. \n");
	return 0;
}

3. Tree에 정수 넣기

#include <stdio.h>
#include <stdlib.h>

struct treeNode {
	struct treeNode *leftPtr;
	int data;
	struct treeNode *rightPtr;
};
typedef struct treeNode TreeNode;
typedef TreeNode *TreeNodePtr;

void insertNode(TreeNodePtr *treePtr, int value);
void inOrder(TreeNodePtr treePtr);
void preOrder(TreeNodePtr treePtr);
void postOrder(TreeNodePtr treePtr);

int main(void) {
	int i, item;
	TreeNodePtr rootPtr = NULL;

	srand(time(NULL));
	printf("The Numbers being plance in the tree are : \n");
	for (i = 1; i <= 10; i++) {
		item = rand() % 15;
		printf("%3d", item);
		insertNode(&rootPtr, item);
	}
	printf("\n\nThe preorder traversal is : \n");
	preOrder(rootPtr);

	printf("\n\nThe inorder traversal is : \n");
	inOrder(rootPtr);

	printf("\n\nThe postorder traversal is : \n");
	postOrder(rootPtr);
	return 0;
}

void insertNode(TreeNodePtr *treePtr, int value) {
	if (*treePtr == NULL) {
		*treePtr = malloc(sizeof(TreeNode));
		
		if (*treePtr != NULL) {
			(*treePtr)->data = value;
			(*treePtr)->leftPtr = NULL;
			(*treePtr)->rightPtr = NULL;
		}
		else {
			printf("%d not inserted. No memory available.\n", value);
		}
	}
	else {
		if (value < (*treePtr)->data) {
			insertNode(&((*treePtr)->leftPtr), value);
		}
		else if (value > (*treePtr)->data) {
			insertNode(&((*treePtr)->rightPtr), value);
		}
		else {
			printf("dup");
		}
	}
}

void inOrder(TreeNodePtr treePtr) {
	if (treePtr != NULL) {
		inOrder(treePtr->leftPtr);
		printf("%3d", treePtr->data);
		inOrder(treePtr->rightPtr);
	}
}

void preOrder(TreeNodePtr treePtr) {
	if (treePtr != NULL) {
		printf("%3d", treePtr->data);
		preOrder(treePtr->leftPtr);
		preOrder(treePtr->rightPtr);
	}
}

void postOrder(TreeNodePtr treePtr) {
	if (treePtr != NULL) {
		postOrder(treePtr->leftPtr);
		postOrder(treePtr->rightPtr);
		printf("%3d", treePtr->data);
	}
}

4. LinkedList

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <Windows.h>


typedef struct DetectiveConanMovie {
	int edition;
	char title[10];
	int release_year;
	struct DetectiveConanMovie *link;
}conan_m;

conan_m *FirstNode = NULL;

typedef struct {
	conan_m* head;
} linkedList_h;

linkedList_h* createLinkedList_h(void) {
	linkedList_h* L;
	L = (linkedList_h*)malloc(sizeof(linkedList_h));
	L->head = NULL;
	return L;
}


void freeLinkedList_h(linkedList_h* L) {
	conan_m* p;
	while (L->head != NULL) {
		p = L->head;
		L->head = L->head->link;
		free(p);
		p = NULL;
	}
}


void printList(linkedList_h* L) {
	conan_m* p;
	p = L->head;
	printf("---------------------\n");
	while (p != NULL) {
		printf("��� : %d\n", p->edition);
		printf("���� : %s\n", p->title);
		printf("���� �⵵ : %d\n", p->release_year);
		p = p->link;
	}
	if (p = NULL) {
		printf("�������� �ʽ��ϴ�.\n");
		return;
	}
	printf("---------------------\n");
}


void insertFirstNode(linkedList_h *L, conan_m *src) {
	conan_m* newNode;
	newNode = (conan_m*)malloc(sizeof(conan_m));
	strcpy(newNode->title, src->title);           
	newNode->edition = src->edition;
	newNode->release_year = src->release_year;

	newNode->link = L->head; 
	L->head = newNode;
}

void insertMiddleNode(linkedList_h* L, conan_m *pre, conan_m *src) {
	conan_m* newNode;
	newNode = (conan_m*)malloc(sizeof(conan_m));
	strcpy(newNode->title, src->title);           
	newNode->edition = src->edition;
	newNode->release_year = src->release_year;

	if (L == NULL) {
		newNode->link = NULL;
		L->head = newNode;
	}
	else if (pre == NULL) {
		L->head = newNode;
	}
	else {
		newNode->link = pre->link;
		pre->link = newNode;
	}
}


void insertLastNode(linkedList_h* L, conan_m *src) {
	conan_m* newNode, *temp;
	newNode = (conan_m*)malloc(sizeof(conan_m));
	strcpy(newNode->title, src->title);           
	newNode->edition = src->edition;
	newNode->release_year = src->release_year;

	newNode->link = NULL;
	if (L->head == NULL) {
		L->head = newNode;
		return;
	}
	
	temp = L->head;
	while (temp->link != NULL) temp = temp->link;
	temp->link = newNode; 
}


void deleteNode(linkedList_h* L, conan_m* p) {
	conan_m* pre;
	if (L->head == NULL) return;
	if (L->head->link == NULL) {  
		free(L->head);
		L->head = NULL;
		return;
	}
	else if (p == NULL)
		return;
	else if (L->head == p) { 
		L->head = p->link;
		free(p);
	}
	else {
		pre = L->head;
		while (pre->link != p) {
			pre = pre->link;
		}
		pre->link = p->link;
		free(p);
	}
}


void loadFile() {
	FILE *load;
	load = fopen("List.txt", "rt");
	if (load != NULL) {
		conan_m *aNode = NULL;
		//int loadfp;
		while (feof(load) == 0) {
			aNode = (conan_m *)malloc(sizeof(conan_m));
			aNode->link = NULL;
			scanf((char *)load, "%s %s %s\n", &aNode->edition, &aNode->title, &aNode->release_year);
			if (FirstNode == NULL) {
				FirstNode = aNode;
			}
			else {
				conan_m *temp = FirstNode;
				while (temp->link != NULL) {
					temp = temp->link;
				}
				temp->link = aNode;
			}
		}
	}
}


void saveFile() {
	FILE *save = fopen("List.txt", "wt");
	if (FirstNode != NULL) {
		while (FirstNode->link != NULL) {
			conan_m *al = FirstNode;
			printf((char *)save, "%s %s %s\n", al->edition, al->title, al->release_year);
			FirstNode = FirstNode->link;
		}
		printf((char *)save, "%s %s %s", FirstNode->edition, FirstNode->title, FirstNode->release_year);
		free(FirstNode);
	}
}



conan_m* searchNode(linkedList_h* L, int edition) {
	conan_m *temp;
	temp = L->head;
	while (temp != NULL) {
		if (temp->edition == edition) return temp;
		else temp = temp->link;
	}
	return temp;
}

int searchNodeIdx(linkedList_h* L, int edition) {
	conan_m *temp;
	int idx = 0;
	temp = L->head;
	while (temp != NULL) {
		idx++;
		if (temp->edition == edition) return idx;
		else temp = temp->link;
	}
	return -1;
}




//main �Լ�
int main() {
	FILE *load, *save;
	linkedList_h* L;
	conan_m *p;
	int choice;
	L = createLinkedList_h();// ���� ����Ʈ ����
	loadFile();
	while (1) {
		printf("\n���� �� � Operation�� �����ϰڽ��ϱ�? \n");
		printf("1. ������ �߰�    2. ������ ����     3. ������ �˻�       4. ���� ����Ʈ Ȯ��       5. ���α׷� ���� \n");
		scanf("%d", &choice);
		if (choice == 1) {
			int choice2 = 0;
			p = (conan_m*)malloc(sizeof(conan_m));// ������ �� ��� �Ҵ�
			printf("*��Ž�� �ڳ� �������� �߰����ּ���!*\n������ ���(edition), ������ �̸�, ������ ���� �⵵ ������ �Է��ϼ���.(�� ������ ��ĭ���� �����մϴ�.) \n");
			scanf("%d %s %d", &p->edition, p->title, &p->release_year);
			printf("\n�������� ��� �߰� �Ͻðڽ��ϱ�? \n");
			printf("1. �Ǿ�    2. �ǵ�    3. Ư�� ������ �ڿ� \n");
			scanf("%d", &choice2);
			if (choice2 == 1) {
				insertFirstNode(L, p);
			}
			else if (choice2 == 2) {
				insertLastNode(L, p);
			}
			else if (choice2 == 3) {
				int edition = 0;
				conan_m *pre;
				printf("���� ������ ��� ����Ʈ���� ã�Ƽ� �Է��ϼ���. \n");
				printList(L);
				scanf("%d", &edition);
				pre = searchNode(L, edition);
				if (pre != NULL) {
					insertMiddleNode(L, pre, p);
				}
				else {
					insertFirstNode(L, p);
				}
			}
		}
		else if (choice == 2) {
			int edition = 0;
			printf("���� ������ ����Ʈ���� ���� ������ ���(edition)�� �Է��ϼ���. \n");
			printList(L);
			scanf("%d", &edition);
			p = searchNode(L, edition);
			if (p != NULL) {
				deleteNode(L, p);
			}
			else {
				printf("��û�Ͻ� �������� ã�� �� �����ϴ�. \n");
			}
		}
		else if (choice == 3) {
			int edition = 0;
			printf("������ ����Ʈ���� �˻��ϰ� ���� ������ ���(edition)��  �Է��ϼ���. \n");
			scanf("%d", &edition);
			int idx = searchNodeIdx(L, edition);
			if (idx == -1) {
				printf("��û�Ͻ� �������� ã�� �� �����ϴ�. \n");
			}
			else {
				printf("��û�Ͻ� �������� ����Ʈ�� %d��°�� �ֽ��ϴ�.\n", idx);
			}
		}
		else if (choice == 4) {
			printList(L);
		}
		else if (choice == 5) {
			saveFile();
			break;
		}
	}
	getchar();
	return 0;
}