LINKED LIST.

Slides:



Advertisements
Similar presentations
Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
Advertisements

Stacks, Queues, and Linked Lists
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Linked List Variations
CSCI2100B Linked List Jeffrey
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Queues : Queue is a first in first out data structure. Queue concept is used in printers, Operating systems for process scheduling. Queue concept is used.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
UNIT 1 Data Structures Using C Linked List By Rohit Khokher Department of Computer Science, Vidya College of Engineering, Meerut, India.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
Algorithms and Data Structures
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
CIRCULAR L INKED L IST Insert as a first node Insert as a last node Delete first node Delete last node Insert after a node Insert before a node Search.
Programming Linked Lists. Collections Store collection of data  Online store - Items  University – Students  Library – books Until now we used arrays.
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Linked List. Insert a node at the beginning #include struct node { int data; struct node* next; }; struct node* head; void Insert(int x) { node *temp.
الطابور QUEUE (abstract data type) واحد من هياكل البيانات الخطية الشائعة الاستخدام داخل البرامج. يحتوي علي عناصر من نفس النوع. من أنواع البيانات الخطية.
 Structures typedef struct student{ char name[30]; long id; int grades[8]; } Student_type, *pStudent_type;
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
Linked List.
Unit-1 Data Structures – Introduction to Data Structures, abstract data types, Linear list – singly linked list implementation, insertion, deletion and.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp ]
Linked List :: Basic Concepts
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Linking in double direction
Linked List.
Doubly Linked List Review - We are writing this code
CSCE 3110 Data Structures & Algorithm Analysis
UNIT-3 LINKED LIST.
Problems with dynamic arrays
Linked list.
Linked Lists Damian Gordon.
Linked List Variations
EEL 4854 IT Data Structures Linked Lists
ENERGY 211 / CME 211 Lecture 12 October 17, 2008.
Queue data structure.
CHAPTER 12 LINKED LIST SRM-MCA.
Stacks.
Stack and Queue APURBO DATTA.
Sequences 9/18/ :20 PM C201: Linked List.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
List Objectives Describe a list
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Circularly Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Linked List Insert as a first node Insert as a last node
Programming Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Doubly Linked Lists or Two-way Linked Lists
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Data structure types. Linear and Non-Linear Data Structures: Linked lists, stacks and queues.
Linked List.
LINKED LISTS.
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
General List.
Linked Lists.
Linked Lists.
Presentation transcript:

LINKED LIST

1 2 Insert node at the beginning struct node { int data; struct node *next; }*head,*var,*trav; head=NULL; head Insert node at the beginning 1 var=(struct node *)malloc(sizeof (struct node)); var->data=value; if(head==NULL) { head=var; head->next=NULL; } else var->next=head; 10 var 1000 10 head 1000 2 NULL

5 3 4 Insert node at the beginning 10 head 1000 20 head 2000 5 NULL 1000 10 1000 NULL Insert node at the beginning 3 var=(struct node *)malloc(sizeof (struct node)); var->data=value; if(head==NULL) { head=var; head->next=NULL; } else var->next=head; 20 var 2000 4 20 var 2000 1000

6 1 2 3 4 4 5 Insert node at the End 20 head 2000 1000 10 1000 NULL 20 3000 30 3000 NULL 6 20 temp 2000 Insert node at the End 1 1000 struct node *temp; temp=head; var=(struct node *)malloc(sizeof (struct node)); var->data=value; if(head==NULL) { head=var; head->next=NULL; } else while(temp->next!=NULL) temp=temp->next; var->next=NULL; temp->next=var; 30 var 3000 2 temp 3 10 1000 NULL 30 var 3000 4 NULL 4 temp 5 10 1000 3000

1 2 3 5 6 Insert node at the Middle 4 head 20 2000 1000 10 1000 3000 NULL Insert node at the Middle var 1 struct node *var2,*temp; var=(struct node *)malloc(sizeof (struct node)); var->data=value; temp=head; if(head==NULL) { head=var; head->next=NULL; } else while(temp->data!=loc) temp=temp->next; var2=temp->next; (3000) temp->next=var; var->next=var2; 40 4000 temp 2 20 2000 1000 10 temp 3 10 1000 3000 temp 4 var 10 1000 4000 5 40 4000 3000 6

Delete node from end head 20 2000 1000 10 1000 4000 40 4000 3000 30 NULL Delete node from end struct node *temp; temp=head; while(temp->next != NULL) { var=temp; //2000, 1000,4000(FINAL VALUE) temp=temp->next;//finally 3000 } if(temp ==head) head=temp->next; free(temp); return 0; printf("data deleted from list is %d",temp->data); var->next=NULL; free(temp); // free(3000) 40 4000 NULL

Delete node from Middle head 20 2000 1000 10 1000 4000 40 4000 NULL Delete node from Middle struct node *temp,*var; temp=head; while(temp!=NULL) { if(temp->data == value) if(temp==head) head=temp->next; free(temp); return 0; } else var->next=temp->next; else { var=temp; temp=temp->next; } }// END OF WHILE printf("data deleted from list is %d",value);

Display the content of the linked list head 20 2000 1000 10 1000 4000 40 4000 NULL Display the content of the linked list trav=head; if(trav==NULL) { printf("\nList is Empty"); } else printf("\nElements in the List: "); while(trav!=NULL) printf(" -> %d ",trav->data); trav=trav->next; printf("\n");

Doubly Linked List Each node of the list contain two Links – one to the previous node and other to the next node. The previous link of the first node and the next link of the last node points to NULL.  We don’t need to keep track of the previous node for traversal or no need of traversing the whole list for finding the previous node. More pointers needs to be handled and more links need to updated.

Implementation of Doubly Linked List in C prev n next node struct node { struct node *prev; int n; struct node *next; } *h, *temp, *temp1 h = NULL To create an empty node node NULL n NULL

Insert at beginning temp temp 10 20 1000 2000 h temp h temp1 10 20 10 if (h == NULL) { int data;   temp =(struct node *)malloc(1*sizeof(struct node)); temp->prev = NULL; temp->next = NULL; printf("\n Enter value to node : "); scanf("%d", &data); (10) temp->n = data; h = temp; temp1 = h; } else { int data;   temp =(struct node *)malloc(1*sizeof(struct node)); temp->prev = NULL; temp->next = NULL; printf("\n Enter value to node : "); scanf("%d", &data); temp->n = data; temp->next = h; h->prev = temp; h = temp; } temp temp NULL 10 NULL NULL 20 NULL 1000 2000 h temp h temp1 NULL 10 NULL NULL 20 1000 2000 10 NULL NULL 10 NULL 1000 2000 1000 1000

Insert at the end h h = temp; 20 10 2000 1000 temp temp1 temp1 temp 30 NULL 20 1000 2000 10 NULL Insert at the end 2000 1000 if (h == NULL) { int data; temp =(struct node *)malloc(1*sizeof(struct node)); temp->prev = NULL; temp->next = NULL; printf("\n Enter value to node : "); scanf("%d", &data); temp->n = data; h = temp; temp1 = h; } else { int data; temp =(struct node *)malloc(1*sizeof(struct node)); temp->prev = NULL; temp->next = NULL; printf("\n Enter value to node : "); scanf("%d", &data); temp->n = data; temp1->next = temp; temp->prev = temp1; temp1 = temp; } temp temp1 temp1 temp NULL 30 NULL 2000 10 NULL 2000 10 3000 1000 30 NULL 3000 1000 1000 3000 temp1 = h; (first node [10])

After inserting the node at the end 2000 10 3000 1000 30 NULL 20

Deleting the node in the Doubly Linked List int i = 1, pos;   printf("\n Enter position to be deleted : "); scanf("%d", &pos); temp2 = h; if (h == NULL) { printf("\n Error : Empty list no elements to delete"); return; } else while (i < pos) temp2 = temp2->next; i++; if (i == 1) if (temp2->next == NULL) printf("Node deleted from list"); free(temp2); temp2 = h = NULL; if (temp2->next == NULL) { temp2->prev->next = NULL; free(temp2); printf("Node deleted from list"); return; } temp2->next->prev = temp2->prev; if (i != 1) temp2->prev->next = temp2->next; if (i == 1) h = temp2->next; printf("\n Node deleted"); 2000 10 3000 1000 30 NULL 20

Display from beginning To search an item temp2 = h;   if (temp2 == NULL) { printf("List empty to display \n"); return; } printf("\n Linked list elements from begining : "); while (temp2->next != NULL) printf(" %d ", temp2->n); temp2 = temp2->next; int data, count = 0; temp2 = h;   if (temp2 == NULL) { printf("\n Error : List empty to search for data"); return; } printf("\n Enter value to search : "); scanf("%d", &data); while (temp2 != NULL) if (temp2->n == data) printf("\n Data found in %d position",count + 1); else temp2 = temp2->next; count++; printf("\n Error : %d not found in list", data);

Circularly Linked List More complicated linked data structure. Elements can be placed anywhere The last node points back to the first node

Insert at the Beginning x y struct node { int data; struct node *link; }; struct node *head = NULL, *x, *y, *z; Creating node head x = (struct node*)malloc(sizeof(struct node)); printf("\n Enter the data:"); scanf("%d", &x->data); //10 x->link = x; head = x; 10 1000 1000 Insert at the Beginning x y x = head; y = (struct node*)malloc(sizeof(struct node)); printf("\n Enter the data:"); scanf("%d", &y->data); while (x->link != head) { x = x->link; } x->link = y; y->link = head; head = y; 10 1000 20 1000 2000 x y 10 2000 20 1000 1000 2000

Delete node at the start x head = y; head 10 2000 20 1000 2000 1000 Delete node at the start if (head == NULL) printf("\n List is empty"); else { x = head; y = head; while (x->link != head) x = x->link; } head = y->link; x->link = head; free(y); X=2000 Y=2000 While(1000 !=2000) { X=1000; } Ist iteration While(2000 !=2000) { } 2nd iteration x head=1000 x->link = 1000 free(2000) 10 1000 1000

head Display node details 10 2000 20 1000 2000 1000 if (head == NULL) printf("\n List is empty"); else { x = head; while (x->link != head) printf("%d->", x->data); x = x->link; } printf("%d", x->data);