Download presentation
Presentation is loading. Please wait.
Published byJodie Davis Modified over 8 years ago
1
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the structures of variables are changed during the computation, they are called dynamic structures. There are next types of linked lists: Singly Linked List Circular Linked List Doubly-Linked List
2
struct LIST { int dann; LIST *next; }; LIST *head=NULL; LIST *rear=NULL; LIST *ptr=NULL; Singly Linked lists (in C++)
3
Operations List Building void make(int a) { ptr=new LIST; if (!head) head=ptr; else rear->next=ptr; ptr->dann=a; rear=ptr; rear->next=NULL; } // new ptr points to a LIST struct // If the list is empty, the ptr pointer changes the head pointer. // else change rear.next field from NULL to point // to the new node // store data in the new node. // set rear to point to the new node // set rear.next field equal to a null pointer - NULL
4
Operations List Building void make(int a) {ptr=new LIST; if (!head) head=ptr; else rear->next=ptr; ptr->dann=a; rear=ptr; rear->next=NULL; } next ptr head 1 rear NULL next ptr 2 NULL next 3 NULL
5
Traversing the list LIST* ptr = head; while (ptr != NULL) { // do something with *ptr node ptr = ptr->next; } Assign List head to node pointer While node pointer is not NULL Display the value member of the node pointed to by node pointer Assign node pointer to its own next member.
6
Traversing the list void print(void) {ptr=head; while (ptr){ cout dann << " "; ptr=ptr->next; } cout << endl; } 1next 2 4 3 NULL head ptr 1 2 3 4
7
Destroying the list void clear(void) {while (head) { ptr=head->next; delete head; head=ptr; } 1next2 4 3 NULL head ptr head
8
Insertion a node void insert_after(int x, int a) { // Initialize ptr to head of list LIST *ptr=head; LIST *temp; // If the list is empty, do nothing if (!ptr)return; // Skip all nodes whose value is not equal to x while (ptr != NULL && ptr->dann!=x) ptr=ptr->next; if (ptr!=NULL) {// Allocate a new node temp and store data temp=new LIST; temp->dann=a; // Set temp.next field equal to a node is situated after ptr temp->next=ptr->next; // Change ptr.next field to point to the new node temp ptr->next=temp; }
9
Insertion a node void insert_after(int x, int a) {ptr=head; LIST *temp; if (!ptr) return; while (ptr != NULL && ptr->dann!=x) ptr=ptr->next; if (ptr!=NULL) { temp=new LIST; temp->dann=a; temp->next=ptr->next; ptr->next=temp; } 1next2 4 3 NULL head ptr next temp 99
10
Deleting a node void delete_after(int x) { // Initialize ptr to head of list LIST *ptr=head; LIST *temp; // If the list is empty, do nothing if (!ptr)return; // Skip all nodes whose value is not equal to x while (ptr != NULL && ptr->dann!=x) ptr=ptr->next; if (ptr!=NULL) {// Link the next field of ptr node to the node after temp temp=ptr->next; ptr->next=temp->next; // Delete temp delete temp; }
11
Deleting a node void delete_after(int x) { LIST *ptr=head; LIST *temp; if (!ptr)return; while (ptr != NULL && ptr->dann!=x) ptr=ptr->next; if (ptr!=NULL) { temp=ptr->next; ptr->next=temp->next; delete temp; } 1next2 4 3 NULL head ptr temp
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.