단순연결리스트에서 역순알고리즘 설명 부탁드려요...
로운
질문 제목 : 단순연결리스트에서 역순알고리즘 설명 부탁드려요...#include stdio.h
#include malloc.h
#include stdlib.h
#include limits.h
#define false 0
#define true 1
typedef int element;
typedef struct listnode {
element data;
struct listnode *link;
} listnode;
typedef struct {
listnode *head; // 헤드 포인터
int length;// 노드의 개수
} listtype;
// phead: 리스트의 헤드 포인터의 포인터
// p : 선행 노드
// new_node : 삽입될 노드
void insert_node(listnode **phead, listnode *p,
listnode *new_node)
{
if( *phead == null ){// 공백리스트인 경우
new_node-link = null;
*phead = new_node;
}
else if( p == null ){ // p가 null이면 첫번째 노드로 삽입
new_node-link = *phead;
*phead = new_node;
}
else { // p 다음에 삽입
new_node-link = p-link;
p-link = new_node;
}
}
// phead : 헤드 포인터에 대한 포인터
// p: 삭제될 노드의 선행 노드
// removed: 삭제될 노드
void remove_node(listnode **phead, listnode *p, listnode *removed)
{
if( p == null )
*phead = (*phead)-link;
else
p-link = removed-link;
free(removed);
}
// 리스트를 초기화한다.
void init(listtype *list)
{
if( list == null ) return;
list-length = 0;
list-head = null;
}
// 리스트안에서 pos 위치의 노드를 반환한다.
listnode *get_node_at(listtype *list, int pos)
{
int i;
listnode *tmp_node = list-head;
if( pos 0 ) return null;
for (i=0; ipos; i++)
tmp_node = tmp_node-link;
return tmp_node;
}
// 리스트의 항목의 개수를 반환한다.
int get_length(listtype *list)
{
return list-length;
}
//
void error(char *message)
{
fprintf(stderr,%s\n,message);
exit(1);
}
// 주어진 위치에 데이터를 삽입한다.
void add(listtype *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(listtype *list, element data)
{
add(list, get_length(list), data);
}
// 리스트의 끝에 데이터를 삽입한다.
void add_first(listtype *list, element data)
{
add(list, 0, data);
}
//
int is_empty(listtype *list)
{
if( list-head == null ) return 1;
else return 0;
}
// 주어진 위치의 데이터를 삭제한다.
void delete(listtype *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(listtype *list, int pos)
{
listnode *p;
if( pos = list-length ) error(위치 오류);
p = get_node_at(list, pos);
return p-data;
}
//
void clear(listtype *list)
{
int i;
for(i=0;ilist-length;i++)
delete(list, i);
}
// 버퍼의 내용을 출력한다.
void display(listtype *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(listtype *list, element item)
{
listnode *p;
p = list-head; // 헤드 포인터에서부터 시작한다.
while( (p != null) ){
// 노드의 데이터가 item이면
if( p-data == item )
break;
p = p-link;
}
if( p == null) return false;
else return return true;
}
//
int main()
{
listtype list1;
init(&list1);
add(&list1, 0, 20);
add_last(&list1, 30);
add_first(&list1, 10);
add_last(&list1, 40);
// list1 = (10, 20, 30, 40)
display(&list1);
// list1 = (10, 20, 30)
delete(&list1, 3);
display(&list1);
// list1 = (20, 30)
delete(&list1, 0);
display(&list1);
printf(%s\n, is_in_list(&list1, 20)==true ? 성공: 실패);
printf(%d\n, get_entry(&list1, 0));
}
질문 내용 :
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;
}
음 전체적으로 이해가 잘 안되요 ㅠㅠ,,
질문요약에 있는 코드가 전체 프로그램 코드입니다,
-
이거이름임
h-1-2-3...이런식으로 리스트가 있을때
h-1-2-3...
h-1-2-3...
h-1-2-3이런식으로 거꾸로 돌리는 함수네요
각 노드형포인터 변수에 1 2 3 번째 노드들을 기억시켜 주면서 링크의 방향을 바꾸는 거에요
이런식으로 그림그리고 각각 변수를 바꿔주면서 한번 해보시면 금방 이해되요
전체적인 이해는;; 책을 보는거에만 그치지 말고 직접 손으로 따라가면서 노드 넣고 삭제도 해보고 그렇게 해보세요
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2700562 | 함수포인터에서요 (7) | 소심한여자 | 2025-07-06 |
2700530 | 전처리문 질문입니다. (1) | 아놀드 | 2025-07-05 |
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 |