110 likes | 285 Views
Linked List Operations. Dr. David Tsai 2010/4. struct Node { int data; struct Node *next; }; typedef struct Node LNode; typedef LNode *List; List first = NULL; extern void creatList(int len, int *array); extern int isListEmpty();
E N D
Linked List Operations Dr. David Tsai 2010/4
struct Node { int data; struct Node *next; }; typedef struct Node LNode; typedef LNode *List; List first = NULL; extern void creatList(int len, int *array); extern int isListEmpty(); extern void printList(); extern List searchNode(int d); extern int deleteNode(List ptr); extern void insertNode(List ptr, int d); 修改自教科書:資料結構理論與實務以C語言實作/陳會安
struct Node { int data; struct Node *next; }; typedef struct Node LNode; typedef LNode *List; List first = NULL; Typedef struct Node { int data; struct Node *next; }Lnode; typedef LNode *List; List first = NULL; 修改自教科書:資料結構理論與實務以C語言實作/陳會安
void createList (int len, int*array) { List newnode; for (int i = 0; i < len; i++) { newnode = (List) malloc (sizeof(LNode)); newnode->data = array[i]; newnode->next = first; first = newnode; } } 修改自教科書:資料結構理論與實務以C語言實作/陳會安
int isListEmpty () { if (first == NULL) return 1; else return 0; } void printList () { List current = first; while (current ! = NULL) { printf ([%d] , current->data); current = current->next; } printf (﹨n ); } 修改自教科書:資料結構理論與實務以C語言實作/陳會安
int isListEmpty () { if (first == NULL) return 1; else return 0; } void printList () { if (isListEmpty()) { printf(“The Linked List is Empty\n”); return 0; } List current = first; while (current ! = NULL) { printf ([%d] , current->data); current = current->next; } printf (﹨n ); } 修改自教科書:資料結構理論與實務以C語言實作/陳會安
List searchNode (int d) { if (!isListEmpty()) { List current = first; while (current ! = NULL) { if (current->data == d) return current; current = current->next; } return NULL; } else { printf(“The Linked List is Empty\n”); return 0; } } 修改自教科書:資料結構理論與實務以C語言實作/陳會安
int deleteNode (List ptr) { List current = first; int value = ptr->data; if (isListEmpty() ) { return -1; } if (ptr==first∥ptr==NULL) { first = first->next; } else { while (current->next!=ptr){ current = current->next; } if (ptr->next == NULL){ current->next = NULL; } else { current->next = ptr->next; } } free (ptr); return value; } 修改自教科書:資料結構理論與實務以C語言實作/陳會安
void insertNode (List ptr, int d) { List newnode; newnode = (List) malloc (sizeof(LNode)); newnode->data = d; newnode->next = NULL; if (ptr == NULL) { newnode->next = first; first = newnode; } else { if (ptr->next == NULL) { ptr->next = newnode; } else { newnode->next=ptr->next; ptr->next = newnode; } } } { newnode->next=ptr->next; ptr->next = newnode; } 修改自教科書:資料結構理論與實務以C語言實作/陳會安
int main() { int temp; int data[6]={ 1, 2, 3, 4, 5, 6 }; List ptr; createList(6, data); printList(); temp = 0; insertNode(NULL, 50); printList(); while ( temp != -1 ) { scanf("%d", &temp); if ( temp != -1 ) { ptr = searchNode(temp); if ( ptr != NULL ) scanf("%d", &temp); insertNode(ptr, temp); printf("插入後串列: "); printList(); } } system("PAUSE"); return 0; } List searchNode (int d) { if (!isListEmpty()) { List current = first; while (current ! = NULL) { if (current->data == d) return current; current = current->next; } return NULL; } else { printf(“The Linked List is Empty\n”); return 0; } } void insertNode (List ptr, int d) { List newnode; newnode = (List) malloc (sizeof(LNode)); newnode->data = d; if (ptr == NULL) { newnode->next = first; first = newnode; } else { if (ptr->next == NULL) { ptr->next = newnode; newnode->next = NULL; } else { newnode->next=ptr->next; ptr->next = newnode; } } }
6 5 3 2 4 1 50 = ptr ptr ptr first newnode newnode newnode 完成分別在(1)串列首之前、(2)串列之間、(3)串列之後插入節點