Bağlı Liste Uygulaması
25.03.2010 - 15:02 tarihinde nerg gönderdi
Aşağıdaki örnekte tekli bağlı liste oluşturılmasına ve kullanılmasına ilişkin fonksiyonlar tanımlanıyor.
Kaynak kod:
/********** datelist.h **************************/ typedef struct tagNode { Date date; struct tagNode *pnext; }Node; typedef struct { Node *pstart; Node *pend; size_t size; }*ListHandle; ListHandle openlist(void); void closelist(ListHandle); void push_front(ListHandle handle); void push_back(ListHandle handle); void display_list(ListHandle handle); void pop_front(ListHandle handle); void pop_back(ListHandle handle); void remove_date(const Date *); void clear_list(ListHandle handle); size_t get_size(ListHandle handle); /********** datelist.c **************************/ PRIVATE Node *create_node(void); PRIVATE void free_nodes(Node *p); /*************************************************************************/ PRIVATE void free_nodes(Node *p) { Node *temp; while (p) { temp = p; p = p->pnext; free(temp); } } /*************************************************************************/ PRIVATE Node *create_node(void) { Node *pd = (Node *)malloc(sizeof(Node)); if (!pd) { printf("cannot allocate memory!\n"); exit(EXIT_FAILURE); } return pd; } /*************************************************************************/ PUBLIC ListHandle openlist(void) { ListHandle pd = (ListHandle)malloc(sizeof(*pd)); if (!pd) { printf("cannot allocate memory!\n"); exit(EXIT_FAILURE); } pd->pstart = pd->pend = NULL; pd->size = 0; return pd; } /*************************************************************************/ PUBLIC void closelist(ListHandle handle) { clear_list(handle); free(handle); } /*************************************************************************/ PUBLIC void push_front(ListHandle handle) { Node *pnew = create_node(); set_random(&pnew->date); handle->size++; if (handle->pstart == NULL) { handle->pstart = handle->pend = pnew; pnew->pnext = NULL; return; } pnew->pnext = handle->pstart; handle->pstart = pnew; } /*************************************************************************/ PUBLIC void push_back(ListHandle handle) { Node *pnew = create_node(); set_random(&pnew->date); pnew->pnext = NULL; handle->size++; if (handle->pstart == NULL) { handle->pstart = handle->pend = pnew; return; } handle->pend->pnext = pnew; handle->pend = pnew; } /*************************************************************************/ PUBLIC void display_list(ListHandle handle) { Node *cur = handle->pstart; if (!handle->size) { printf("empty list!\n"); return; } while (cur) { display_date(&cur->date); cur = cur->pnext; } } /*************************************************************************/ PUBLIC void clear_list(ListHandle handle) { free_nodes(handle->pstart); handle->pstart = handle->pend = NULL; } /*************************************************************************/ PUBLIC size_t get_size(ListHandle handle) { return handle->size; } /*************************************************************************/ PUBLIC void pop_front(ListHandle handle) { Node *temp; if (!handle->size) { printf("liste bos!\n"); return; } handle->size--; if (handle->pstart == handle->pend) { free(handle->pstart); handle->pstart = handle->pend = NULL; return; } temp = handle->pstart; handle->pstart = handle->pstart->pnext; free(temp); } /*************************************************************************/ PUBLIC void pop_back(ListHandle handle) { Node *temp, *cur; if (!handle->size) { printf("liste bos!\n"); return; } handle->size--; if (handle->pstart == handle->pend) { free(handle->pstart); handle->pstart = handle->pend = NULL; return; } temp = handle->pend; for (cur = handle->pstart; cur->pnext != handle->pend; cur = cur->pnext) ; cur->pnext = NULL; handle->pend = cur; free(temp); } /*************************************************************************/ void display_menu() { printf("[1] PUSH FRONT\n"); printf("[2] PUSH BACK\n"); printf("[3] DISPLAY LIST\n"); printf("[4] POP FRONT\n"); printf("[5] POP BACK\n"); printf("[6] EMPTY LIST\n"); printf("[7] EXIT\n"); printf("seciminiz : "); } /*************************************************************************/ int get_option() { int option; display_menu(); scanf("%d", &option); if (option < 1 || option > 7) option = 0; return option; } /*************************************************************************/ int main() { int option; ListHandle handle = openlist(); for (;;) { option = get_option(); switch (option) { case 1: push_front(handle); break; case 2: push_back(handle); break; case 3: display_list(handle);break; case 4: pop_front(handle);break; case 5: pop_back(handle);break; case 6: clear_list(handle); break; case 7: goto END; case 8: printf("invalid entry!\n"); } } END: closelist(handle); printf("end of program!\n"); return 0; }
Etiketler:
