링크드 리스트에 관한 질문입니다.

song의 이미지

코드는 다음과 같습니다.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct theater {
	int row;
	int sit;

	struct theater *prev;
	struct theater *next;
};

struct theater *first_theater;

void theater_select();
void theater_insert();
int theater_total();
void main(void)
{
	
	int sub_menu=-1;
	first_theater=NULL;

	while ( sub_menu !=0 ){
		
		printf("1. 상영관 정보 출력\n");
		printf("2. 상영관 추가\n");
		printf("3. 상영관 정보 수정\n");
		printf("4. 상영관 삭제\n");
		printf("0. 상위메뉴\n");


		printf("\n>");
		
		scanf("%d",&sub_menu);

		
		switch(sub_menu){
			case 1:
				theater_select();
				break;
			case 2:
				theater_insert();
				break;
		}
		
	}

}

void theater_select()
{
	int i=0;
	
	struct theater *current = first_theater;

	i = theater_total();
	while(current)
	{
		printf("%d 관 열수 : %d 좌석수 :%d\n",i,current->row,current->sit);
		current  = current->next;
	}
	if(i==0)
		printf("\n없다..\n");
	
	

}
void theater_insert(void)
{
	int i=0;
	int s,r;

	struct theater *res;

	struct theater *tmp = first_theater;

	
	printf("%d관 정보 입력\n",theater_total()+1);
	printf("열 수: ");
	scanf("%d",&r);
	printf("\n좌석 수: ");
	scanf("%d",&s);
	
	printf("\n입력 내용이 정확합니까(yes : 1 , no : 2)?");
	scanf("%d",&i);
	if(i==2)
		return;
	
	res = malloc(sizeof(*res));
	res->row=r;
	res->sit=s;

	if(first_theater == NULL) {
		first_theater = res;
		res->next = NULL;
		res->prev = NULL;

	} else {
		tmp = first_theater;
		
		while(tmp){
		
			tmp = tmp->next;
		}
		if(tmp == NULL) {
			printf("sdfsfds\n");
			return;
		}
		tmp->next = res;
		res->next = NULL;
		res->prev = tmp;
	}

	
}

int theater_total()
{
	int i=0; 
	struct theater *current = first_theater;

	while(current){
		i++;
		current=current->next;
	}
	return i;
}

여기서 처음 추가시에는 되는데 그 다음 추가부터는 포인터가 널인 이유가 ...ㅠ.ㅠ 날이 밝아 오내요...
hb_kim의 이미지

		while(tmp){
		
			tmp = tmp->next;
		}
		if(tmp == NULL) {
			printf("sdfsfds\n");
			return;
		}

tmp 가 NULL 이 될때까지 루프를 돌고나서 tmp 가 NULL 인지 체크하면 당연 항상 TRUE 겠죠. 이렇게 바꾸면 어떨까요. 잘된다는 보장은 없지만...

		while(tmp->next){
		
			tmp = tmp->next;
		}
		if(tmp == NULL) {
			printf("sdfsfds\n");
			return;
		}
angpoo의 이미지

뭐 이런 숙제스런 질문을 다...

근데 prev, next 정의까지 했으면 double linked list일텐데
first_theater 처럼 last_theater라는걸 만들면 while루프 없이 바로 마지막에 붙여 넣을 수 있습니다.

운형의 이미지

hb kim 님 말대로 하면 우선 문제는 해결.. 그런데...

더블 리스트를 왜 쓰신거죠.?

지금 처럼 사용하실 거면, 싱글로 그냥 쓰세요.

current를 항상 리스트 처음 부터 가져 갈 거라면.. -_-

current를 전역(이거 싫어하는 분들 많죠..) 이던, main에서 선언해서 인자로 넘기던...

마지막에 삽입한 노드 혹은 검색한 노드를 가르키게 해놓으면, 삽입이던 검색이던, 리스트 전체를 훑어 보는 소모적인 일을 하지 않아도, 됩니다.

현재 사용하신건, 더블을 가장한 싱글..

Do you think that's the air you are breathing now?

bugiii의 이미지

angpoo wrote:
뭐 이런 숙제스런 질문을 다...

숙제스럽다... 놀라운 표현입니다. -_-;

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.