이중연결리스트 검색

julley123의 이미지

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
 
#define SIZE 3000
 
 
void menu();
void InitcafList(); //cafeteria(식당) 
void addcaf(char *str);
void removecafList(char *str);
void SearchcafList(char *str); // cafeteria Search
void ShowcafList();
void SaveData();
void LoadData();
 
 
struct cafif {
	int cafnum;
	char cafName[200];
	char adress[40];
	char tel[30];
};
 
 
typedef struct caf
{
	struct cafif cafinfo[SIZE];
	struct caf *next;
	struct caf *prev;
}cafList;
 
cafList *Head, *Tail;
 
int main()
{
	int x;
	char str[200];
	InitcafList();//링크드 리스트 초기화
	LoadData();//로딩
 
 
	while (1)
	{
		menu();
		scanf("%d", &x);
		fflush(stdin);//입력 버퍼 지움(scanf에서 남은 엔터 문자)
 
 
		switch (x)
		{
		case 1:
			ShowcafList();
			break;
		case 2:
			printf("추가할 음식점 정보 입력 :");
			gets(str);//스페이스 문자까지 입력 받을수 있게 gets함수 사용  
			addcafList(str);
			break;
		case 3:
			printf("삭제할 음식점 이름 입력 (전화번호):");
			gets(str);
			removecafList(str);
			break;
		case 4:
			printf("검색할 음식점 입력 (전화번호):");
			gets(str);
			SearchcafList(str);
			break;
		case 5:
			SaveData();//종료와 동시에 음식점리스트 저장
			return 0;
			break;
		}
	}
	return 0;
}
 
 
void menu()
{
	printf("==================================\n");
	printf("**전주시 음식점**\n");
	printf("1. 음식점 목록\n");
	printf("2. 음식점 추가\n");
	printf("3. 음식점 삭제\n");
	printf("4. 음식점 검색\n");
	printf("5. 종료\n");
	printf("==================================\n");
}
 
 
void InitcafList()
{
	//처음과 끝 메모리할당
	Head = (cafList*)malloc(sizeof(cafList));
	Tail = (cafList*)malloc(sizeof(cafList));
	Head->next = Tail;
	Tail->prev = Head;
}
 
 
 
 
 
 
void addcafList(char *str)
{
	int go;
	cafList *CL;
	cafList *add = (cafList*)malloc(sizeof(cafList));
	CL = Head->next;
	if (strcmp(CL->cafinfo->cafName, str) == 0)
	{             
		printf("같은 이름의 음식점이 이미 존재합니다.\n");
		printf("다시 입력하시려면 0, 메뉴로 돌아가시려면 1을 입력하세요 : ");
		scanf("%d", &go);
		if (go == 0)
		{
			printf("추가할 책 이름 입력:");
			gets(str);
			addcafList(str);
		}
		else if (go == 1)
			menu();
	}
	strcpy(add->cafinfo->cafName, str);
	Tail->prev->next = add;//맨뒤에 add를 연결
	add->prev = Tail->prev;
	add->next = Tail;
	Tail->prev = add;
}
void removecafList(char *str)
{
	cafList *del, *CL;
	CL = Head->next;
	while (CL != Tail)
	{
		if (strcmp(CL->cafinfo->cafName, str) == 0)//지울 문자열을 찾았다면
		{
			del = CL;
			CL->prev->next = CL->next;
			CL->next->prev = CL->prev;//지울 객체를 건너뛰어 객체 연결
			free(del);
			return;
		}
		CL = CL->next;
	}
}
 
 
void SearchcafList(char *str)
{
	cafList *CL;
	CL = Head->next;
	while (CL != Tail)
	{
		if (strcmp(CL->cafinfo->tel, str) == 0)
		{
			printf("%s 찾았습니다\n", str);
			return;
		}
		CL = CL->next;
	}
	printf("%s 찾지못했습니다\n", str);
}
 
 
 
 
 
 
void ShowcafList()
{
	cafList *CL;
	CL = Head->next;
	printf("음식점 이름\n");
	while (CL != Tail)
	{
		printf("%s\n", CL->cafinfo->cafName);
		CL = CL->next;
	}
}
 
 
void SaveData()//데이터 저장
{
	cafList *BL;
	FILE *fp = fopen("cafe.txt", "w");
	BL = Head->next;
	printf("저장된 음식점 목록\n");
	while (BL != Tail)
	{
		fprintf(fp, "%s\n", BL->cafinfo->cafName);
		BL = BL->next;
	}
	fclose(fp);
}
 
 
void LoadData()//데이터 로딩
{
	FILE *fp = fopen("cafe.txt", "r");
	char str[200];
 
	if (fp == NULL) return;
	while (1)
	{
		if (fgets(str, 200, fp) == NULL) break;
		if (strlen(str) > 1)
		{
			str[strlen(str) - 1] = '\0';
			addcafList(str);
		}
	}
	fclose(fp);
}

컴파일 하시면 4번 전화번호 검색을 통해서 음식점명 음식점그룹 음식점주소 음식점 전화번호가 나오게 하고 싶은데

어떻게 코딩을 해야할지 모르겠습니다. 도움 부탁드립니다.

그리고 변수에 char cafgroup[50]; 선언을 하면 컴파일 중지가 되어 그것또한 문의 드립니다.

File attachments: 
첨부파일 크기
Plain text icon cafe.txt232.53 KB
mauri의 이미지

그냥 지나가려다가 전주로 이사한지 한 15년된 정으로 몇자 적습니다.

4번 전화번호 검색을 통해서 음식점명 음식점그룹 음식점주소 음식점 전화번호가 나오게 하고 싶은데
ㄴ입력, 삭제가 되는데, 검색을 못하시겠다는 것이 어떤 의미인지를 잘 모르겠습니다.
혹시 부분 문자열을 검색하는 레퍼런스를 모르시는거라면 strstr을 사용하시면 되구요.
어느 한 노드에서 검색대상 문자열을 찾았다고 리턴하는게 아니라, tail을 만날때까지 검색문자가 들어간 음식점 주소나 이름 등이 포함된 노드 정보는 죄다 출력해 주시면 될듯 싶습니다.

char cafgroup[50]; 선언을 하면 컴파일 중지가 되어
ㄴ어디에 선언을 하면 컴파일이 중지된다는 말씀이신지요??
어디에 선언을 하면 어떤 오류가 나는지를 좀더 구체적으로 알려주세요.

그리고, 질문에 없지만..

typedef struct caf
{
	struct cafif cafinfo[SIZE];
	struct caf *next;
	struct caf *prev;
}cafList;

이건 좀 이상합니다. 링크드 리스트인데 각 노드별로 카페정보 구조체를 배열(그것도 무려 3000개!!!) 을 가지고 있다니..
카페정보노드가 100개만 되도, 카페정보를 3000 X 100 = 300000개를 갖게 되네요..;;

void addcafList(char *str)
{
	int go;
	cafList *CL;
	cafList *add = (cafList*)malloc(sizeof(cafList));
	CL = Head->next;
	if (strcmp(CL->cafinfo->cafName, str) == 0)
	{             
		printf("같은 이름의 음식점이 이미 존재합니다.\n");
		printf("다시 입력하시려면 0, 메뉴로 돌아가시려면 1을 입력하세요 : ");
		scanf("%d", &go);
		if (go == 0)
		{
			printf("추가할 책 이름 입력:");
			gets(str);
			addcafList(str);
		}
		else if (go == 1)
			menu();
	}
	strcpy(add->cafinfo->cafName, str);
	Tail->prev->next = add;//맨뒤에 add를 연결
	add->prev = Tail->prev;
	add->next = Tail;
	Tail->prev = add;
}

그리고, 이것도 이상합니다.
갑자기 책 이름이 왜 나오는지는 둘째치고서라도..;;
카페 이름만 받으면 대체 주소나 전화번호는 언제 넣나요.. ㅡ_ㅡ)???

암튼.. 손댈곳이 한두곳이 아니어서 좀 총체적 난국인듯 싶습니다.
다른 사람 코드 긁어다가 우다다 만드는게 아니라요.

먼저 리스트 출력부터 만들어 보시구요(입력은 하드코딩)
그다음 입력을 만들어 보시구요..
그 다음 검색..
그 다음 삭제..

이런순으로 차근차근 만들어 보시기 바랍니다.

댓글 달기

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