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

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

Transcript of Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

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

Data StructureLecture-3

Prepared by:Shipra Shukla

Assistant ProfessorKaziranga University

Page 2: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

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

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

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

B C

A

data pointer

node

Page 4: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

Anatomy of a linked list• A linked list consists of:• A sequence of nodes

4

a b c d

Each node contains a valueand 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

Page 5: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

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

Page 6: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Page 7: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

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.

Page 8: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

Linked Liststruct node {int a;struct node *next;

}; struct node *head;

8

a

next

a

next

a

next

a

next

Page 9: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Page 10: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

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 heapsecond = malloc(sizeof(struct node));third = malloc(sizeof(struct node));head->data = 1; // setup first nodehead->next = second; // note: pointer assignment rulesecond->data = 2; // setup second nodesecond->next = third;third->data = 3; // setup third linkthird->next = NULL;// At this point, the linked list referenced by "head"// matches the list in the drawing.return head;}

Page 11: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

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);}

Page 12: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

To be called from main()function as:

node *head;………head = create_list();

Page 13: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

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. Thisalgorithm traverses LIST, applying an operation PROCESS to eachelement 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.

Page 14: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

PRINTING A LINKED LISTPRINT(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.

Page 15: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Page 16: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

Inserting a node in beginning

void insertbegin (int item){P= (node *) malloc (sizeof(node));P->info=item;If(start== null)P->next=null;elseP->next=start;Start=p;}

Page 17: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

Inserting a node at endvoid 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;}}

Page 18: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

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;}

Page 19: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

Deleting a node in beginningprintf("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*/

Page 20: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

Deleting a node at endprintf("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*/

Page 21: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

Deleting a node at locationDeleting after an elementprintf("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*/

Page 22: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Page 23: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Page 24: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Page 25: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Page 26: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Page 27: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Page 28: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Page 29: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Page 30: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

Algorithm: Push in stack

Page 31: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

Algorithm: Pop in stack

Page 32: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

Circular link list insert at beginningnode * insertfirst(node*start){node*pp=(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;}}

Page 33: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

Circular link list insert at lastnode * insertlast(node*start){node*pp=(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);}

Page 34: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

Circular link list delete at beginnode *delete_first(node*start){node*pp=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);}

Page 35: Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.

Circular link list delete at lastnode *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);}