#include <stdio.h>
#include <stdlib.h>
typedef struct _node{
int data;
struct _node * next;
}Node;
Node * head = NULL;
Node * tail = NULL;
Node * cur = NULL;
Node * newN = NULL;
int newD;
void printNode(Node * newN){
if(head == NULL){
printf("입력된 데이터가 없음\n");
return;
}
else{
cur = head;
printf("Linked List : %d -> ", cur->data);
while(cur->next!=NULL){
cur = cur->next;
printf("%d -> ", cur->data);
}
}
printf("\n\n");
return;
}
void deleteNode(Node * newN){
if(head == NULL){
printf("입력된 데이터 없음\n");
return;
}
else{
int deD;
Node * deN = NULL;
printf("삭제할 값 입력: ");
scanf("%d", &deD);
cur = head;
if(cur->data == deD){
deN = cur;
cur = cur->next;
head = cur;
free(deN);
printNode(newN);
return;
}
while(cur->next->data != deD){
cur = cur->next;
if(cur->next == NULL){
printf("일치하는 값이 없습니다.\n");
return 0;
}
}
deN = cur->next;
printf("\n%d 삭제됨 \n", cur->next->data);
cur->next = cur->next->next;
free(deN);
printNode(newN);
return;
}
}
void searchNode(Node * newN){
if(head == NULL){
printf("입력된 값 없음\n");
return;
}
else{
int seD;
int k = 1;
printf("검색할 값 입력: ");
scanf("%d", &seD);
cur = head;
while(cur->data != seD){
cur = cur->next;
k++;
if(cur == NULL){
printf("일치하는 데이터가 없습니다.\n");
return 0;
}
}
printf("\n데이터(%d)는 %d 번째 노드에 있습니다. \n", seD, k);
return;
}
}
void insertNode(Node * newN){
int inD, inno;
int k = 0;
Node * inN = NULL;
cur = head;
printf("입력할 값(1보다 작은 값 입력 시 종료): ");
scanf("%d", &inD);
if(head == NULL){
newN->data = inD;
newN->next = NULL;
printNode(newN);
return;
}
printf("\n입력할 값의 위치: ");
scanf("%d", &inno);
cur = head;
if(inD < 1){
printf("1보다 작은 값을 입력할 수 없음. 입력 종료\n\n");
printNode(newN);
return;
}
inN->data = inD;
if(inno == 1){
inN->next = cur;
printNode(newN);
return;
}
for(int i = 1; i < inno; i++){
cur = cur->next;
}
inN->next = cur->next;
cur->next = inN;
free(cur);
printNode(newN);
return;
}
int main(){
printf("\n초기 노드 값 입력 (값이 1보다 작으면 작성 종료) : \n");
while(1){
scanf("%d", &newD);
if(newD < 1){
break;
}
newN = (Node*)malloc(sizeof(Node));
newN->data = newD;
newN->next = NULL;
if(head==NULL){
head = newN;
}
else{
tail->next = newN;
}
tail = newN;
}
printNode(newN);
while(1){
int tyno;
printf("\n\ntype 1 : View All Data\ntype 2 : Search Data Location\ntype 3 : Insert Data\ntype 4 : Delete Data\ntype 0 : Stop\n\nWrite down type # :");
scanf("%d", &tyno);
printf("\n\n");
if(tyno == 0){
break;
}
if(tyno == 1){
printNode(newN);
}
if(tyno == 2){
searchNode(newN);
}
if(tyno == 3){
insertNode(newN);
}
if(tyno == 4){
deleteNode(newN);
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct _node {
int data;
struct _node *next;
struct _node *prev; // 🔹 이전 노드 포인터 추가
} Node;
Node *head = NULL;
Node *tail = NULL;
Node *cur = NULL;
// 전체 노드 출력 (앞 방향)
void printNodeForward() {
if (head == NULL) {
printf("입력된 데이터가 없음\n");
return;
}
cur = head;
printf("Forward: ");
while (cur != NULL) {
printf("%d", cur->data);
if (cur->next != NULL) printf(" -> ");
cur = cur->next;
}
printf("\n");
}
// 전체 노드 출력 (뒤 방향)
void printNodeBackward() {
if (tail == NULL) {
printf("입력된 데이터가 없음\n");
return;
}
cur = tail;
printf("Backward: ");
while (cur != NULL) {
printf("%d", cur->data);
if (cur->prev != NULL) printf(" <- ");
cur = cur->prev;
}
printf("\n\n");
}
// 검색
void searchNode() {
if (head == NULL) {
printf("입력된 값 없음\n");
return;
}
int seD, pos = 1;
printf("검색할 값 입력: ");
scanf("%d", &seD);
cur = head;
while (cur != NULL) {
if (cur->data == seD) {
printf("\n데이터(%d)는 %d번째 노드에 있습니다.\n", seD, pos);
return;
}
cur = cur->next;
pos++;
}
printf("일치하는 데이터가 없습니다.\n");
}
// 삽입
void insertNode() {
int inD, inPos;
printf("입력할 값(1보다 작은 값 입력 시 종료): ");
scanf("%d", &inD);
if (inD < 1) {
printf("1보다 작은 값은 입력할 수 없음. 입력 종료\n");
return;
}
Node *inN = (Node *)malloc(sizeof(Node));
inN->data = inD;
inN->next = NULL;
inN->prev = NULL;
if (head == NULL) { // 빈 리스트
head = tail = inN;
printNodeForward();
printNodeBackward();
return;
}
printf("\n입력할 위치 (1 = 맨 앞): ");
scanf("%d", &inPos);
if (inPos <= 1) { // 맨 앞 삽입
inN->next = head;
head->prev = inN;
head = inN;
printNodeForward();
printNodeBackward();
return;
}
cur = head;
for (int i = 1; i < inPos - 1 && cur->next != NULL; i++) {
cur = cur->next;
}
inN->next = cur->next;
inN->prev = cur;
if (cur->next != NULL) {
cur->next->prev = inN;
} else {
tail = inN; // 맨 뒤 삽입
}
cur->next = inN;
printNodeForward();
printNodeBackward();
}
// 삭제
void deleteNode() {
if (head == NULL) {
printf("입력된 데이터 없음\n");
return;
}
int deD;
printf("삭제할 값 입력: ");
scanf("%d", &deD);
cur = head;
while (cur != NULL && cur->data != deD) {
cur = cur->next;
}
if (cur == NULL) {
printf("일치하는 값이 없습니다.\n");
return;
}
if (cur == head) {
head = head->next;
if (head != NULL) head->prev = NULL;
if (cur == tail) tail = NULL; // 하나만 있었을 경우
} else if (cur == tail) {
tail = tail->prev;
tail->next = NULL;
} else {
cur->prev->next = cur->next;
cur->next->prev = cur->prev;
}
printf("\n%d 삭제됨\n", cur->data);
free(cur);
printNodeForward();
printNodeBackward();
}
int main() {
int newD;
printf("\n초기 노드 값 입력 (값이 1보다 작으면 작성 종료): \n");
while (1) {
scanf("%d", &newD);
if (newD < 1) break;
Node *newN = (Node *)malloc(sizeof(Node));
newN->data = newD;
newN->next = NULL;
newN->prev = tail;
if (head == NULL) {
head = newN;
} else {
tail->next = newN;
}
tail = newN;
}
printNodeForward();
printNodeBackward();
while (1) {
int tyno;
printf("\n\ntype 1 : View All Data (Forward & Backward)"
"\ntype 2 : Search Data Location"
"\ntype 3 : Insert Data"
"\ntype 4 : Delete Data"
"\ntype 0 : Stop\n\nWrite down type #: ");
scanf("%d", &tyno);
printf("\n");
if (tyno == 0) break;
if (tyno == 1) { printNodeForward(); printNodeBackward(); }
else if (tyno == 2) searchNode();
else if (tyno == 3) insertNode();
else if (tyno == 4) deleteNode();
else printf("잘못된 입력입니다.\n");
}
// 프로그램 종료 전 메모리 해제
cur = head;
while (cur != NULL) {
Node *tmp = cur;
cur = cur->next;
free(tmp);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct _node {
int data;
struct _node *next;
} Node;
Node *head = NULL;
Node *tail = NULL;
Node *cur = NULL;
// 전체 노드 출력
void printNode() {
if (head == NULL) {
printf("입력된 데이터가 없음\n");
return;
}
cur = head;
printf("Linked List: ");
while (cur != NULL) {
printf("%d", cur->data);
if (cur->next != NULL) printf(" -> ");
cur = cur->next;
}
printf("\n\n");
}
// 노드 검색
void searchNode() {
if (head == NULL) {
printf("입력된 값 없음\n");
return;
}
int seD, pos = 1;
printf("검색할 값 입력: ");
scanf("%d", &seD);
cur = head;
while (cur != NULL) {
if (cur->data == seD) {
printf("\n데이터(%d)는 %d번째 노드에 있습니다.\n", seD, pos);
return;
}
cur = cur->next;
pos++;
}
printf("일치하는 데이터가 없습니다.\n");
}
// 노드 삽입
void insertNode() {
int inD, inPos;
printf("입력할 값(1보다 작은 값 입력 시 종료): ");
scanf("%d", &inD);
if (inD < 1) {
printf("1보다 작은 값은 입력할 수 없음. 입력 종료\n");
return;
}
Node *inN = (Node *)malloc(sizeof(Node));
inN->data = inD;
inN->next = NULL;
if (head == NULL) { // 빈 리스트에 첫 삽입
head = tail = inN;
printNode();
return;
}
printf("\n입력할 위치 (1 = 맨 앞): ");
scanf("%d", &inPos);
if (inPos <= 1) { // 맨 앞 삽입
inN->next = head;
head = inN;
printNode();
return;
}
cur = head;
for (int i = 1; i < inPos - 1 && cur->next != NULL; i++) {
cur = cur->next;
}
inN->next = cur->next;
cur->next = inN;
if (inN->next == NULL) { // 맨 뒤 삽입이면 tail 갱신
tail = inN;
}
printNode();
}
// 노드 삭제
void deleteNode() {
if (head == NULL) {
printf("입력된 데이터 없음\n");
return;
}
int deD;
printf("삭제할 값 입력: ");
scanf("%d", &deD);
Node *prev = NULL;
cur = head;
while (cur != NULL && cur->data != deD) {
prev = cur;
cur = cur->next;
}
if (cur == NULL) {
printf("일치하는 값이 없습니다.\n");
return;
}
if (cur == head) { // 첫 번째 노드 삭제
head = head->next;
if (cur == tail) { // 노드가 하나만 있었던 경우
tail = NULL;
}
} else {
prev->next = cur->next;
if (cur == tail) { // 마지막 노드 삭제
tail = prev;
}
}
printf("\n%d 삭제됨\n", cur->data);
free(cur);
printNode();
}
int main() {
int newD;
printf("\n초기 노드 값 입력 (값이 1보다 작으면 작성 종료): \n");
while (1) {
scanf("%d", &newD);
if (newD < 1) break;
Node *newN = (Node *)malloc(sizeof(Node));
newN->data = newD;
newN->next = NULL;
if (head == NULL) {
head = newN;
} else {
tail->next = newN;
}
tail = newN;
}
printNode();
while (1) {
int tyno;
printf("\n\ntype 1 : View All Data"
"\ntype 2 : Search Data Location"
"\ntype 3 : Insert Data"
"\ntype 4 : Delete Data"
"\ntype 0 : Stop\n\nWrite down type #: ");
scanf("%d", &tyno);
printf("\n");
if (tyno == 0) break;
if (tyno == 1) printNode();
else if (tyno == 2) searchNode();
else if (tyno == 3) insertNode();
else if (tyno == 4) deleteNode();
else printf("잘못된 입력입니다.\n");
}
// 프로그램 종료 전 메모리 해제
cur = head;
while (cur != NULL) {
Node *tmp = cur;
cur = cur->next;
free(tmp);
}
return 0;
}