#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node* link;
}Node;
typedef struct {
Node* header;
int numOfData;
}LinkedList;
LinkedList* createLinkedList() {
LinkedList* L = (LinkedList*)malloc(sizeof(LinkedList));
L->header = NULL;
L->numOfData = 0;
return L;
}
void insertNode(LinkedList* L,Node* pre, int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
if (pre == NULL) {
newNode->link = L->header;
L->header = newNode;
}
else {
newNode->link = pre->link;
pre->link = newNode;
}
L->numOfData++;
return 1;
}
void removeNode(LinkedList* L, Node* pre) {
Node* temp;
if (L->numOfData == 0) {
return 0;
}
if (pre == NULL) {
temp = L->header;
L->header = temp->link;
}
else {
temp = pre->link;
pre->link = temp->link;
}
free(temp);
L->numOfData--;
return 1;
}
void printList(LinkedList* L) {
Node* temp;
temp = L->header;
while (temp!=NULL){
printf("%d\t", temp->data);
temp = temp->link;
}
printf("노드의 개수:%d\n", L->numOfData);
}
int main() {
LinkedList* L;
L = createLinkedList();
insertNode(L, NULL, 1);
printList(L);
insertNode(L, NULL, 2);
printList(L);
removeNode(L, NULL);
printList(L);
return 0;
}