연결리스트 질문해보아요!
물고기자리
질문 제목 : 연결리스트 특정 코드 실행 부분질문 내용 :
#include stdio.h
#include stdlib.h // malloc 함수를 위해서 stdlib.h 헤더파일 선언
int main(void)
{
typedef struct node{
char name[20];
char phone[20];
struct node *link;
}linked_node; //구조체 선언(이름,전화번호,링크)
linked_node *new_node, *head, *temp;
int num;
int cnt=2; // 구조체 데이터 관련 카운트 변수 num, 구조체 주소 관련 카운트 변수 cnt
char person[20];
new_node=(struct node *)malloc(sizeof(linked_node));
printf(이름과 전화번호 입력 : );
scanf(%s %s,&new_node-name, &new_node-phone);
new_node-link=null;
head=temp=new_node;
num=2;
while(num=4) // 노드를 4개까지만 만들기위해 카운트를 4까지 제한.
{
new_node=(struct node *)malloc(sizeof(linked_node));
if(new_node==null){
printf(memory allocation error \n);
exit(0);
}
printf(이름과 전화번호 입력 : );
scanf(%s %s,&new_node-name, &new_node-phone); //이름과 전화번호 데이터 입력.
new_node-link=null;
temp-link=new_node;
temp=new_node;
num=num+1;
}
temp=head; // temp가 첫번 째 노드의 주소값을 가지고 있음.
num=1;
while(temp-link!=null)
{
printf(%s %s \n,temp-name, temp-phone);
temp=temp-link;
num++;
}
printf(%s %s \n, temp-name, temp-phone);
temp=head;
printf(head 주소 값 : %d \n, &head);
printf(1 번째 노드의 주소 값 : %d \n, head);
while(temp-link!=null)
{
printf(%d 번째 노드의 주소 값 : %d \n, cnt, temp-link);
temp=temp-link;
cnt++;
}
new_node=head; //첫 번째 노드의 주소를 다시 new_node 에 저장.
head=new_node-link; //첫 노드가 가리키고 있는 다음 노드의 주소값을 head에 저장.
free(new_node);//new_node에 할당한 메모리를 반납.
printf(head 가 가리키고 있는 주소 값 : %d \n,head);
temp=head;
printf(찾고자 하는 사람의 이름을 입력하세요 : );
scanf(%s,person);
while(temp-name!=person)
{
if(temp-link==null)
printf(not found \n);
else
temp=temp-link;
}
new_node=(struct node *)malloc(sizeof(linked_node));
printf(새 사람의 이름과 전화번호 입력 : );
scanf(%s %s,&new_node-name, &new_node-phone);
new_node-link=temp-link;
temp-link=new_node;
return 0;
}
굵은 글씨로 써진 코드 위 까지는 실행이 아주 잘됩니다..
근데 찾고자 하는 사람의 이름을 입력하면
계속 not found가 떠요 ㅠㅠ...
temp도첫 노드 삭제후둘째 노드의 주소를 대입시켰는데도 말이죠... 즉, link의 값은 null이 나올리가 없는데 말이에요...
해결방법좀 알려주세요 ㅠ_ㅠ!
-
무슬
person변수가 문자열인데... temp-name != person 이렇게 하시면 되나요?
문자열 비교는 strcmp,strncmp 등등으로...
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2700510 | c언어를 어케하면 잘할수 있을까요.. | 연연두 | 2025-07-05 |
2700484 | 두 개가 차이가 뭔지 알려주세요...(소수 찾는 프로그램) (2) | 날위해 | 2025-07-05 |
2700426 | 인터넷 창 띄우는 질문이요 (1) | 정훈 | 2025-07-04 |
2700400 | 원넓이를 계산이요 ㅜㅜ | 천칭자리 | 2025-07-04 |
2700368 | if에 관해서 질문이요... | Orange | 2025-07-04 |
2700339 | 이거 결과값이 왜이런건지.. (4) | 그댸와나 | 2025-07-04 |
2700313 | 파일 읽어서 저장하는데 빈파일일 경우 문재가 발생하네요.. (2) | 크나 | 2025-07-03 |
2700287 | 구조체 동적할당 연습을 하는데 오류가 뜹니다...(해결) (3) | 아련나래 | 2025-07-03 |
2700264 | 문자와 숫자 동시에 입력??? | 글고운 | 2025-07-03 |
2700236 | txt파일로만 쓰고 읽게 하려면 어떻게 해야 하나요..?? (8) | 미국녀 | 2025-07-03 |
2700211 | 전위 연산자 (2) | 어른처럼 | 2025-07-02 |
2700183 | C에서 파일이름을 받고, 그 파일의 사이즈를 출력해줘야하는데 내용이 출력이 안되네요 ;ㅅ; | 피스케스 | 2025-07-02 |
2700150 | 꼭좀 도와주세요ㅠㅠㅠ | 호습다 | 2025-07-02 |
2700095 | 연산문제...질문... | 오빤테앵겨 | 2025-07-01 |
2700070 | while문 , 3의배수 출력하는 프로그램좀 짜주세욤. | 횃불 | 2025-07-01 |
2700041 | 초보인데요 ㅎ 배열안에 배열을 집어넣을수 있나요?? | 헛장사 | 2025-07-01 |
2700012 | 배열// (1) | 전갈자리 | 2025-07-01 |
2699895 | 무한루프에 빠집니다.!! 해결좀부탁드려요 (10) | 선아 | 2025-06-30 |
2699842 | 질문을 너무 많이 하네여.....죄송.... (2) | 해님꽃 | 2025-06-29 |
2699816 | 오류 질문입니다.. (1) | 해비치 | 2025-06-29 |