단일연결리스트 중간 삽입 코딩해보았는데 잘 안되네요 ㅠㅠ
시아
질문 제목 : 단일연결리스트 중간삽입질문 내용 :제가 데이터구조를 연습하려고 단일연결리스트 코드를 짜보았는데
처음과 마지막 노드에 데이터를 대입하는 건 성공했습니다.
그런데 중간에 삽입하는게 힘드네요 ㅠㅠ
예를 들어 월 화 수 목 금 사이에 민이란 글자를 집어넣을때
수라는 글자 뒤에 집어넣어
월 화 수 민 목 금 이렇게 만들고 싶었습니다.
그런데 원하는 대로 안돌아가는 경우가 많네요...어디가 문제인지 ㅠㅠ
(주석처리한 부분입니다)
#include stdio.h
#include stdlib.h
#include string.h
typedef struct listnode{
char data[10];
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 addfirstnode(linkedlist_h*,char*);
void deletefirstnode(linkedlist_h*);
//void addmiddlenode(linkedlist_h*,char*,char*);
linkedlist_h* createlinkedlist_h(void){
linkedlist_h* l;
l = (linkedlist_h*)malloc(sizeof(linkedlist_h));
l-head=null;
return l;
}
void addlastnode(linkedlist_h* l,char *x){
listnode* newnode;
listnode* p;
newnode = (listnode*)malloc(sizeof(listnode));
strcpy(newnode-data,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;
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 addfirstnode(linkedlist_h* l,char* x){
listnode* newnode;
listnode* p;
newnode=(listnode*)malloc(sizeof(listnode));
strcpy(newnode-data,x);
newnode-link=l-head;
l-head=newnode;
}
void deletefirstnode(linkedlist_h* l){
l-head=l-head-link;
}
/*
void addmiddlenode(linkedlist_h* l,char* conf,char* x){
listnode* newnode;
listnode* temp;
newnode=(listnode*)malloc(sizeof(listnode));
temp=(listnode*)malloc(sizeof(listnode));
strcpy(newnode-data,x);
if(l-head==null){
l-head=newnode;
newnode-link=null;
}
else{
temp=l-head;
while(!strcmp(temp-link-data,conf)){
temp=temp-link;
}
newnode-link = temp-link;
temp-link = newnode;
}
}
*/
int main(){
linkedlist_h* l;
linkedlist_h* pre;
l=createlinkedlist_h();
pre=createlinkedlist_h();
printf((1) 공백리스트 성성!\n);
printlist(l); getchar();
printf((2) 리스트에 4개의 노드 추가하기!\n);
addlastnode(l,월);
addlastnode(l,수);
addlastnode(l,금);
addlastnode(l,토);
printlist(l); getchar();
/*
printf((5) 리스트 중간에 노드 한개 추가하기!\n);
addmiddlenode(l,월,민);
printlist(l); getchar();
*/
printf((3) 리스트 처음에 노드 두개 추가하기!\n);
addfirstnode(l,일);
addfirstnode(l,민);
printlist(l); getchar();
printf((4) 마지막 노드 삭제하기!\n);
deletelastnode(l);
printlist(l); getchar();
printf((6) 리스트 원소를 역순으로 변환하기!\n);
reverse(l);
printlist(l); getchar();
printf((7) 첫번째 노드 삭제하기!\n);
deletefirstnode(l);
printlist(l); getchar();
printf((8) 리스트 공간 해체!\n);
freelinkedlist_h(l);
printlist(l);
getchar();
return 0;
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
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 |
2694929 | 열혈강의 25-2 두번째 문제 질문 | 지우개 | 2025-05-15 |
2694900 | dequeue 에서 리턴값 프린트 방법알려주세요 오늘 12시까지 대화방에 있습니다 도와주세요 | 미투리 | 2025-05-15 |
2694854 | 절대값을 구할때 (2) | 그녀는귀여웠다 | 2025-05-15 |
2694827 | 이제 어떻게 공부해야할지 모르겠네요 | 새얀 | 2025-05-14 |
2694778 | 순열 계산요. | 맛조이 | 2025-05-14 |
2694754 | ShowWindow 함수를 이용하려 하는데 질문있습니다. (2) | 파도 | 2025-05-14 |
2694731 | 리눅스 커널의 시작점 질문 | 미르 | 2025-05-13 |
2694702 | 이거 뭐가문제인가요 코드수정좀 (3) | 맑은 | 2025-05-13 |
2694675 | C언어 후위표기를 중위표기로 | 앨런 | 2025-05-13 |
2694646 | 안녕하세요 파일 합치기 함수! (1) | 연블루 | 2025-05-13 |
2694618 | 잘몰라서 설명부탁드립니다. scanf 관련 (3) | 파라 | 2025-05-12 |