C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[48961] 자료구조 링크드 리스트 설명좀
김병조 [milkiss20] 2018 읽음    2007-04-16 16:21
자료구조 시간에 책보고 쭉 작성한건데요
링크드 리스트 하는건데 .ㅡㅜ 모르겠네요 걍 치라고 해서 친건데
설명이나 주석좀 달아주시면 정말 ㄳㄳ

#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*);



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");
}

int main(){
    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("(4) 마지막 노드 삭제하기! \n");
    deletelastnode(L);
    printlist(L); getchar();

    printf("(5) 리스트 원소를 역순으로 변환하기! \n");
    reverse(L);
    printlist(L); getchar();

    printf("(3) 리스트에 마지막 노드 한개 추가 하기! \n");
    addlastnode(L, "일");
    printlist(L); getchar();

    printf("(6) 리스트 공간을 해제하여, 공백 리스트 상태로 만들기! \n");
    freelinkedlist_h(L);
    printlist(L); getchar();

    getchar();
    return 0;
}

+ -

관련 글 리스트
48961 자료구조 링크드 리스트 설명좀 김병조 2018 2007/04/16
48967     Re:자료구조 링크드 리스트 설명좀 이현진 1467 2007/04/16
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.