어느부분이틀렸는지 봐주세요ㅜㅜ
바다
초보입니다학교서 수업듣고 있는데연결리스트로 구현된 리스트 ADT 테스트 프로그램을 짜는건데요일단 책에 있는걸 그대로 써서 프로그램 돌려보라고 교수님꼐서 그러셨는데그대로 타이핑해 해봤는데 오류가 엄청 뜨더라구요...좀 봐주실수있나요ㅜㅜ부탁드립니다.#includestdio.h
#includemalloc.h
#includestdlib.h
#includelimits.h #define FALSE 0
#define TRUE 1 typede fint element;
typedef struct ListNode{
element data;
struct ListNode *link;
}ListNode;
typedef struct{
ListNode *head; //헤드포인터
int length; //노드의개수
}LinkedListType; 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);
} //리스트안에서pos 위치의노드를반환한다. ListNode *get_node_at(LinkedListType *list, int pos){
int i;
ListNode *tmp_node = list-head;
if(pos0) return NULL;
for(i=0; ipos; i++)
tmp_node = tmp_node-link;
return tmp_node;
} //리스트의항목의개수를반환한다. int get_length(LinkedListType *list){
return list-length;
} //주어진위치에데이터를삽입한다. void add(LinkedListType *list, int position, element data){
ListNode *p;
if((position = 0) && (position = list-length)){
ListNode*node= (ListNode *)malloc(sizeof(ListNode));
if(node == NULL)error();
node-data = data;
p = get_node_at(list,position-1);
insert_node(&(list-head),p,node);
list-length++;
}
} //리스트의끝에데이터를삽입한다.
void add_last(LinkedListType *list, element data){
add(list, get_length(list), data);
} //리스트의앞에데이터를삽입한다.
void add_first(LinkedListType *list, element data){
add(list, 0, data)
} int is_empty(LinkedListType *list){
if(list-head == NULL) return 1;
else return 0;
}
//주어진위치의데이터를삭제한다. void delete_(LinkedListType *list, int pos){
if(is_empty(list) && (pos = 0) && (pos list-length)){
ListNode *p = get_node_at(list, pos-1);
remove_node(&(list-head),p,(p!=NULL)?p-link:NULL);
list-length--;
}
} element get_entry(LinkedListType *list, int pos){
ListNode *p;
if(pos = list-length)error(위치);
p = get_node_at(list, pos);
return p-data;
} void clear(LinkedListType *list){
int i;
for(i=0; ilist-length; i++)
delete(list,i);
} //버퍼의내용을출력한다. void display(LinkedListType *list){
int i;
ListNode *node=list-head;
printf(();
for(i=0; ilist-length; i++){
printf(%d,node-data);
node = node-link;
}
printf()\n);
} //데이터값이s인노드를찾는다. int is_in_list(LinkedListType *list, element item){
ListNode *p;
p = list-head;
while( (p != NULL) ){
if( p-data == item )
break;
p = p-link;
}
if(p == NULL) return FALSE;
else return True;
}//리스트를초기화한다. void init(LinkedListType *list){
if(list == NULL) return;
list-length = 0;
list-head = NULL;
} int main(){
LinkedListType list1;
init(&list1);
add(&list1, 0, 20);
add_last(&list1, 30);
add_first(&list1, 10);
add_last(&list1, 40);
display(&list1);delete(&list1, 3);
display(&list1);
delete(&list1, 0);
display(&list1);
printf(%s\n, is_in_list(&list1, 20)==true ? 성공: 실패);
printf(%d\n, get_entry(&list1, 0));
}
-
사랑
아아 함수였는데 오타가 있었네요 _언더바가 한개 들어가있어서
오류없이 수정되었습니다 :) -
밝은빛누리예
delete가 함수인가요? 저렇게만 쓰시면 동적 할당된 list를 메모리에서 해제하는 기능일텐데..
-
다니엘
void clear(LinkedListType *list){
int i;
for(i=0; ilist-length; i++)
delete(list);
}
이 부분에서 delete 가 정의되지 않았다고 나오는데 이건 왜그러죠
delete(list,i);
해도 안되는거 같은데..
재 능력으로는 모든 수를 해도 안되네요..ㅜㅜㅜ