140 likes | 271 Vues
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
E N D
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 • 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
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; • };
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;
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;
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;
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;
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;
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;
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;
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; • } • }
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