Download presentation

Presentation is loading. Please wait.

Published byHanna Thoburn Modified over 5 years ago

1
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University

2
Link List Overview A data structure in which each element is dynamically allocated and in which elements point to each other to define a linear relationship Linked lists Abstract data type (ADT) Basic operations of linked lists Insert, find, delete, print, etc. Variations of linked lists Circular linked lists Doubly linked lists

3
Linked Lists 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 A Head BCA datapointer node

4
Anatomy of a linked list A linked list consists of: A sequence of nodes 4 abcd Each node contains a value and a link (pointer or reference) to some other node The last node contains a null link The list may (or may not) have a header head

5
More terminology A node’s successor is the next node in the sequence The last node has no successor A node’s predecessor is the previous node in the sequence The first node has no predecessor A list’s length is the number of elements in it A list may be empty (contain no elements) 5

7
Cont.. A struct definition that contains variables holding information about something and that has a pointer to a struct of its same type. Every time an element was created, it would create a new element. Each of these individual structs or classes in the list is commonly known as a node or element of the list.

8
Linked List struct node { int a; struct node *next; }; struct node *head; 8 a next a a a

10
Building LinkList struct node* BuildOneTwoThree() { struct node* head = NULL; struct node* second = NULL; struct node* third = NULL; head = malloc(sizeof(struct node)); // allocate 3 nodes in the heap second = malloc(sizeof(struct node)); third = malloc(sizeof(struct node)); head->data = 1; // setup first node head->next = second; // note: pointer assignment rule second->data = 2; // setup second node second->next = third; third->data = 3; // setup third link third->next = NULL; // At this point, the linked list referenced by "head" // matches the list in the drawing. return head; }

11
Example node *create_list() { int k, n; node *p, *head; printf ("\n How many elements to enter?"); scanf("%d", &n); for (k=0; k<n; k++) { if (k == 0) { head = (node *) malloc(sizeof(node)); p = head; } else { p->next = (node *) malloc(sizeof(node)); p = p->next; } scanf("%d %s %d", &p->roll, &p->name, &p->age); } p->next = NULL; return (head); }

12
To be called from main()function as: node *head; ……… head = create_list();

13
TRAVERSING A LINKED LIST LIST be a linked list in memory stored in linear arrays INFO and LINK with START pointing to the first element and NULL indicating the end of LIST. variable PTR which points to the node that is currently being processed LINK[PTR] points to the next node to be processed. PTR := LINK[PTR] Process INFO[PTR], the information at the second node Algorithm: Let LIST be a linked list in memory. This algorithm traverses LIST, applying an operation PROCESS to each element of LIST. 1. Set PTR := START. [Initializes pointer PTR]. 2. Repeat Steps 3 and 4 while PTR # NULL. 3. Apply PROCESS to INFO[PTR]. 4. Set PTR:=LINK[PTR]. [PTR now points to the next node.] [End of Step 2 loop.] 5. Exit.

14
PRINTING A LINKED LIST PRINT(INFO, LINK, START) This procedure prints the information at each node of the list. 1. Set PTR := START. 2. Repeat Steps 3 and 4 while PTR # NULL: 3. Write: INFO[PTR]. 4. Set PTR := LINK[PTR]. [Updates pointer.] [End of Step 2 loop.] 5. Return.

16
Inserting a node in beginning void insertbegin (int item) { P= (node *) malloc (sizeof(node)); P->info=item; If(start== null) P->next=null; else P->next=start; Start=p; }

17
Inserting a node at end void insertend (int item) { Node*p,*loc; P= (node *) malloc (sizeof(node)); P->info=item; P->next=null; If(start==null) { start=p; } else { loc=start; While(loc->next!=null) { loc=loc->next; loc->next=p; }

18
Inserting a node at location void insertpos (int item,int loc) { node*p,*temp; int k; temp=start; for(k=0;k<loc;k++) { temp= temp->next; if(temp==null){ printf(“node is less than one”); return; } p= (node *) malloc (sizeof(node)); p->info=item; p->next=loc->next; loc->next=p; }

19
Deleting a node in beginning printf("Deleting from beginning\n"); if (head != NULL) { p = head; head = head->next; free(p); } /*Empty list: i.e. head==NULL, do nothing*/ print_list(head); /*Display the data in the list*/

20
Deleting a node at end printf("Deleting from end\n"); if (head != NULL) { p = head; while (p->next != NULL) { q = p; p = p->next; } q->next = NULL; free(p); } print_list(head); /*Display the data in the list*/

21
Deleting a node at location Deleting after an element printf("Deleting after\n"); scanf("%d", &a); p = head; while ((p != NULL) && (p->data != a)) p = p->next; if (p != NULL) { q = p->next; if (q != NULL) { p->next = q->next; free(q); } print_list(head); /*Display the data in the list*/

30
Algorithm: Push in stack

31
Algorithm: Pop in stack

32
Circular link list insert at beginning node * insertfirst(node*start) { node*p p=(node*)malloc(sizeof(node)); printf(“enter the no.”); scanf(“%d”,&p->num); if(start==null) { p->next=p; start=p; last=p; } else { p->next=start; start=p; last->next=p; }

33
Circular link list insert at last node * insertlast(node*start) { node*p p=(node*)malloc(sizeof(node)); printf(“enter the no.”); scanf(“%d”,&p->num); if(start==null) { p->next=p; start=p; last=p; } else { last->next=p; last=p; last->next=start; } return(start); }

34
Circular link list delete at begin node *delete_first(node*start) { node*p p=start; if(p==null) { printf(“list empty”); } else { p=start; start=start->next; last->next=p; printf(“element deleted id=%d”, p->num); free(p); } return(start); }

35
Circular link list delete at last node *delete_last(node*start) { node*p,*q; p=start; if(p==null) { printf(“list empty”); } else { while(p->next!=last) { q=p; p=p->next; } printf(“element deleted id=%d”, p->num); q->next=p->next; last=q; } return(start); }

Similar presentations

© 2020 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google