1 / 11

Linked List Operations

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();

starbuck
Download Presentation

Linked List Operations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked List Operations Dr. David Tsai 2010/4

  2. 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語言實作/陳會安

  3. 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語言實作/陳會安

  4. 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語言實作/陳會安

  5. 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語言實作/陳會安

  6. 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語言實作/陳會安

  7. 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語言實作/陳會安

  8. 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語言實作/陳會安

  9. 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語言實作/陳會安

  10. 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; } } }

  11. 6 5 3 2 4 1 50 = ptr ptr ptr first newnode newnode newnode 完成分別在(1)串列首之前、(2)串列之間、(3)串列之後插入節點

More Related