stack에서 linked list로 하는 거에서요
빛초롱
질문 제목 :
각각의 줄마다 설명좀 부탁드려도 될까요? 이해가 도저히 안되네요 ㅠ
예를 들어 버퍼는 무엇이고 pcurrent는 무엇을 의미하는지 등등 부탁드립니다. ㅠ
도저히 모르겠습니다.
질문 내용 :
#includestdio.h
#includestdlib.h
#includestring.htypedef struct _stack stack;typedef struct _stack //노드
{
char *data;
stack *next;
stack *prev;
}stack;void addnode(stack *l); //노드를 추가하는 함수
void deletenode(stack *l); //노드를 삭제하는 함수int main()
{
stack *head;
stack *current;
stack *pcurrent;
char buffer[80];
stack ini;
int input;
int counter=0;
int i;
head = &ini;
while(1)
{
printf(\n1.push 2.pop 3.exit : );
scanf(%d,&input);
fflush(stdin);
switch(input)
{
case 1://넣을값을 입력받는다.
{
if(counter == 0) //처음에는 current 를 next 로 이동시키지 않는다.
{
printf(넣을값을 입력하세요 : );
gets(buffer);
addnode(head);
current = head;
current-data = (char *)malloc( strlen(buffer) + 1 ); //char 포인터를 글자수만큼만 동적할당한다.
strcpy(current-data , buffer);
system(cls);
printf(%s\n,current-data);
counter++;
}
else
{
printf(넣을값 입력하세요 : );
gets(buffer);
addnode(current);
current = current-next; // = 두번째부터
current-data = (char *)malloc( strlen(buffer) + 1 );
strcpy(current-data , buffer);
pcurrent = head;
counter++; // 입력받는 갯수만큼 counter 를 증가시킨다
system(cls);
for( i = 0 ; i counter ; i++ ) //counter 만큼만 출력한다.
{
printf(%s\n,pcurrent-data);
pcurrent = pcurrent-next;
}
}
break;
}
case 2:
{
if(counter != 0) // counter 가 0 이면 실행하지 않는다.
{
pcurrent = head;
while(1)
{
if(pcurrent-next == null )
{
break;
}
pcurrent = pcurrent-next;
}
current = current-prev;
deletenode(pcurrent);
counter--;
pcurrent = head;
system(cls);
for( i = 0 ; i counter ; i++ )
{
printf(%s\n,pcurrent-data);
pcurrent = pcurrent-next;
}
}
else //counter 가 0 일때 에러를 출력한다.
{
system(cls);
printf(error\n);
}
break;
}
case 3:
{
return 0; // 종료
}
}
}
return 0;
}
}
void addnode(stack *l)
{
static int ctr=0; // 첫번째 호출과 두번째호출을 구분하기위한 static 변수
if( ctr 0) //두번째부터 l-next 에 동적할당
{
l-next = (stack *)malloc(sizeof(stack));
l-next-next = null;
l-next-prev = l;
}
else // 첫번째는 l-next 가 존재하지 않음.
{
l = (stack *)malloc(sizeof(stack));
l-next = null;
l-prev = null;
ctr++;
}
}
void deletenode(stack *l) // l 포인터를 free 시킨다. 이 함수는 맨 마지막 노드만 삭제할 수 있다.
{
stack *temp;
temp = l;
l-prev-next = null;
free(temp);
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692424 | 2차원배열 자료입력질문이요! (1) | 똘끼 | 2025-04-22 |
2692401 | 유닉스안에서 C언어를 이용한 명함 만들기 입니다; 이해안가는 부분이있네요 | 2gether | 2025-04-22 |
2692374 | 고수님들 댓글 마니부탁해요!!! (2) | 엄지 | 2025-04-22 |
2692343 | scnaf에 자꾸 선언을 참조하라는데;; (8) | 도래 | 2025-04-22 |
2692282 | 도스상에서 생성된 exe파일에 press~ 뜨게 하기 (4) | 회사원 | 2025-04-21 |
2692256 | scanf("%*c"); ㅠㅠ 고수님들 | 거북이 | 2025-04-21 |
2692230 | 하노이탑 질문입니다. (1) | 미쁘다 | 2025-04-21 |
2692210 | 정보 올림피아드 문제인데.. 풀이 과정이 궁금합니다.(재귀함수) (5) | 물티슈 | 2025-04-20 |
2692144 | C언어와 리눅스에 대한 질문입니다. | 싴흐한세여니 | 2025-04-20 |
2692114 | 컨텍스트 스위칭하는데 걸리는 시간 측정.. | YourWay | 2025-04-19 |
2692086 | 간접참조 연산자, 증감연산자 질문이용! (2) | 블랙캣 | 2025-04-19 |
2692056 | 주석좀 달아주세요. 몇개적엇는데 몇개만달아주세요. (2) | DevilsTears | 2025-04-19 |
2691978 | 진수 쉽게 이해하는법... (3) | 지지않는 | 2025-04-18 |
2691949 | getchar() 한 문자를 입력받는 함수 질문 | 채꽃 | 2025-04-18 |
2691919 | 배열 정렬 및 합치기 질문입니다. | 사과 | 2025-04-18 |
2691845 | c언어왕초보 질문이 있습니다........ | 루나 | 2025-04-17 |
2691815 | void add(int num); 함수... (4) | 살랑살랑 | 2025-04-17 |
2691756 | 명령 프롬프트 스크롤바가 없어요 | 두메꽃 | 2025-04-16 |
2691725 | 자료구조에 관련해서 질문이 있어 글을 올립니다. | 누리알찬 | 2025-04-16 |
2691697 | if 문에서 구조체 배열에 저장되있던 문자열 검사하는 법 ? (2) | 민트맛사탕 | 2025-04-16 |