2008. 5. 16. 09:52ㆍETC Programmings
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} node;
typedef struct LinkedList {
int length;
struct Node *fNode;
}linkedList;
node* firstNode;
int length;
void initList(struct LinkedList*);
node* getNodeAt(struct LinkedList*, int);
void add(struct LinkedList*, int);
void addInIndex(struct LinkedList*, int , int);
void erase(struct LinkedList*, int);
void replace(struct LinkedList*, int, int);
void reset(struct LinkedList*);
void display(struct LinkedList*);
int get(struct LinkedList*, int);
int isFull(struct LinkedList*);
int isEmpty(struct LinkedList*);
int checkPosition(struct LinkedList*, int);
void initList(struct LinkedList* list){
if(!isEmpty(list))
reset(list);
list->length = 0;
list->fNode = (node *)malloc(sizeof(node));
}
node* getNodeAt(struct LinkedList* list, int index){
int i;
node* current = list->fNode;
for(i = 0 ; i < index ; i++)
{
current = current->next;
}
return current;
}
void add(struct LinkedList* list, int entryData){
node* newNode = (node* )malloc(sizeof(node));
newNode->data = entryData;
if(list->length == 0){
list->fNode = newNode;
}
else {
getNodeAt(list, list->length - 1)->next = newNode;
}
list->length +=1;
}
void addInIndex(struct LinkedList* list, int entryData, int position){
node* oldNode;
node* newNode = (node* )malloc(sizeof(node));
newNode->data = entryData;
if(checkPosition(list, position)) {
if (position == list->length)
add(list, entryData);
else {
oldNode = getNodeAt(list, position);
getNodeAt(list, position - 1)->next = newNode;
newNode->next = oldNode;
}
list->length += 1;
}
}
void erase(struct LinkedList* list, int position){
node* tempNode = getNodeAt(list, position);
if(checkPosition(list, position)) {
if(position == 0){
tempNode = tempNode->next;
free(list->fNode);
list->fNode = tempNode;
}
else if(position != 0 && position == list->length){
getNodeAt(list, position - 1)->next = NULL;
free(tempNode);
}
else {
getNodeAt(list, position - 1)->next = getNodeAt(list, position + 1);
free(tempNode);
}
list->length -= 1;
}
}
void replace(struct LinkedList* list, int changedData, int position){
if(checkPosition(list, position))
getNodeAt(list, position)->data = changedData;
}
void display(struct LinkedList* list){
if(!isEmpty(list)){
int i;
for(i = 0 ; i < list->length; i++) {
printf("[ %d ] ", getNodeAt(list, i)->data);
}
}
printf("\n");
}
void reset(struct LinkedList* list){
int i;
for(i = list->length - 1 ; i >= 0; i--) {
free(getNodeAt(list, i));
}
}
int get(struct LinkedList* list, int position){
if(checkPosition(list, position))
return getNodeAt(list, position)->data;
else
return 0;
}
int isEmpty(struct LinkedList* list){
return list->length == 0;
}
int checkPosition(struct LinkedList* list, int position){
return position >= 0 && position < list->length;
}
void main(void){
linkedList* p_listOne;
linkedList* p_listTwo;
p_listOne = (linkedList*) malloc(sizeof(linkedList));
p_listTwo = (linkedList*) malloc(sizeof(linkedList));
initList(p_listOne);
initList(p_listTwo);
add(p_listOne, 100);
add(p_listTwo, 200);
add(p_listOne, 100);
add(p_listTwo, 200);
add(p_listOne, 100);
add(p_listTwo, 200);
add(p_listOne, 100);
add(p_listTwo, 200);
display(p_listOne);
display(p_listTwo);
}
아주~~ 재미있었습니다. free() 라는 함수의 중요성도 깨달았고 -_- ㅋㅋㅋㅋ 포인터의 개념도 더 확실해 졌고.. 잇힝.. 아무튼 너무 재미있어효 ^^ ㅋㅋ