Algorithm For Linked List
Algorithm For Linked List
Algorithm For Linked List
Struct node{ int data; struct node *next; }; typedef struct node NODE Function create(NODE ** start,int m) Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7 Step 8 Step 9 Begin declare struct pointer variable temp and t [NODE *temp, *t] check if *start equal to NULL then Allocate Space to newly created node Set temp = (NODE *)malloc (sizeof(NODE)) Assign value to that node Set temp data = m Assign NULL to the address part Set temp next = NULL Assign address of the temp variable to start pointer. Set *start = NULL else Begin for loop for temp equal to *start in oder to temp next not equal to NULL with the one increment of address temp do [for(temp = (*start) ; tempnext !=NULL ; temp=tempnext); ]
End for loop Step 10 Allocate space to newly created node Set t = (NODE *) malloc ( sizeof(NODE)) Step 11 Assign value to information part of a node Set tdata = m Step 12 Assign NULL to the address part of NODE t Set tnext=NULL Step 13 Allocate Address of t to the tempnext part Set temp next= t Step 14 endif Step 15 End
Step 10
Begin declare struct pointer variable temp and t [Set NODE *temp, *t] check if equal to 1 than Allocate Space to newly created node Set temp = (NODE *)malloc (sizeof(NODE)) Assign value to that node Set temp data = m Assign *start to the address part Set temp next = (*start) Assign address of the temp variable to start pointer. Set *start = temp else Begin For Loop For i equal to 1 in order to i less than to loc-1 with one increment do Set for (i=1; i<loc-1; i++) increment temp one step ahead Set temp=temp->next Check if temp->next equal to NULL than Position not found Return Enif End For Loop Allocate space to newly created node Set t = (NODE *) malloc ( sizeof(NODE)) Assign value to information part of a node Set tdata = m Assign tempnext to the address part of NODE t Set tnext=tempnext Allocate Address of t to the tempnext part Set temp next= t End if End
Assign value of *start into tempnext Set tempnext = (*start) Assign address of temp into *start Set *start = temp End
Step 5
Step 3 Step 4
Assign value of *start to temp Set temp = (*start) Check if equal to 1 than Assign value of tempnext into *start Set *start = tempnext Free temp Set Free(temp) Endif else For Loop Begin for i equal to 1 in order to i less than and equal to pos -1 with one increment do Set t = temp Set temp = temp next Check if temp equal to NULL than Position not found Return Endif End for loop Assign value of tempnext to tnext Set tnext = tempnext Free temp Set Free(temp) Endif End
Step 7 Step 8
Step 9
Step 4
Step 5 Step 6
Sorting a Linked list (1) Exchange the data part of two node
void sort(int n, NODE **start) Step 1 Step 2 Step 3 Begin declare struct pointer variable temp and t [Set NODE *temp, *t] For Loop Begin for i equal to 0 in order to i is less than n-1 with one increment do Set temp = (*start) Set t= tempnext; For Loop Begin for j equal to 1 in order to i is less than n-i with one increment do check if tempdata greater than tdata than
Step 4 Step 5
Set p = tempdata Set tempdata = tdata Set tdata=p Step 6 Step 7 Step 8 Step 9 Step 10 Endif Set temp = tempnext; Set t = tnext; End for End for End
Step 8 Step 9
Step 22 Step 23 Step 24 Step 25 Step 26 Step 27 Step 28 Step 29 Step 30 Step 31 Step 32 Step 33 Step 34 Step 35 Step 36 Step 37 Step 38 Step 39 Step 40 Step 41 Step 42 Step 43 Step 44 Step 45 Step 46 Step 47
else if p data equal to q data than Assign p data to temp data [ Set temp data = p data] Assign p next to p [Set p = pnext] Assign qnext to q [Set q = qnext] endif endif endif end while While Loop Begin while p not equal to NULL do Allocate space to data [Set ptr = (NODE *) malloc (sizeof(NODE))] Assign ptr to temp next [Set temp next = ptr] Assign temp next to temp [Set temp = temp next] Assign p data to temp data [Set temp data = p data] Assign p next to p [Set p = pnext] End while While Loop Begin while q not equal to NULL do Allocate space to data [Set ptr = (NODE *) malloc (sizeof(NODE))] Assign ptr to temp next [Set temp next = ptr] Assign temp next to temp [Set temp = temp next] Assign q data to temp data [Set temp data = q data] Assign p next to p [Set q = qnext] End while Assign NULL to tempnext End While
End for loop Step 9 Allocate space to newly created node Set t = (NODE *) malloc ( sizeof(NODE)) Step 10 Assign value to information part of a node Set tdata = m Step 11 Assign NULL to the address part of NODE t Set tnext=(*start) Step 12 Allocate Address of t to the tempnext part Set temp next= t
Step 13 Step 14
endif End
Step 13 Step 14
Step 20
End
void dellast (NODE **start) Step 1 Step 2 Step3 Begin Assign value of *start to temp Set temp = (*start) While Loop Begin while tempnext not equal to *start do Set t = temp Set temp = temp next End while Assign temp next in to address part of the t [Set t next = temp next] if *startnext equal to (*start) Set *start = NULL Set Free (temp) else Set Free (temp) End if End
Step Step 5
Check if temp equal to (*start) than Display Position not found Return Endif Step 11 Step 12 Step 13 Step 14 End for loop Assign value of tempnext to tnext Set tnext = tempnext Free temp Set Free(temp) Endif End
Step 17 Step 18 Step 19 Step 20 Step 21 Step 22 Step 23 Step 24 Step 25 Step 26 Step 27 Step 28 Step 29 Step 30 Step 31 Step 32 Step 33 Step 34 Step 35 Step 36 Step 37 Step 38 Step 39 Step 40 Step 41 Step 42 Step 43
else if p data greater than to q data than Assign q data to temp data [ Set temp data = q data] Assign q next to q [ Set q next = q] else if p data equal to q data than Assign p data to temp data [ Set temp data = p data] Assign p next to p [Set p = pnext] Assign qnext to q [Set q = qnext] endif endif endif end while While Loop Begin while p not equal to (*start) do Allocate space to data [Set ptr = (NODE *) malloc (sizeof(NODE))] Assign ptr to temp next [Set temp next = ptr] Assign temp next to temp [Set temp = temp next] Assign p data to temp data [Set temp data = p data] Assign p next to p [Set p = pnext] End while While Loop Begin while q not equal to (*head) do Allocate space to data [Set ptr = (NODE *) malloc (sizeof(NODE))] Assign ptr to temp next [Set temp next = ptr] Assign temp next to temp [Set temp = temp next] Assign q data to temp data
[Set temp data = q data] Assign p next to p [Set q = qnext] End while Assign (*add) to tempnext End While
End for loop Step 11 Allocate space to newly created node Set t = (NODE *) malloc ( sizeof(NODE)) Step 12 Assign value to information part of a node Set tdata = m Step 13 Assign NULL to the next field of NODE t Set tnext=NULL Step 14 Assign temp to the prev field of NODE t Set tprev =temp Step 15 Allocate Address of t to the tempnext part Set temp next= t Step 16 Assign address of t to *tail Set *tail = t; Step 17 endif Step 18 End
Assign value of (*start) to tnext part Sst tnext = (*start) Assign t to (*start)next part Set (*start)next = t Assign NULL to tprev part Set tprev = NULL Assign t to (*start) Set (*start) = t else Begin For Loop For i equal to 1 in order to i less than to pos-1 with one increment do Set for (i=1; i<pos-1; i++) increment temp one step ahead Set temp=temp->next if temp->next equal to NULLthan Position not found Return Endif End For Loop Allocate space for new node Set t=(NODE *) malloc (sizeof(NODE)) Assign m to tdata Set tdata = m Assign tempnext to tnext part Set tnext = tempnext Assign temp to tprev part Set tprev = temp Assign t to tempnextprev part Set tempnextprev = t Assign t to tempnext part Set tempnext = t Endif End
Step 11 Step 12
Second Method
void atlast(NODE **start, NODE **tail) Step 1 Begin Step 2 Assign *tail to temp pointer and tempprev to t Pointer Set temp = (*tail) Set t = tempprev Step 3 if (*tail) prev equal to NULL and (*tail) next equal to NULL than Set *tail = *start = NULL Set free temp pointer Endif Step 4 Set *tail = t Step 5 Assign NULL to t next [Set t next = NULL] Step 6 Free temp poinbter
Step 7 End
Step 6 Step 7
Step 12 Step 13