1 / 14

C Programming

C Programming. Presented by Do Van Quyen. Linked List. 18 December2011. Contents. Linked list nodes Linked list operations Other linked lists Demo Q&A. B. A. A. C. C. B. A. 1. Linked lists. ListNode. ListNode. Head. ListNode. A linked list is a series of connected nodes

fell
Télécharger la présentation

C Programming

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. C Programming Presented by Do Van Quyen Linked List 18 December2011

  2. Contents • Linked list nodes • Linked list operations • Other linked lists • Demo • Q&A

  3. B A A C C B A 1. Linked lists ListNode ListNode Head ListNode • A linked list is a series of connected nodes • Each node contains at least • A piece of data (any type) • Pointer to the next node in the list • Head: pointer to the first node • The last node points to NULL  In memory 800 712 0 1000 712 800 node data pointer

  4. 1. Linked lists • ListNode: Definition • struct Node { • int data;// data • Node* next;// pointer to next • }; • struct List { • Node* fistNode; • }; • Void List_init(List* list){ • list->firstNode = 0; • };

  5. 2. Linked list operations • Add an element to the list 3 firstNode  newNode 1 2 list • void List_Add(List* list, int data){ • } • Node* newNode= new Node; • newNode->data = data; • newNode->next = list->firstNode; • list->firstNode = newNode;

  6. 2. Linked list operations • Remove the first element node 2 firstNode  list 1 • void List_removeFirst(List* list){ • } • Node* obsoleteNode = list->firstNode; • list->firstNode = list->firstNode->next; • Delete obsoleteNode;

  7. 2. Linked list operations • Insert a new node to the list X  2 3 list newNode 1 • void List_insertAfter(Node* node, int data){ • } • Node* newNode= new Node; • newNode->data = data; • newNode->next = node->next; • node->next = newNode;

  8. 2. Linked list operations • Remove a node from the list 2 X  list 1 • void List_removetAfter(Node* node){ • } • Node* obsoleteNode = node->next; • node->next = obsoleteNode->next; • delete obsoleteNode;

  9. 2. Linked list operations • Delete all elements of the list • void List_deleteAll(list* list){ • Node* node = list->firstNode; • While(node!=0){ • Node*nextNode = node->next; • delete node; • node = nextNode; • } • List->fisrtNode = 0;

  10. 2. Linked list operations • The length of the list • int List_Length(list* list){ • Node* node = list->firstNode; • int i = 0; • While(node!=0){ • i++; • node = node->next; • } • Return i;

  11. 2. Linked list operations • Search an element in the list • Node* List_Search(list* list, int data){ • Node* node = list->firstNode; • While(node!=0){ • if(node->data == data){ • return node; • } • else node = node->next; • } • return node;

  12. 2. Linked list operations • Display the list • Void List_display(list* list){ • Node* node = list->firstNode; • int i = List_Length(list); • Cout <<“\n The length of the list:\t”<<i; • if(list->firstNode == 0) • cout <<“\n The list is empty\r\n”; • else{ • while(node!=0){ • cout<<“\nThe address of the node”<<i<<“\t”<<&node->data; • cout<<“\nNode->data:\t\t”<<node->data; • cout<<“\nNode->next:\t\t”<<node->next<<“\n”; • node = node->next; • i--; • } • cout<<endl; • } • }

  13. prev A next head tail prev A B C next first Other linked list • Doubly-linked lists: Each list node stores both the previous and next nodes in the list. Useful for traversing linked lists in both directions • Circular-linked lists: Last node's next references the first node. Works with or without headers

  14. Thank You

More Related