#include <stdio.h> #include <stdlib.h> struct NODE{ struct NODE *next; int data; }; int main(void) { int num=0, n, a=0, c; struct NODE *head; struct NODE *search_point; //struct NODE *new_point; head=(NODE*)malloc(sizeof(struct NODE)); while(num!=4) { printf("어떤 작업을 원하십니까?\n"); printf("1.추가 2.삭제 3.출력 4.종료\n"); scanf("%d", &num); switch(num) { case 1: printf("넣을 데이터를 입력해주세요. \n"); scanf("%d", &n); search_point=head; while(search_point->next != NULL && a!=0){ search_point=search_point->next; } if(a!=0) { search_point->next=(NODE*)malloc(sizeof(struct NODE)); search_point=search_point->next; } search_point->data=n; search_point->next=NULL; a=1; break; case 2: printf("몇 번째 데이터를 삭제하시겠습니까?\n"); scanf("%d", &n); search_point=head; if(n==1) { search_point->data=(search_point->next)->data; search_point->next=(search_point->next)->next; } else { c=1; while(c<(n-1)) { search_point=search_point->next; c++; } search_point->next=(search_point->next)->next; } break; case 3: search_point=head; while(search_point->next != NULL){ printf("in : %d ", search_point->data); search_point=search_point->next; } printf("in : %d \n", search_point->data); break; case 4: printf("\n종료되었습니다.\n"); break; } printf("\n\n"); } return 0; }
2018.03.03 15:37
링크드 리스트 예제
조회 수 295 추천 수 0 댓글 0