CH Gowri Kumar gkumar007@gmail.com 9/21/2018 Linked Lists CH Gowri Kumar gkumar007@gmail.com.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
Linked Lists: deleting...
CSEB324 Data Structures & Algorithms
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Linked List Variations
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Data Structures: A Pseudocode Approach with C
Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Linked Lists. A linear linked list is a collection of objects, called nodes, each of which contains a data item and a pointer to the next node in the.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
CS261 Data Structures Linked Lists - Introduction.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
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.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Chapter 12 – Data Structures
Dynamic Allocation Review Structure and list processing
5.13 Recursion Recursive functions Functions that call themselves
Introduction to Linked Lists
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
Linked lists.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Linked list insertion Head. Linked list insertion Head.
Lists.
Linked List Sudeshna Sarkar.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Introduction to Linked Lists
Lists.
Chapter 16-2 Linked Structures
Circular Buffers, Linked Lists
Object Oriented Programming COP3330 / CGS5409
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Lists, Strings & Multi-dimensional arrays
Linked List Insert After
Review & Lab assignments
Philip Bernhard, PhD Spring 2018
ECE 103 Engineering Programming Chapter 64 Tree Implementation
Linked Lists.
Data Structures & Algorithms
Linked List Improvements
Linked lists.
Pointers, Dynamic Data, and Reference Types
Linked List Insert After
Dynamic Data Structures
Data Structures & Programming
Presentation transcript:

CH Gowri Kumar gkumar007@gmail.com 9/21/2018 Linked Lists CH Gowri Kumar gkumar007@gmail.com

typedef struct node Node; typedef struct node* List; 9/21/2018 struct node { int data; struct node* next; }; typedef struct node Node; typedef struct node* List; List Initialize(); void InsertBegin(List l,int d); void InsertEnd(List l, int d); void Insert(List l, Node* pos,int d); Node* Find(List l,int d); void Delete(List l, int d); This is header based implementation of linked lists. The typedefs help us to reduce the amount of typing. Instead of typing “struct node*” all the time, we can just use Node*; Initiliaze – Creates a dumy header node and returns it. InsertBegin – The element is inserted at the beginning of the list InsertEnd – The element is inserted at the end of the list. Insert – The general routine, inserts the elment after the node “pos”. Find – Return the node, whose next node points to the actual node. The intended use is: Node* p = Find(l,10); p->next->data == 10 If the element is no there in the list, it returns NULL. Delete – deletes the element d from the list. September 21, 2018 http://www.gowrikumar.com

Menu Initialize InsertBegin InsertEnd Insert Find Delete September 21, 2018 http://www.gowrikumar.com

Initialize September 21, 2018 http://www.gowrikumar.com

temp = (Node*)calloc(1,sizeof(Node)); return temp; } 9/21/2018 List Initialize() { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); return temp; } We would be using calloc in our implementation, instead of malloc. He difference between calloc and malloc is that: Malloc – just allocates the memory Calloc – Allocates the memory and also clears it. The initial values would be Data would be 0 and the next would be NULL. If we had used malloc, the code would have been: Node* temp; Temp = (Node8)malloc(sizeof(Node)); Temp->next = NULL; Return temp; The intended use of the Initialize is as follows: List head; Head=Initialize(); September 21, 2018 http://www.gowrikumar.com

temp = (Node*)calloc(1,sizeof(Node)); return temp; } main() { 9/21/2018 head X List Initialize() { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); return temp; } main() { List head; head = Initialize(); } We would be using calloc in our implementation, instead of malloc. He difference between calloc and malloc is that: Malloc – just allocates the memory Calloc – Allocates the memory and also clears it. The initial values would be Data would be 0 and the next would be NULL. If we had used malloc, the code would have been: Node* temp; Temp = (Node8)malloc(sizeof(Node)); Temp->next = NULL; Return temp; The intended use of the Initialize is as follows: List head; Head=Initialize(); September 21, 2018 http://www.gowrikumar.com

InsertBegin September 21, 2018 http://www.gowrikumar.com

1 10 8 4 6 3 2 5 head X September 21, 2018 http://www.gowrikumar.com 9/21/2018 1 10 8 4 6 3 2 5 head X Let’s try inserting a few elements shown above into the list. September 21, 2018 http://www.gowrikumar.com

void InsertBegin(List head,int d) { Node* temp; 9/21/2018 1 10 8 4 6 3 2 5 head X void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = head->next; head->next = temp; } Let’s try inserting a few elements shown above into the list. September 21, 2018 http://www.gowrikumar.com

void InsertBegin(List head,int d) { Node* temp; 9/21/2018 1 10 8 4 6 3 2 5 head X void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = head->next; head->next = temp; } 1 First we create a node temp, using calloc and copy the value into it. Now we need to insert the node into the list. For that first we make temp->next point to whatever head->next is pointing to. Then we make head->next point to temp; Here in this case, head->next is NULL, so you may think that it is not necessary to have the Statement temp->next = head->next; we will see the explanation for it when we insert another node At the beginning of the list. September 21, 2018 http://www.gowrikumar.com

1 10 8 4 6 3 2 5 head X 1 September 21, 2018 http://www.gowrikumar.com 9/21/2018 1 10 8 4 6 3 2 5 head X 1 Now the list looks this September 21, 2018 http://www.gowrikumar.com

void InsertBegin(List head,int d) { Node* temp; 1 10 8 4 6 3 2 5 head X void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = head->next; head->next = temp; } 1 September 21, 2018 http://www.gowrikumar.com

void InsertBegin(List head,int d) { Node* temp; 9/21/2018 1 10 8 4 6 3 2 5 head X void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = head->next; head->next = temp; } 1 The order of operations of the nodes is important. To understand it better, let’s try To interchange the instructions and see what happens. September 21, 2018 http://www.gowrikumar.com

void InsertBegin(List head,int d) { Node* temp; 9/21/2018 1 10 8 4 6 3 2 5 head X void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; head->next = temp; temp->next = head->next; } 1 If we execute head->next = temp first, we lose the remaining of the list. And if we execute the next statement, temp->next would be pointing to itself. September 21, 2018 http://www.gowrikumar.com

void InsertBegin(List head,int d) { Node* temp; 9/21/2018 1 10 8 4 6 3 2 5 head X 1 void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = head->next; head->next = temp; } Now let us insert 10 into the list at the beginning of the list. September 21, 2018 http://www.gowrikumar.com

void InsertBegin(List head,int d) { Node* temp; 9/21/2018 1 10 8 4 6 3 2 5 head X 1 void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = head->next; head->next = temp; } 10 It is similar to the earlier insertion. Here it should make clear why temp->next has to be assigned Head->next; Here we have remaining list to be taken care of (earlier it was an empty list). September 21, 2018 http://www.gowrikumar.com

void InsertBegin(List head,int d) { Node* temp; 9/21/2018 1 10 8 4 6 3 2 5 head X 1 void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; head->next = temp; temp->next = head->next; } 10 As said earlier, the order of pointer manipulations is important. September 21, 2018 http://www.gowrikumar.com

InsertEnd September 21, 2018 http://www.gowrikumar.com

void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; 9/21/2018 1 10 8 4 6 3 2 5 head X 10 1 tail void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; . . . . . . . . } Now let’s insert 8 into the linked-list, but to the end of the list. September 21, 2018 http://www.gowrikumar.com

void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; 9/21/2018 1 10 8 4 6 3 2 5 head X 10 1 tail void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; while(tail->next != NULL) tail = tail->next; . . . . . . . . } First we need to go to the end of the list. For that we initialize tail to point head and Traverse the list using a while loop, with tail = tail->next, as the loop statement. September 21, 2018 http://www.gowrikumar.com

void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; 9/21/2018 1 10 8 4 6 3 2 5 head X 10 1 tail void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; while(tail->next != NULL) tail = tail->next; . . . . . . . . } When the loop terminates, tail is pointing to the last element. Note that we want to point to the last element and not to NULL. September 21, 2018 http://www.gowrikumar.com

void InsertEnd(List head,int d) { . . . . . . . . 9/21/2018 1 10 8 4 6 3 2 5 head tail X 10 1 8 void InsertEnd(List head,int d) { . . . . . . . . temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; tail->next = temp; } A new node is created and the last node’s next pointer is made to point to this new node. Because, calloc takes care of initializing the next to NULL, we don’t have to worry about pointing it to NULL. September 21, 2018 http://www.gowrikumar.com

Insert September 21, 2018 http://www.gowrikumar.com

void Insert(List head,Node* p,int d) { 9/21/2018 1 10 8 4 6 3 2 5 head X 10 1 8 void Insert(List head,Node* p,int d) { temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = p->next; p->next = temp; } Now let’s look at the general version of Insert, where the element is to be inserted after the node. September 21, 2018 http://www.gowrikumar.com

void Insert(List head,Node* p,int d) { 9/21/2018 1 10 8 4 6 3 2 5 head X 10 1 8 4 void Insert(List head,Node* p,int d) { temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = p->next; p->next = temp; } Now let’s look at the general version of Insert, where the element is to be inserted after the node. September 21, 2018 http://www.gowrikumar.com

1 10 8 4 6 3 2 5 head X 10 1 4 8 September 21, 2018 http://www.gowrikumar.com

Find September 21, 2018 http://www.gowrikumar.com

void Find(List l,Node* p,int d) { Node *temp; temp = l; 1 10 8 4 6 3 2 5 head X 10 1 4 8 void Find(List l,Node* p,int d) { Node *temp; temp = l; while(temp->next != NULL) if(temp->next->data == d) return temp; temp = temp->next; } return NULL; September 21, 2018 http://www.gowrikumar.com

void Find(List l,Node* p,int d) { Node *temp; temp = l; 1 10 8 4 6 3 2 5 head temp X 10 1 4 8 void Find(List l,Node* p,int d) { Node *temp; temp = l; while(temp->next != NULL) if(temp->next->data == d) return temp; temp = temp->next; } return NULL; September 21, 2018 http://www.gowrikumar.com

Delete September 21, 2018 http://www.gowrikumar.com

void Delete(List l,Node* p,int d) { Node *temp,*del; temp = Find(l,d); 1 10 8 4 6 3 2 5 head X 10 1 4 8 void Delete(List l,Node* p,int d) { Node *temp,*del; temp = Find(l,d); if(temp != NULL) del = temp->next; temp->next = del->next; free(del); } September 21, 2018 http://www.gowrikumar.com

void Delete(List l,Node* p,int d) { Node *temp,*del; temp = Find(l,d); 1 10 8 4 6 3 2 5 head temp del X 10 1 4 8 void Delete(List l,Node* p,int d) { Node *temp,*del; temp = Find(l,d); if(temp != NULL) del = temp->next; temp->next = del->next; free(del); } September 21, 2018 http://www.gowrikumar.com

10 8 4 6 3 2 5 head X 10 4 8 September 21, 2018 http://www.gowrikumar.com

int main { List l; Node* temp; l = Initialize(); InsertBegin(l,1); InsertEnd(l,8); temp = Find(l,8); Insert(l,temp,4); Delete(l,1); } September 21, 2018 http://www.gowrikumar.com

The End September 21, 2018 http://www.gowrikumar.com