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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
   | #include <stdio.h> #include <stdlib.h> #include <malloc.h> #pragma once
  typedef struct Node { 	int data; 	struct Node* Next; }Node;
  typedef struct LinkedList { 	struct Node* head; // HEAD를 가르키는 Node 포인터 변수 	struct Node* tail; // TAIL을 가르키는 Node 포인터 변수 	int numdata; };
  void LInitialize(LinkedList* plist) { 	plist->tail = NULL; 	plist->head = NULL; }
  void LInsert(LinkedList* plist, int data) { 	Node* nNode = (Node*)malloc(sizeof(Node)); 	nNode->data = data; 	if (plist->tail == NULL) // 첫번째 노드인경우 	{ 		plist->tail = nNode; 		plist->head = nNode; 		nNode->Next = nNode; 		plist->numdata = 0; 	} 	else // 첫번째 노드가 아닌경우 	{ 		nNode->Next = plist->tail->Next; // head 노드를 가르킴 		plist->tail->Next = nNode; // 이전 노드가 새로운 노드를 가르킴 		plist->tail = nNode; // nNode가 tail노드가 되도록 변경 	} 	plist->numdata++;// 노드 개수 추가 }
 
  void LDelete(LinkedList* plist) { 	struct Node* next; // 이전노드를 가르키는  Node 포인터 변수 	struct Node* cur; // 현재 노드를 가르키는 Node 포인터 변수 	printf("\n 데이터 삭제 %d ", plist->head->data);// 삭제되는 노드의 데이터를 출력 	next = plist->head->Next; 	free(plist->head); // 반환 	plist->head = next; 	plist->tail->Next = plist->head; 	plist->numdata--; }
  void LPrint(LinkedList* plist,int num) { 	while (num != 0) 	{ 		printf("%d", plist->head->data); 		plist->head = plist->head->Next; 		num--; 	} }
 
  int main() { 	LinkedList CList; 	LInitialize(&CList); 	LInsert(&CList, 1); 	LInsert(&CList, 2); 	LInsert(&CList, 3); 	LInsert(&CList, 4); 	LInsert(&CList, 5); 	LInsert(&CList, 6); 	LInsert(&CList, 7); 	LInsert(&CList, 8); 	LInsert(&CList, 9); 	LPrint(&CList, CList.numdata); 	LDelete(&CList); }
   |