C언어 수업정리/4월 수업정리

4월 23일 연결리스트와 반복문

송시혁 2013. 4. 23. 15:58

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

typedef struct node
{
  char data;
  struct node *next;
}NODE;
void print_list (NODE *head);  
int main()
{
  FILE *fp;
  NODE * temp = NULL, *list = NULL;
  char ch;
  
  if((fp= fopen("d10-5.txt""r")) == NULL)
  {
    printf("file open error!");
    exit(-1);
  }
  fscanf(fp, "%c"&ch);
  while(!feof (fp))
  {
    temp=(NODE *) malloc(sizeof(NODE));
    temp->data = ch;
    temp->next = list;
    list = temp;
    fscanf(fp, "%c"&ch);
  }
  fclose(fp);

  temp = list;
  print_list(list);
  
  temp= list;
  while(temp != NULL)
  {
    //printf("%c ->", temp->data);
    list = temp;
    temp = temp->next;
    free(list);
    
    
    
  }
  //printf(" NULL\n");

  return 0;
}


void print_list (NODE *head)
{
  if(head == NULL)
  {
    printf("NULL\n");
  }
  else
  {
    printf("%c ==> ", head->data);
    print_list (head->next);
  }
}