자료구조에서 정보 삭제 소스짜봣는데 에러점여..
초엘
질문 제목 :자료구조 구조체로 짜봣는데정보입력받고 정보삭제하려는데소스 정보삭제하는데 프리부분에서 오류가 뜨네요..질문 내용 : 머가문제일까요?
typedef struct listnode{char data[10];
struct listnode* link;
} listnode;
void delelctnode(linkedlist_h* l, char* del)
{
listnode* p;
listnode* c;
listnode* f;
p=l-head;
c=p-link;
if(strcmp(p-data,del)==0){
f=p;
p=c;
c=c-link;
free(f);
}
else if(strcmp(p-data,del)!=0){
while(strcmp(p-data,del)!=0){
p=p-link;}
f=p;
p=c;
c=c-link;
free(f);
}
}
앞에 melloc함수로 3개자료를 형성했는데
두번쨰 데이터랑 비교해서 같으면 두번째 거를 삭제하려고하는데
그럼 두번째꺼 데이터에 링크를 넣고 링크에 다음 링크를 넣고
두번재꺼를 프리하려고 짯는데
프리를쓰면 처음데이터뜨고 쓰레기값이 막뜨며 에러뜨는데 머가 문제인가여 ㅎ?
그대로 출력이 되네요
전체소스
#include stdio.h
#include stdlib.h
#include string.h
typedef struct listnode{ //단순연결리스트의 노드 구조 정의
char data[10];
char booktitle[50];
char bookname[50];
int money;
struct listnode* link;
} listnode;
typedef struct{ //리스트의 헤드 노드의 구조 정의
listnode* head;
} linkedlist_h;
linkedlist_h* createlinkedlist_h(void);
void freelinkedlist_h(linkedlist_h*);
void addlastnode(linkedlist_h*, char*);
void reverse(linkedlist_h*);
void deletelastnode(linkedlist_h*);
void printlist(linkedlist_h*);
void findadd();
void newaddlastnode();
void delelctnode();
linkedlist_h* createlinkedlist_h(void){ //공백 연결리스트 생성 연산
linkedlist_h* l;
l = (linkedlist_h*)malloc(sizeof(linkedlist_h)); //헤드 노드 할당
l - head = null; //공백 리스트이므로 null 설정
return l;
}
void addlastnode(linkedlist_h* l, char* x){ //리스트의 마지막 노드 삽입 연산
listnode* newnode;
listnode* p;
newnode = (listnode*)malloc(sizeof(listnode)); //삽입할 새 노드 할당
strcpy(newnode-data, x); //새 노드의 데이터 필드에 x 저장
newnode-link= null;
if (l-head == null){ //현재 리스트가 공백인 경우 :
l-head = newnode;
return;
}
p = l-head;
while (p-link != null) p = p-link;
p -link = newnode;
}
//void reverse(linkedlist_h * l){ //리스트의 노드 순서를 역순으로 바꾸는 연산
// listnode* p;
// listnode* q;
// listnode* r;
//
// p = l-head;
// q=null;
// r=null;
//
// while (p!= null){ //노드의 연결을 반대로 바꾸기
// r = q;
// q = p;
// p = p-link;
// q-link = r;
// }
// l-head = q;
//
// }
//
// void deletelastnode(linkedlist_h * l){ //리스트의 마지막 노드 삭제 연산
// listnode* previous;
// listnode* current;
// if (l-head == null) return; //공백 리스트인 경우, 삭제 연산 중단
//
// if (l-head-link == null) { //리스트에 노드가 한 개만 있는 경우,
// free(l-head); // 첫 번째 노드를 메모리 해제하고
// l-head = null; // 리스트 시작 포인터를 null로 설정한다.
// return;
// }
// else {&; //리스트에 노드가 여러 개 있는 경우,
// previous = l-head;
// current = l-head-link;
// while(current -link != null){
// previous = current;
// current = current-link;
// }
// free(current);
// previous-link = null;
// }
// }
//
// void freelinkedlist_h(linkedlist_h* l){ //리스트 전체 메모리 해제 연산
// listnode* p;
// while(l-head != null){
// p = l-head;
// l-head = l-head-link;
// free(p);
// p=null;
// }
// }
void printlist(linkedlist_h* l){ //노드 순서대로 리스트를 출력하는 연산
listnode* p;
printf(l = ();
p= l-head;
while(p != null){
printf(%s, p-data);
p = p-link;
if(p != null) printf(, );
}
printf() \n);
}
void newaddlastnode(linkedlist_h* l, char* x){ //리스트의 마지막 노드 삽입 연산
listnode* newnode;
listnode* p;
newnode = (listnode*)malloc(sizeof(listnode)); //삽입할 새 노드 할당
strcpy(newnode-data, x); //새 노드의 데이터 필드에 x 저장
p=l-head;
newnode-link=l-head;
l-head=newnode;
}
void findadd(linkedlist_h* l ,char* find, char* add)
{
listnode* newnode;
listnode* x;
listnode* p;
newnode = (listnode*)malloc(sizeof(listnode));
strcpy(newnode-data, add);
p=l-head;
if(strcmp(p-data,find)==0){
x=p-link;
p-link=newnode;
newnode-link=x;
}
else if(strcmp(p-data,find)!=0)
{
while(strcmp(p-data,find)!=0){
p=p-link;}
x=p-link;
p-link=newnode;
newnode-link=x;}
return;
}
void delelctnode(linkedlist_h* l, char* del)
{
listnode* p;
listnode* c;
listnode* f;
p=l-head;
c=p-link;
if(strcmp(p-data,del)==0){
f=p;
p=c;
c=c-link;
free(f);
}
else if(strcmp(p-data,del)!=0){
while(strcmp(p-data,del)!=0){
p=p-link;}
f=p;
p=c;
c=c-link;
free(f);
}
}
int main(){
char findname[10];
char addname[10];
char delelct[10];
int select;
linkedlist_h* l;
l = createlinkedlist_h();
printf((1) 공백 리스트 생성하기! \n);
printlist(l); getchar();
printf((2) 리스트에 3개의 노드 추가하기! \n);
addlastnode(l, 월);
addlastnode(l, 수);
addlastnode(l, 금);
printlist(l); getchar();
printf((3) 리스트 마지막에 노드 한개 추가하기! \n);
addlastnode(l, 일);
printlist(l); getchar();
printf((3-1) 마지막 노드 처음으로 보내기! \n);
printlist(l); getchar();
newaddlastnode(l, 처음);
printlist(l); getchar();
printf((3-2) 노드 중간에 추가! \n);
printf(찾을이름검색,추가할이름 검색 \n);
//scanf(%s,name);
gets(findname);
gets(addname);
findadd(l,findname,addname); getchar();
printlist(l); getchar();
printf((3-3) 원하는 노드 삭제! \n);
printf(삭제할 이름검색 검색);
gets(delelct);
delelctnode(l,delelct); getchar();
printlist(l); getchar();
/*printf((4) 마지막 노드 삭제하기! \n);
deletelastnode(l);
printlist(l); getchar();
printf((5) 리스트 원소를 역순으로 변환하기! \n);
reverse(l);
printlist(l); getchar();
printf((6) 리스트 공간을 해제하여, 공백 리스트 상태로 만들기! \n);
freelinkedlist_h(l);
printlist(l);
getchar();*/
return 0;
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2695707 | 3의 배수 나타내기. (2) | 수리 | 2025-05-23 |
2695626 | 피보나치수열 과제 때문에 질문 드립니다. (6) | 옆집언니 | 2025-05-22 |
2695595 | 포인트공부중입니다 int형에서 4=1 인가요? (3) | 족장 | 2025-05-22 |
2695567 | 드라이브 고유번호를 가져오는 함수 (2) | 초코맛사탕 | 2025-05-21 |
2695533 | 음수의 산술변환! 질문이요 ㅠㅠ... (4) | 꽃여름 | 2025-05-21 |
2695506 | 구조체 배열 이용 도서목록 출력 프로그램 (1) | 가을귀 | 2025-05-21 |
2695450 | c언어 함수 질문이요.... | 이슬비 | 2025-05-20 |
2695403 | VirtualAlloc함수 및 메모리 질문 | 크리에이터 | 2025-05-20 |
2695355 | c언어 for함수 | 미쿡 | 2025-05-19 |
2695327 | 안녕하세요 제가 이번에 좀 큰 프로그램을.. | 악당 | 2025-05-19 |
2695295 | mutex동기화의 thread기반 채팅 서버소스 질문입니다 | 그루터기 | 2025-05-19 |
2695270 | 질문이요..swap 관한겁니다..ㅠㅠ (3) | 콩알녀 | 2025-05-19 |
2695244 | 노땅초보궁금한게 하나 있는데요..반복문(while문)초보자질문 (6) | 큰꽃늘 | 2025-05-18 |
2695166 | do while 문 어떤것이잘못된건지 모르겠어요 (2) | 아이폰 | 2025-05-18 |
2695122 | 구조체에 대해 물어보고 싶은게 있습니다 ^^^.. (7) | 수련 | 2025-05-17 |
2695091 | txt 파일 입출력 후 2차 배열에 저장하기입니다. (3) | 헛장사 | 2025-05-17 |
2695063 | 수도요금 프로그램좀 짜주세요. | 시내 | 2025-05-17 |
2695033 | 답변좀요ㅠㅠ (1) | 비사벌 | 2025-05-16 |
2695010 | C++의 STL은 왜 굳이 템플릿화 시켜서 라이브러리를 만드나요? (초보수준의 질문..) (2) | 엘보어 | 2025-05-16 |
2694958 | 로직이 변한다는 것에 대해서 궁금합니다. | 튼동 | 2025-05-16 |