중첩 구조체의 연결리스트
글쓴이: ddong90 / 작성시간: 수, 2010/01/27 - 7:04오후
중첩된 구조체에 연결리스트를 사용해보고싶어서 채팅을 참조하여 어떻게 되는건지 해보려고 하는데요..
일단 프로그램실행후 join함수를 실행한후 이름을 넣게되면 waituser에 저장이됩니다
그뒤 create_channel을 사용하여 들어갈 채널을 입력하고
들어갈 user 이름을 넣으면 waituser에 저장된 이름은 삭제가 되고 channel구조체안에 이름이 저장됩니다
이런식으로 해보려고 하는데요...
너무 헷갈리고 개념을 제대로 이해한건지...
잘 모르겠네요...
조언좀 부탁드립니다
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct _user { char user_name[20]; struct _user *user_next; }user; user *waituser_head, *waituser_tail; typedef struct _channel { int user_count; char channel_name[20]; struct _channel *channel_next; user *user_head, *user_tail; }channel; channel *channel_head, *channel_tail; void init_user(void) //waituser를 위한 초기화 { waituser_head=(user*)malloc(sizeof(user)); waituser_tail=(user*)malloc(sizeof(user)); waituser_head->user_next=waituser_tail; waituser_tail->user_next=waituser_tail; } void init_channel_user(void) //channel안의 user에 대한 초기화 { channel_head=(channel*)malloc(sizeof(channel)); channel->user_head=(user*)malloc(sizeof(user)); channel_tail=(channel*)malloc(sizeof(channel)); channel->user_tail=(user*)malloc(sizeof(user)); channel_head->channel_next=channel_tail; channel_tail->channel_next=channel_tail; channel_head->user_head->user_next=channel_head->user_tail; channel_tail->user_tail->user_next=channel_tail->user_tail; } void insert() { char buf[20]; user *t; t=(user*)malloc(sizeof(user)); printf("Input name: "); fgets(buf,20,stdin); buf[(strlen(buf)-1)]='\0'; printf("-----> 빈방에 입장하셨음\n"); t->user_next=waituser_head->user_next; waituser_head->user_next=t; } void check_channel(char *s) { channel *p; p=channel_head->channel_next; while(strcmp(s,p->channel_name) !=0 && p!=channel_tail) { p=p->channel_next; } if(p!=channel_tail) { printf("이미존재하는방입니다\n"); join_channel(); } } void join_channel() { int a; char buf[20]; char user_buf[20]; printf("들어갈 채널: "); fgets(buf,20,stdin); buf[(strlen(buf)-1)]='\0'; check_channel(buf); printf("누가들어갈것인가? "); fgets(user_buf,20,stdin); user_buf[(strlen(user_buf)-1)]='\0'; channel *t; t=(channel*)malloc(sizeof(channel)); strcpy(t->channel_name,buf); t->channel_next=channel_head->channel_next; channel_head->channel_next=t; user *i, *j; channel->user *p; i=waituser_head; j=i->user_next; while(strcmp(user_buf,j->waituser_name) !=0 && j!=waituser_tail) { i=i->user_next; j=j->user_next; } if(j==waituser_tail) return 0; i->user_next=j->user_next; free(j); p=(user*)malloc(sizeof(user)); strcpy(p->user_name,user_buf); p->user_next=channel->user_head->user_next; channel->user_head->user_next=p; }
Forums:
댓글 달기