중첩 구조체의 연결리스트

ddong90의 이미지

중첩된 구조체에 연결리스트를 사용해보고싶어서 채팅을 참조하여 어떻게 되는건지 해보려고 하는데요..
일단 프로그램실행후 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;
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.