게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
c언어 이중링크드리스트즘 도와주세요
게시물ID : programmer_21632짧은주소 복사하기
작성자 : oooooo
추천 : 0
조회수 : 391회
댓글수 : 2개
등록시간 : 2017/11/05 16:04:09
옵션
  • 베스트금지
  • 베오베금지
  • 본인삭제금지
  • 외부펌금지
 이중으로 연결해서 정순 역순 출력하려는데 역순 출력이 안되네요
제가 잘못짠 부분이 어디인가요? 잘못한데 설명즘 부탁드려요

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


typedef struct booklist
{
    char books[30];      // 책 제목
    char press[30];      // 출판사
    int  price;       // 판매 가격을 저장할 변수
    struct booklist  *next;
    struct booklist  *prev;

} tagBooks;

void Input_List(tagBooks **Ps);
void Display_List(tagBooks **Ps);
void Remove(tagBooks **Ps);
void my_free(tagBooks **Ps);

int main()
{
    tagBooks* pStart = NULL;
    int num = 0;

    while (1)
    {
        printf(" 1. 입력, 2. 출력, 3.삭제, 4.종료 \n");
        scanf("%d", &num);
        switch (num)
        {
        case 1:
            Input_List(&pStart);  
            break;
        case 2:
            Display_List(&pStart);
            break;
        case 3:
            Remove(&pStart);
            break;
        case 4:
            my_free(&pStart);  
            printf("프로그램 종료\n");
            exit(1);
        }
    }
    return 0;
}
void Input_List(tagBooks **Ps)
{
    
    tagBooks *Pnew = (tagBooks*)malloc(sizeof(tagBooks));
    tagBooks *tmp;

    Pnew->next = NULL;
    Pnew->prev = NULL;

    printf("책제목 : ");
    scanf("%s", Pnew->books);
    printf("출판사 : ");
    scanf("%s", Pnew->press);
    printf("판매가격 : ");
    scanf("%d", &Pnew->price);
    printf("\n");

    

    if (*Ps == NULL)
    {
        *Ps = Pnew;
        Pnew = Pnew->next;
    }
    else
    {
        tmp = *Ps;
        
        while (tmp->next != NULL)
        {
            
            tmp = tmp->next;
            
        }
        tmp->next = Pnew;
        Pnew->prev = tmp;
    }
}
void Display_List(tagBooks **Ps)
{
    tagBooks *tmp;
    tmp = *Ps;
    tagBooks *p;

    while (tmp != NULL)
    {
        printf("책 제목 : %s 출판사 : %s 출판 가격 : %d\n", tmp->books, tmp->press, tmp->price);

        p = tmp;
        tmp = tmp->next;
    }


    while (p != NULL)
    {
        printf("책 제목 : %s 출판사 : %s 출판 가격 : %d\n", tmp->books, tmp->press, tmp->price);

        p = p->prev;
    }

}
void Remove(tagBooks **Ps)
{
    tagBooks *Pnew = *Ps;
    tagBooks *tmp = *Ps;
    char re[20];

    printf("삭제할 책이름을 써주세요. ");
    scanf("%s", re);

    do
    {
        printf("%s %s\n", re, Pnew->books);

        if (!strcmp(re, Pnew->books))
        {


            if (Pnew == *Ps)
            {
                tmp = Pnew->next;
            }
            else if (Pnew->next == NULL)
            {


            }
            else
            {
                printf("in\n");
                tmp->next = Pnew->next;


            }
            free(Pnew);

        }

        if (!strcmp(re, Pnew->books))
        {
            Pnew = Pnew->next;
        }

    } while (!strcmp(re, Pnew->books));

}
void my_free(tagBooks **Ps)
{

    tagBooks *tmp;
    tagBooks *val;

    tmp = *Ps;
    val = *Ps;


    while (tmp != NULL)
    {
        val = tmp->next;
        free(tmp);

        tmp = val;
    }

}
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호