2012. 7. 13. 14:47

#include <stdio.h>
#include <stdlib.h>//! exit
#include <time.h>//! time

#define MAX 10

typedef struct tnode_ {
        int item;
        tnode_ *next;
}tnode;

tnode_ *head,*tail;//! 대가리와 꼬리

void display_node() {
        int num=1;//! node number counter
        tnode_ *tmp;
        tmp=head->next;
        printf("\n===================================");
        while(tmp!=tail) {                
                printf("\n %d아이템은  %d 입니다",num,tmp->item);
                tmp=tmp->next;
                num++;
        }//! end while
        printf("\n===================================");
}

void find_node() {
        int find,num=1,cnt=0;//! find item ,node number,find node counter
        tnode_ *tmp;
        printf("\n찾을 아이템을 입력하시오 : ");
        scanf("%d",&find);
        tmp=head->next;//! first node
        while(tmp!=tail) {//! node 끝까지 찾음
                if(tmp->item==find) {
                        printf("\n 아이템 %d는 %d번째 있다",find,num);
                        cnt++;
                }//! end if
                tmp=tmp->next;
                num++;
        }//! end while
        if(cnt==0) printf("\n찾는 노드가 없다");
}

void del_node_all() {
        int find;//! find item
        tnode_ *tmp_b,*tmp_c;//! before 전노드 current 현재노드
        printf("\n삭제할 아이템을 입력하시오(같은 아이템 모두 삭제) : ");
        scanf("%d",&find);
        tmp_b=head;
        tmp_c=head->next;
        while(tmp_b!=tail) {//! node 끝까지 찾음 마지막 노드 삭제고려
                if(tmp_c->item==find) {
                        tmp_b->next=tmp_c->next;
                        free(tmp_c);
                        printf("\n아이템 %d 삭제되었습니다",find);
                }//! end if
                tmp_b=tmp_b->next;
                tmp_c=tmp_b->next;
        }//! end while
}

void insert_node_rear() {//! MAX=10 개의 RANDOM NUMBER INSERT rear
        tnode_ *tmp,*tmp_e;
        tmp_e=head;//! end node
        srand((unsigned)time(NULL));
        for(int i=0;i<MAX;i++) {
                if((tmp=(tnode_*)malloc(sizeof(tnode_)))==NULL) {
                        fprintf(stderr,"Memory is Full");
                }
                while(tmp_e->next!=tail) {//! search end node
                        tmp_e=tmp_e->next;
                }//! end while
                tmp->item=rand()%11;
                tmp->next=tail;
                tmp_e->next=tmp;
                //head=tmp_e;
        }//! end for
        printf("\n입력 완료");
}


void insert_node_front() {//! MAX=10 개의 RANDOM NUMBER INSERT front
        tnode_ *tmp;
        srand((unsigned)time(NULL));
        for(int i=0;i<MAX;i++) {
                if((tmp=(tnode_*)malloc(sizeof(tnode_)))==NULL) {
                fprintf(stderr,"Memory is Full");
                }
                tmp->item=rand()%11;
                tmp->next=head->next;
                head->next=tmp;
        }//! end for
        printf("\n입력 완료");
}
        
void init_node() {//! head,tail initialize
        if((head=(tnode_*)malloc(sizeof(tnode_)))==NULL) {
                fprintf(stderr,"Memory is Full");
                exit(1);
        }
        if((tail=(tnode_*)malloc(sizeof(tnode_)))==NULL) {
                fprintf(stderr,"Memory is Full");
                exit(1);
        }
        head->next=tail;
        tail->next=tail;//! 무한루프 방지
}

int main() {
        int cmd;
        init_node();
        while(1) {
                printf("\n============================================================");
                printf("\n    1)앞에삽입(랜덤 10개씩)      2)뒤에삽입 랜덤 10개씩     ");
                printf("\n    3)삭제          4)검색       5)출력        Other)종료   ");
                printf("\n============================================================");
                printf("\nCommand> ");
                scanf("%d",&cmd);
                switch(cmd) {
                case 1 :
                        insert_node_front();
                        break;
                case 2 :
                        insert_node_rear();
                        break;
                case 3 :
                        del_node_all();
                        break;
                case 4 :
                        find_node();
                        break;
                case 5 :
                        display_node();
                        break;
                default :
                        exit(0);
                }
        }
        return 0;
}

Posted by 몰라욧