프로그래밍[Univ]/데이터구조

[스택] 응용 1 : 문자 역순 출력프로그램

Cloud Travel 2011. 5. 30. 15:26

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

typedef struct nodetype{ 
 char data;
 struct nodetype *next_ptr;
}node;

typedef struct stacktype{ 
 int count;
 node *head_ptr;
}stack;

stack *makestack(){ 
 stack *returnstack; 
 returnstack = (stack *)malloc(sizeof(stack));
 if( returnstack != NULL ){
  memset(returnstack,0,sizeof(stack));
  return returnstack;
 }else{
  printf("Error!! Memory\n");
  return 0;
 }
 return 0;
}

void Push(stack *Pstack, node d){
 node *newnode = (node *)malloc(sizeof(node));
 *newnode = d;
 newnode -> next_ptr = Pstack -> head_ptr;
 Pstack -> head_ptr = newnode;
 Pstack -> count++;
}

void Pop(stack *Pstack){
 if ( Pstack -> count > 0 ){
  node *freenode;
  freenode = Pstack -> head_ptr; 
  Pstack -> head_ptr = Pstack -> head_ptr -> next_ptr; 
  printf("%c",freenode -> data);
  if ( freenode != NULL ){
   free(freenode); 
  }
  Pstack -> count--; 
  return ;
 }else{
  printf("Error!! Empty\n");
  return ;
 }
}

void main(){
 stack *Lstack;
 Lstack = makestack();
 
 printf("String Reverse Program\n");
 printf("Input : ");
 
 while (1){
  node datanode;
  scanf("%c",&datanode.data);
  if ( datanode.data == '\n' ) break;
  Push(Lstack,datanode);
 }
 

 printf("Output : ");

 while( Lstack -> count > 0 ){
  Pop(Lstack);
 }
 
 printf("\n\nEnd\n");
}

-------------------------------------------------------------------------------

정말 단순한 프로그램입니다.

받은 문자를 그대로 스택에 모두 Push한후

문자열 입력이 끝나면 모두 Pop 해주면서 문자열을 역순으로 뽑아주는 프로그램입니다.

-------------------------------------------------------------------------------

월요병이 도져서 정말 아무것도 하기 싫은데-_-; 먼가 아무것도 안하면 내일부터

무지 게일러질것 같아서 책직질하면서 단순한 프로그램 하나 만들어 봤습니다.

내일은 괄호 검사/중위 계산등을 다시한번 보면서

stack 에 대해서 맞치도록 하겠습니다..

월요병때매 오늘은 이만 놀러-ㅅ-;;

P.S : 요새 정말 프로그래밍 할때 스트레스인게... studio C++ 2010 Express 다운받아서
        코딩하는데 지역변수로 계속 지정하라는 에러가 나오면서 안되서 스트레스 받고있습니다-_-;
        기억클래스류에 전혀 에러가 없는데 계속 나는 에러를 고치며 일부로 지역변수로 다바꾸느냐고
        힘들어 미치겠습니다-_-;