링크드리스트에 관한 질문입니다.
Together
질문 제목 : 링크드리스트에 관한 질문입니다.링크드리스트에 관한 질문입니다.질문 내용 :
제가 아직 초보라서 책 보면서 과제를 하고 있는데요
링크드리스트로 제품 목록을 넣는걸 하고 싶은데 책에는 int형으로 입력이 되어있는데
글자를 넣을려면 어떻게 해야 하나요??
구제체에 char[] d; 이런식으로 해줘야 하나요??????
안되던데 ㅠㅠ;;;;; 어떻게 해야 될지....... 아래는 책의 소스 입니다. ㅠㅠ;;
#include stdio.h
#include stdlib.h
typedef int element;
typedef struct listnode
{
element data;
struct listnode *link;
} listnode;
void error(char *message)
{
fprintf(stderr, %s\n, message);
exit(1);
}
void insert_node(listnode **phead, listnode *p, listnode *new_node)
{
if(*phead == null)
{
new_node-link = null;
*phead = new_node;
}
else if(p == null)
{
new_node-link = *phead;
*phead = new_node;
}
else
{
new_node-link =p-link;
p-link = new_node;
}
}
void remove_node(listnode **phead, listnode *p, listnode *removed)
{
if(p == null)
*phead = (*phead)-link;
else
p-link = removed-link;
free(removed);
}
void display(listnode *head)
{
listnode *p = head;
while(p != null)
{
printf(%d-, p-data);
p = p-link;
}
printf(\n);
}
void display_recur(listnode *head)
{
listnode *p = head;
if(p != null)
{
printf(%d-, p-data);
display_recur(p-link);
}
}
listnode *search(listnode *head, int x)
{
listnode *p;
p = head;
while(p != null)
{
if(p-data == x) return p;
p = p-link;
}
return p;
}
listnode *concat(listnode *head1, listnode *head2)
{
listnode *p;
if(head1 == null) return head2;
else if(head2 == null) return head1;
else
{
p = head1;
while(p-link != null)
p = p-link;
p-link = head2;
return head1;
}
}
listnode *reverse(listnode *head)
{
listnode *p, *q, *r;
p = head;
q = null;
while(p != null)
{
r = q;
q = p;
p = p-link;
q-link = r;
}
return q;
}
listnode *create_node(element data, listnode *link)
{
listnode *new_node;
new_node = (listnode *)malloc(sizeof(listnode));
if(new_node == null) error(메모리 할당 에러);
new_node-data = data;
new_node-link = link;
return(new_node);
}
int main(void)
{
listnode *list1=null, *list2=null;
listnode *p;
insert_node(&list1, null, create_node(10, null));
insert_node(&list1, null, create_node(20, null));
insert_node(&list1, null, create_node(30, null));
display(list1);
remove_node(&list1, null, list1);
display(list1);
insert_node(&list2, list1, create_node(60, null));
insert_node(&list2, null, create_node(70, null));
insert_node(&list2, null, create_node(80, null));
display(list2);
list1 = concat(list1, list2);
display(list1);
list1 = reverse(list1);
display(list1);
p = search(list1, 20);
printf(탐색성공 : %d\n, p-data);
}
-
핑크빛입술
char *Name;
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
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 |
2699763 | 질문입니다 ! 꼭 좀 도와주세요ㅠㅠ (2) | 미라 | 2025-06-28 |
2699555 | c언어 다항식을 입력을 했는데 왜 출력이 안될까요? | 피스케스 | 2025-06-27 |