Linear using Linked organization

() Unit VI: Linear data structures using linked organization

• Concept of linked organization, singly linked list, doubly linked list, circular linked list.

• Linked list as an ADT.

• Representation and manipulations of polynomials using linked lists.

• Comparison of a sequential and linked memory organization.

• Concept of Generalized Linked List.

• Representation polynomial using GLL. List Overview

• What is a list and its ADT

• Array as an list

• Linked List

• Why linked list ?

– Limitations of array

• Linked lists

(ADT)

– Types of linked list

• Basic operations of linked list and their implementations ::

– Insert, fnd, delete, print, etc.

• Variations of linked lists

– Circular linked lists

– Doubly linked lists The List ADT

• A sequence of zero or more elements

A1, A2, A3, … AN

• N: length of the list

• A1: frst element

• AN: last element

• Ai: position i • If N=0, then empty list • Linearly ordered

– Ai precedes Ai+1

– Ai follows Ai-1 List ADT :: Operations

• printList: print the list • makeEmpty: create an empty list • fnd: locate the position of an object in a list – list: 34,12, 52, 16, 12 – fnd(52)  3 • insert: insert an object to a list – insert(x,3)  34, 12, 52, x, 16, 12 • remove: delete an element from the list – remove(52)  34, 12, x, 16, 12 • Find Kth: retrieve the element at a certain position Implementation of an ADT

• Two standard implementations for the list ADT

– Array-based

– Linked list Overall operations on the list:

Insertion • Create – Insert at frst • Insertion – Insert at last • Deletion – Insert at particular position • Searching • Display • Sorting Deletion • Reverse – Deletion at Front • Split – Deletion at last – • Merge Deletion a particular element Array Implementation

• Elements are stored in contiguous array positions Array Implementation...

• Requires an estimate of the maximum size of the list

• printList and fnd: linear

• Find Kth: constant

• insert and delete: slow

– e.g. insert at position 0 (making a new element)

• requires frst pushing the entire array down one spot to make room

– e.g. delete at position 0

• requires shifting all the elements in the list up one

– On average, half of the lists needs to be moved for either operation Why Linked list ?

• Limitations of array

– Dynamic vs. static data structure

• unpredictable storage requirement, wastage of memory

– Operations on array vs. linked list

• Extensive manipulation of data make it slow

• Hard to implement nonlinear data structure using array. Linked Lists

A B C 

Head

• Linked list is ordered list type. node • A linked list is a series of connected nodes A • Each node contains at least

– A piece of data (any type) data pointer

– Pointer /link to the next node

in the list

• Head: pointer to the frst node

• The last node links points to NULL Pointer Implementation (Linked List)

• Ensure that the list is not stored contiguously – use a linked list – a series of structures that are not necessarily adjacent in memory

. Each node contains the element and a pointer to a structure containing its successor ✔the last cell’s next link points to NULL . Compared to the array implementation, the pointer implementation uses only as much space as is needed for the elements currently on the list  but requires space for the pointers in each cell A Simple Linked List C implementation

• Declare Node structure for the nodes – data: int/ char/ foating -type – next: a pointer to the next node in the list

typedef strcuture Node { int data; // data Node* next; // pointer to next };

Node 10

data next Pointer-Based Linked Lists

Figure :: A head pointer to a list Terminology

• linked list a list consisting of items in which each item knows the location of the next item.

• Node an item in a linked list.

• Each node contains a piece of list data and the location of the next node (item).

• link (of a node) the location of the next node.

• Head node frst node in a linked list

• Head pointer points to the head node Basic Structure Pseudo code

• • Problems: Find last node in list • Create an empty list. P=head; head = NULL; While (p->next !=NULL) p= p->next; • Determine if a list is empty Find an item (x) in a list p = head while( p->next != NULL && if (head == NULL) p->info != x) { // list is empty access p->info p = p->link } Create void main()

{ Node *p,* head =NULL;

int i; /* // multiple nodes printf(“\n\t How many elements” ); clrscr(); scanf( “%d”,&n); // One node creation p=head; for(i=0;inext=(Node *) malloc (sizeof(Node)); scanf(“%d”,head->data); p=p->next; head->next=NULL; scanf(“%d”,p->data); } printf(“%d”,head->data); getch(); } Preliminaries

Head

Head

Head

Figure a) A linked list of integers; b) insertion; c) deletion Displaying the Contents of a Linked List

Head Head

The effect of the assignment cur = cur->next Displaying the Contents of a Linked List

Void display(Node *HeadD) Recursive { void printR(Node *Head while(HeadD!=NULL) { { if(Head!=NULL) print HeadD; { HeadD = HeadD→next; printf(“%d”, head→ data) } printR(head->next) } } }

Insertion

• Insertin at start Insertion at MIDDLE 1. t  next = start 1. Enter info of the node after which 2. start = t new node to be inserted Return start 2. Read x • Insertion at LAST 3. p = start 1. p = start 4. Repeat step 5 until p info < > x 2. Repeat step 3 until p  next 5. p = p  next NULL 6. t next = p  next 3. p = p next 4. 5. p  next = 7. p  next = t getnode() 8. Return 6. Return start Void insert_E(Node *Headt) { Node *temp = allocate address(); while(Headt->next!=NULL) { Headt = Headt->next; } Headt ->next = temp; } Node *insert_S(Node *Headt) { Node *temp = allocate address(); temp->next =Headt; Headt = temp; Return Headt; } Void insert_Betw(Node *Head) { int t; Node *Headt=Head; Node *temp = allocate address(); printf(“Enter data after which we want to insert a Node”); scanf(“%d”, &t);

If (Headt == NULL) Headt = temp; return Headt;

while(Headt ->data !=t) { Headt = Headt->next; }

Temp->next = headt->next; Headt->next = temp; return Head; } Deletion

• Delete frst node • Delete at pos 1. x = start 1. Enter th e info of node to be 2. start = start next deleted 3. free(x) 2. Read n 3. p = start • Delete last node 4. c = start 1. p = start 5. while (c  info < > NULL) c = start 2. while (c next < > NULL) { p = c p = c c = c next } c = c next 6. p next = c next 3. p  next = c next 7. free ( c ) 4. free ( c) 8. Return start 5. return start Deleting a Specifed Node from a Linked List

Figure :: Deleting a node from a linked list

Figure :: Deleting the first node Deleting a Specifed Node from a Linked List

• Deleting an interior node prev->next=cur->next; • Deleting the frst node head=head->next; • Return deleted node to system cur->next = NULL; delete cur; cur=NULL; Node *reverse(Node * start) { Node *current ,*temp, *result; temp=NULL; result=NULL; current =start; Reverse The if(start->next==NULL) return start; Singly Link List while(current!=NULL) { temp = current->next; current->next = result; result= current; current = temp; } start = result; return start; } Display List in reverse order

void printR(Node *head) { if(head != NULL) { printR(head->next); printf("%d ",head->data); } } Assignment 9

Create a singly linked list with options: Insert (at front, at end, in the middle), Delete (at front, at end, in the middle), Display, Display Reverse, Revert the SLL without using additional data structure. Bubble sort to sort a linked list

n=length(head) for(i = 1; i < n; i++) { p1 = head; p2 = head->next; p3 = p2->next;

for(j = 1; j <= (n - i); j++) { if(p2->value < p3->value) { p2->next = p3->next; p3->next = p2; p1->next = p3; p1 = p3; p3 = p2->next; } else { p1 = p2; p2 = p3; p3 = p3->next; } } }

Sorting Node * sortlist(N ode *start) { Node *index 1, Index 2; int temp; index1= start; for(; new1->next != NULL; index1= index1->next) { for(index 2 = index1->next; index2 != NULL; index2= index2->next) { if(index1->data > index2->data) { temp = index1->data; index1->data = index2->data; index1->data = temp; } } }} Merging two linked list

Node *Merge (Node * head1, Node *head2 , Node * head3) Node * h1, *h2, h3 { // create third list frst node head3= (Node *) malloc(sizeof (Node)) head3- >info =h1->info head1=h1->next head3->next =NULL h3=head3 }

Merging continued …

• Case 1

– copy all the remaining node of list one to list 3 • Case 2

– Copy the all the remaining element of list two to list three • Last

– Return header of list 3 Variations of Linked Lists

• Circular linked lists • – The last node points to the frst node of the list A B C

Head

How do we know when we have fnished traversing the list? (Tip: check if the pointer of the current node is equal to the head.) Variations of Linked Lists

• Doubly linked lists – Each node points to not only successor but the predecessor – There are two NULL: at the frst and last nodes in the list – Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists – backwards

 A B C 

Head Array versus Linked Lists

• Linked lists are more complex to code and manage than arrays, but they have some distinct advantages.

– Dynamic: a linked list can easily grow and shrink in size.

• We don’t need to know how many nodes will be in the list. They are created in memory as needed.

• In contrast, the size of a C++ array is fxed at compilation time.

– Easy and fast insertions and deletions

• To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements.

• With a linked list, no need to move other nodes. Only need to reset some pointers. Circular Linked List Circular Linked Lists

• In linear linked lists if a list is traversed (all the elements visited) an external pointer to the list must be preserved in order to be able to reference the list again.

• Circular linked lists can be used to help the traverse the same list again and again if needed.

• A circular list is very similar to the linear list where in the circular list the pointer of the last node points not NULL but the frst node. Linked Lists

A Linear Linked List Circular Linked Lists Application of Circular Linked List

1. A time sharing problem solved by the operating system. • In this the operating system must maintain a list of present users • Must alternately allow each user to use a small slice of CPU time, one user at a time. • The operating system will pick a user, let him/her use a small amount of CPU time • And then move on to the next user, etc. 2. Circular linked list is the basic idea of round robin scheduling algorithm. A circular linked list can be effectively used to create a queue (FIFO) or a deque (efficient insert and remove from front and back Application of Circular Linked List

3. Multiplayer games :

• All the Players are kept in a Circular Linked List and the pointer keeps on moving forward as a player's chance ends.

• A jitter bufer is a type of bufer – that takes numbered packets from a network and places them in order, a video or audio player can play them in order. – Packets that are too slow (laggy) are discarded. – This can be represented in a circular bufer, without needing to constantly allocate and deallocate memory, as slots can be re-used once they have been played. PRIMITIVE FUNCTIONS IN CIRCULAR LISTS

• The structure defnition of the circular linked lists and the linear linked list is the same:

struct node { int data; struct node *next; };

typedef struct node *Node PRIMITIVE FUNCTIONS IN CIRCULAR LISTS

• Create

• Insertion (Start, In between, End )

• Deletion (Start, In between, End )

• Searching

• Display

• Sorting

• Reverse

• Split

• Merge Create CLL

Allocate memory to Head node in main ; Head->data = - 999 Head ->next = NULL

1. // in create function pass Head node , return type will be void 2. Ask no of nodes to be inserted n 3. Allocate memory to to Temp , scan Head->data 4. Temp->next = Head; 5. Repeat 3 till n void create_link(struct link *start) { int ch; node = start ; printf("\n Enter 'n' for break:"); scanf(“%d “,&ch); while(inext=getdata() node=node->next; node->next=start; i++; } } Display

Display( Node * head) { p=head->next; do { printf(p->data) p= p->next }while(p !=Head) } Insert At First in CLL void insertFirst(Node * Head) { //create a link struct node *link = (struct node*) malloc(sizeof(struct node)); scanf(“%d”,&link→data);

if (Head->next = NULL) { head ->next = link; link->next = head; } else { //point it to old frst node link->next = head->next; head->next = link; //point frst to new frst node } } Insert At END in CLL void insertEnd(Node * Head) { //create a link struct node *link = (struct node*) malloc(sizeof(struct node)); scanf(“%d”,&link->data);

if (Head->next = NULL) { head ->next = link; link->next = head; } else { p = head->next; While(p->next!=Head) p=p->next; P->next = link; Link->next = head } } Insert in between in CLL void insertINBet(Node * Head) { { //create a link p = head->next; struct node *link = (struct node*) While(p->data!=n && malloc(sizeof(struct node)); p->next!=Head) { Scanf(“%d”,&link->data); p=p->next; Printf(After which to add)”; } Scanf(“%d”,&n); If(p->next ==Head) if (Head->next = NULL) printf(“data not found”); { Else { head ->next = link; Link->next = p->next; link->next = head; P->next = link; } } Else } Delete node from any where void Delete(Node * Head) else { printf(“Data to be deleted”); if(p->next ==Head && p->data ==n ) scanf(“%d”,&n); { p = head->next; head->next = p->next; Q=p; free p; If(p==NULL) //list is empty } while(p->data!=n && p->next! =Head) else { { Q= p; q->next = p->next; p=p->next; free p; } } If(p->next ==Head && p->data ! } =n ) printf(“data not found”);

With Head Node Node structure for polynomial

struct node { int coef , expo; struct node *next; }; typedef struct node *Node Create polynomial using CLL

• Allocate memory to Head node in main ; • Head->data = - 999 • Head ->next = NULL

1. // in create function pass Head node , return type will be void 2. Ask for coef, expo to be inserted in polynomial 3. Allocate memory to to Temp , store coef, expo 4. If Head->next = NULL – head ->next = temp; – temp->next = head;

5. Repeat 3 till (expo !=0) • Else – p = head->next; – Check till exponent greater than temp->exponent – q =p // will point to previous node – p=p->next; – temp->next = q->next – q->next = temp Display

Display( Node * head) { p=Head ->next; do { printf(p->coef , p->expo) p= p→next

}while(p !=Head) }