일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- mac terminal command
- docker example
- #1차원배열
- 토큰경제
- #성적관리프로그램
- html charset
- html youtube
- 하이퍼레저패브릭
- html plug-in
- #2차원배열
- hyperledger transaction
- border-box
- html multimedia
- html code
- html object
- #CallByAddress
- #JAVASCRIPT
- relative path
- git flow
- #C++ 연산자함수오버로딩
- html video
- #binary
- #bubbleSort
- #자바상속#자바이즈어#is~a
- #다차원포인터
- html id
- #3차원배열
- html5 new tag
- #C++ has~a
- #android activity
- Today
- Total
A sentimental robot
Single LinkedList Exercise3 본문
#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 |