國立屏東商業技術學院 資訊工程系 程式設計(二)
具備已排序特性的鏈結串列實作
turnin code c.hw16
due date: June 3, 23:59
以鏈結串列的作業為基礎,實作具備已排序特性的鏈結串列
參考下面的Makefile
main: sortedlinkedlist.o main.c cc main.c sortedlinkedlist.o -o main sortedlinkedlist.o: sortedlinkedlist.c sortedlinkedlist.h cc -c sortedlinkedlist.c clean: rm -f *.*~ *~ *.o main
將宣告置於sortedlinkedlist.h中
struct node { int value; struct node *next; }; typedef struct node Node; void showAllNodes(); Node *createANode(int num); void add(Node *newNode); void removeANode(Node *delNode); Node *search(int v);
main.c的內容如下:
#include <stdio.h> #include <stdlib.h> #include "sortedlinkedlist.h" int main() { char cmd; int num; int quit=0; Node *n; while(!quit) { printf("COMMAND: "); scanf(" %c", &cmd); switch(cmd) { case 'h': printf("\tq : quit\n"); printf("\th : help\n"); printf("\ti : data insertion\n"); printf("\td : data deletion\n"); printf("\ts : show all data\n"); break; case 'i': printf("\tNumber: "); scanf(" %d", &num); n=createANode(num); add(n); break; case 'd': printf("\tNumber: "); scanf(" %d", &num); n=search(num); if(n!=NULL) { removeANode(n); } else { printf("the number does not exist!\n"); } break; case 's': showAllNodes(); break; case 'q': quit=1; break; default: printf("Bad command"); } } return 0; }
請完成sortedlinkedlist.c的實作。以make完成編譯後,執行./main可得到以下的結果:
[23:35 user@ws hw16]$ ./main COMMAND: i Number: 50 COMMAND: i Number: 2 COMMAND: s 2-->50-->NULL COMMAND: i Number: 100 COMMAND: i Number: 80 COMMAND: s 2-->50-->80-->100-->NULL COMMAND: d Number: 80 COMMAND: s 2-->50-->100-->NULL COMMAND: h q : quit h : help i : data insertion d : data deletion s : show all data COMMAND: q [23:35 user@ws hw16]$
注意: