A sentimental robot

Single LinkedList Exercise3 본문

Data Structure

Single LinkedList Exercise3

GOD03219 2018. 1. 3. 13:50

#include <stdio.h>
#include<stdlib.h>
#define ASSERT(exp) if(!(exp)){puts("Err\n");}
struct Node
{
 int data;
 struct Node* next;
};
int Insert(struct Node* root, int position)
{

static int count=0;

if(count < position ) return 0;

count ++;

 

 struct Node* cur = root;
 struct Node * new1 = (struct Node*)malloc(sizeof(Node));
 new1->next=NULL;


 for(int i=0; i<position ;i++){
  cur=cur->next;


 }
 if(root->next==NULL){     // 처음 노드와 더미노드 연결
  root->next = new1;
 }else{

  new1->next = cur->next;
  cur->next = new1;
 }
 return 1;
}
int Delete(struct Node* root, int position)
{
 struct Node *del;
 struct Node *cur=root;


 if(cur->next==NULL)  return 0;
 for(int i=0; i<position ;i++){
  cur=cur->next;
 }
 del = cur->next;
 cur->next = del->next;
 del->next = NULL;
 free(del);


 return 1;
}
int SetData(struct Node* root, int position, int data)
{
 struct Node *cur = root->next;
 if(cur==NULL)  return 0;
 for(int i=0; i<position ;i++){

  cur=cur->next;

 }
 cur->data = data;
 return 1;

}
int GetData(struct Node* root, int position, int* data)
{
 struct Node*cur = root->next;
 if(cur==NULL) return 0;
 for(int i=0 ;i<position; i++)
 {
  if(cur->next==NULL)  return 0;
  cur=cur->next;
 }
 *data=cur->data;
 // printf("%d",cur->data);

 return 1;
}
void main(void)
{
 Node* root = (struct Node*)malloc(sizeof(struct Node));
 Node *cur;
 root->next = NULL;
 int r,n;

 

 //0번위치에 노드 10개 삽입

 for(int i=0; i<10 ;i++)

 {
  r = Insert(root, 0);ASSERT(r);
  r = SetData(root,0,i+1);ASSERT(r);

 

 }
 cur = root->next;

 while(cur != NULL){


  printf("cur->data = %d\n",cur->data);
  cur = cur ->next;
 }

 

 //삽입확인 
 for(int i=0 ;i<10; i++)

 {
  r = GetData(root,i,&n);ASSERT(r);
  printf("n = %d\n",n);
  ASSERT(n == 10 - i);

 }

 

 //모두삭제 
 for(int i=0 ;i<10; i++)
 {

  r = Delete(root, 0);ASSERT(r);
 }
 cur = root->next;

 while(cur != NULL){
  printf("cur->data = %d\n",cur->data);
  cur = cur ->next;
 }

 puts("모두 삭제 끝");


 // 마지막위치에 10개삽입 
 for(int i=0 ;i<10; i++)
 {
  r = Insert(root,i);ASSERT(r);
  r = SetData(root,i,i+1);ASSERT(r);
 }
 cur = root->next;

 while(cur != NULL){
  printf("cur->data = %d\n",cur->data);
  cur = cur ->next;
 }
 //짝수번째모두삭제

 for(int i=10-1 ;i>=0; i-=2)
 {
  r = Delete(root, i);ASSERT(r);

 }
 cur = root->next;

 while(cur != NULL){
  printf("cur->data = %d\n",cur->data);
  cur = cur ->next;
 }
 //짝수번째다시삽입

 for(int i=1 ;i<10; i+=2)
 {
  r = Insert(root, i);ASSERT(r);
  r = SetData(root,i,i+1);ASSERT(r);

 }
 cur = root->next;

 while(cur != NULL){
  printf("cur->data = %d\n",cur->data);
  cur = cur ->next;
 }


// 복원확인 
 
 for(int i=0 ;i<10; i++)
 {
  r = GetData(root,i,&n);ASSERT(r);
  ASSERT(n==i+1);
 }


 //에러확인 
 for(int i=10 ;i<20; i++)
 {
  r = GetData(root,i,&n);ASSERT(!r);
 }
 
 //역순으로삭제
 for(int i=10-1 ;i>=0; i--)
 {

  r = Delete(root, i);ASSERT(r);
 }
 cur = root->next;

 while(cur != NULL){
  printf("역순삭제= %d\n",cur->data);
  cur = cur ->next;
 }
 ASSERT(root->next == NULL);

 free(root);

 root = NULL;  // 좋은습관
 
}

'Data Structure' 카테고리의 다른 글

Bubble sort  (0) 2018.01.03
Double LinkedList  (0) 2018.01.03
Map에 대하여..  (0) 2018.01.03
Stack  (0) 2018.01.03
Linear Queue  (0) 2018.01.03