8.19.2017

LinkedList 자료구조

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

typedef struct Test {
        int data;
        struct Test* p; //주소를 담을 변수 생성
}test;

void main(){
        test* head = (test*)malloc(sizeof(test)); //시작위치

        test t1;
        test t2;
        test t3;

        printf("%d\n", &t1);
        printf("%d\n", &t2);
        printf("%d\n\n", &t3);


        t1.data = 10;
        t2.data = 20; //(*t1.p).data = 20;
        t3.data = 30;

        t1.p = &t2;
        t2.p = &t3;
        t3.p = NULL;

        printf("%d\n", t1.p);
        printf("%d\n", t2.p);
        printf("%d\n", t3.p);

}

============================================================================

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

typedef struct TEST {
        int data;
        struct TEST* next;
}test;

void main() {
        //연결리스트 : 주소를 연결해 놓은 집합체
        //                         마지막 노드의 next는 NULL
        //                         head는 시작위치를 알려줄 수 있게끔
        //                         데이터 X

        test* head = (test*)malloc(sizeof(test));
        head->next = NULL;

        test* new1 = (test*)malloc(sizeof(test));
        (*head).next = new1;
        new1->next = NULL;

        test* new2 = (test*)malloc(sizeof(test));
        new1->next = new2;
        new2->next = NULL;

        test* new3 = (test*)malloc(sizeof(test));
        new2->next = new3;
        new3->next = NULL;

        new1->data = 10;
        new2->data = 20;
        new3->data = 30;

        test* move = head->next;
        while (move != NULL) {
                printf("%d", move->data);
                move = move->next;
        }printf("\n"); // 출력
}

=============================================================================

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

typedef struct Test {
        int data;
        struct Test* next;
}test;

test* head;

void input(int data) {
        test* newnode = (test*)malloc(sizeof(test));
        newnode->data = data;
        newnode->next = head->next;
        head->next = newnode;
}

void del_node1() {
   test* move = head->next;

   printf("삭제할 데이터 입력 : "); int data;
   scanf("%d", &data);

   while (move != NULL) {
      if (move->next->data == data) {
         test* temp = move->next;
         move->next = move->next->next;
         free(temp);
         break;
      }
      move = move->next;
   }
}

void del_node2() {
        test* move = head->next; //삭제할 노드를 찾을 변수
        test* move2 = head; //삭제할 노드의 이전 노드

        printf("삭제할 데이터 입력 : "); int data;
        scanf("%d", &data);

        while (move != NULL) {
                if (move->data == data) {
                        move2->next = move->next;
                        free(move);
                        break;
                }
                move = move->next;
                move2 = move2->next;
        }
}

void main() {
        head = (test*)malloc(sizeof(test));
        head->next = NULL;

        input(10);
        input(20);
        input(30);
        input(40);
        input(50);
        del_node2();

        test* move = head->next;
        while (move != NULL) {
                printf("%d", move->data);
                move = move->next;
        }printf("\n"); // 출력
}

댓글 없음:

댓글 쓰기