본문 바로가기
코스웨어/10년 시스템제어

시스템제어 - 4월2일 보고서- 8번 남민호

by 알 수 없는 사용자 2010. 4. 4.
728x90
반응형
 
#include <stdio.h> 
#include <process.h>
#include <stdlib.h>
typedef struct node
{
  char data;
  struct node *next;
}NODE;
typedef struct book BOOK;
구조체
char의 1바이트와 struct node의4바이트 총 5바이트로 되어 있다.
NODE *insert (char item,NODE *list);
int main() 
  NODE *list = NULL;
  char ch;
 
  while('e' != ch)
  {
    scanf("%c",&ch);
    list=insert(ch,list);
  }
  for(;list->next != NULL;list = list ->next)
  {
  printf("%c",list->data);
  }
  free(list);
  return 0;
}
우선 list NULL 주어 초기화 시킨다
while문의 조건식 ch 'e'아니면 실행 한다
ch 입력받고 insert 불러들여 ch값과 list 넘겨준다
insert() 끝나면 넘겨받은 값이 ilst 대입 대고
for에서 조건식에 만족하지 않을 때까지 실행
for문이 끝나면 할당받은 메모리를 시스템에 돌려준다.
NODE *insert (char item,NODE *list)
{
  NODE *current = NULL;
  NODE *follow = NULL;
  NODE *newnode = NULL;
 
  current = follow = list;
 
  if((newnode =(NODE *)malloc(sizeof(NODE))) == NULL)
  {
    printf("NO!!!");
    return list;
  }
 
  newnode->data = item;
 
  while((current != NULL) && (current->data < item))
  {
    follow = current;
    current = current->next;
  }
 
  newnode->next = current;
  if(current == list)
  {
    list = newnode;
  }
  else
  {
    follow->next = newnode;
  }
  return list;
}
연결리스트에 새로운 노드 삽입부 
노드삽입에는 4가지가 있다
1. list를 NULL로 받고 젤 처음 삽입
2. list를 NULL로 받지않고 젤 처음 삽입
3. 마지막에 삽입
4. 중간에 삽입
Bitmap
 
main()에서 처음 list가 넘어와 NULL값으로 오기때문에 4가지중 
1번째한다
 
우선 newnode이 동적할당을 받아 노드가 생성 된다.
이때 newnode이 NULL이면 list를 리턴한다.
할당받은 메모리 data부분에 main()에서 넘겨받은 item값을 대입한다
 
while문의 조건식은 첫 조건식은 끝부분인지 확인하는 조건식이고
2번째 조건식은 입력한 값을 정렬하게 하는 조건식이다
조건식을 모두 만족하면 연결리스트의 삽입 위치가 이동된다.
newnode->next에 current의 값이 대입 된다
 
if문의 조건식은 제일 앞일 경우 실행 시키는 조건식이고
else는 중간이나 마지막에 해당할 경우 실행시키는 명령문이다.
이부분이 노드 삽입하는 영역이다
728x90