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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
#include <stdio.h> #include <stdlib.h> //Khai báo kiểu dữ liệu Node struct Node { int data; struct Node * next; }; typedef struct Node NODE; //Hàm tạo node mới NODE * CreateNewNode(int data) { NODE * newNode = (NODE *) malloc (sizeof(NODE)); newNode -> data = data; return newNode; } //Hàm in danh sách void Display(NODE * tail) { NODE * current = tail; if (tail != NULL) { do { current = current -> next; printf(" %d -> ", current -> data); } while (current != tail); } } //Hàm đếm số lượng node int Length(NODE * tail) { NODE * current = tail; int i = 1; if (tail == NULL) { return 0; } else { current = current -> next; while (current != tail) { i++; current = current -> next; } } return i; } //Hàm thêm vào đầu danh sách NODE * InsertAtHead(NODE * tail, int data) { NODE * newNode = CreateNewNode(data); if (tail == NULL) { tail = newNode; newNode -> next = newNode; } else { newNode -> next = tail -> next; tail -> next = newNode; } return tail; } //Hàm thêm vào cuối danh sách NODE * InsertAtEnd(NODE * tail, int data) { return InsertAtHead(tail, data) -> next; } int main() { NODE * cll = NULL; int option, data, location; while (1) { printf("\n====================MENU===================="); printf("\n1. Them nut"); printf("\n2. In danh sach"); printf("\n3. Thoat\n"); printf("Menu chon: "); scanf("%d", &option); if (option == 1) { printf("Nhap du lieu de them vao danh sach: "); scanf("%d", &data); if(data%2==0) { cll = InsertAtHead(cll, data); } else { cll = InsertAtEnd(cll, data); } } else if (option == 2) { Display(cll); } else if (option == 3) { break; } } return 0; } |