#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 5
typedef struct tnode_ {//! Double Linked list typedef Struct
int item;
tnode_ *next;
tnode_ *prev;
}tnode;
tnode_ *head,*tail;//! 대가리와 꼬리
void node_display() {
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 node_find() {
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 node_del_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;
tmp_c->next->prev=tmp_c->prev;
free(tmp_c);
printf("\n아이템 %d 삭제되었습니다",find);
}//! end if
tmp_b=tmp_b->next;
tmp_c=tmp_b->next;
}//! end while
}
void node_sort() {//! 오름차순 sorting
tnode *tmp_b,*tmp_c;//! before node , current node
int tmp;//! temp item
for(tmp_b=head;tmp_b!=tail;tmp_b=tmp_b->next) {
for(tmp_c=tmp_b->next;tmp_c!=tail;tmp_c=tmp_c->next) {
if(tmp_b->item > tmp_c->item) {//! Bubble sort
tmp=tmp_b->item;
tmp_b->item=tmp_c->item;
tmp_c->item=tmp;
}
}
}
}
void node_insert_rear() {//! MAX=10 개의 RANDOM NUMBER INSERT rear
tnode_ *tmp,*tmp_e;
tmp_e=head;//! search 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;
tail->prev=tmp;
tmp->next=tail;
tmp->prev=tmp_e;
tmp_e->next=tmp;//! head=tmp_e;
}//! end for
printf("\n입력 완료");
}
void node_init() {//! 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->prev=head;
head->next=tail;
tail->prev=head;
tail->next=tail;//! 무한루프 방지
}
int main() {
int cmd;
node_init();
while(1) {
printf("\n============================================================");
printf("\n 1)뒤에삽입 랜덤 10개씩 2)Sorting node ");
printf("\n 3)삭제 4)검색 5)출력 Other)종료 ");
printf("\n============================================================");
printf("\nCommand> ");
scanf("%d",&cmd);
switch(cmd) {
case 1 :
node_insert_rear();
break;
case 2 :
node_sort();
break;
case 3 :
node_del_all();
break;
case 4 :
node_find();
break;
case 5 :
node_display();
break;
default :
exit(0);
}
}
return 0;
}
'Language > C & C Plus' 카테고리의 다른 글
[C] volatile int (0) | 2012.07.13 |
---|---|
[C] 주소연산자(&)와 포인터연산자(*) (0) | 2012.07.13 |
[C] Function key 입력 받기 (0) | 2012.07.13 |
[C] Singly Linked list(전역변수) (0) | 2012.07.13 |
[C] strcpy(), strcat(), strlen(), atoi() 함수 구현하기(플로그 캡쳐) (0) | 2012.07.13 |