Follow

Translate

Tuesday, March 22, 2016

Inserting into a Sorted Linked List


Suppose ITEm is to be inserted into a sorted linked LIST.  Then ITEM must be inserted between nodes A and B so that

                                                   INFO(A) < ITEM <= INFO(B)

The following is a procedure which finds the location LOC of node A, that is, which finds the location LOC of the last node in LIST whose value is less that ITEM.

Traverse the list, using a pointer variable PTR and comparing ITEM with INFO[PTR] at each node. While traversing, keep track of the location preceding node by using a pointer variable SAVE

SAVE : = PTR    and   PTR : = LINK[PTR].

The traversing continues as long as INFO[PTR]  > ITEM, or in the words, the traversing stops as soon as ITEM <= INFO[PTR]. The PTR points to node B, so SAVE will contain the location of the node A.
If You want to learn about the technology, computer science & engineering, web programming, freelancing, earning please click here :CSE SOLVE

Related Posts:

  • Lesson One Data Stucture What is Data Structures? Data Structure: Data may be organized on many different ways; The logical or mathematical model of a particular organization of data is called a data structure. If You want to learn about the techn… Read More
  • We simulate the operation PUSH(STACK, ITEM) We simulate the operation PUSH(STACK, ITEM) (b)  We simulate the operation PUSH(STACK, ITEM): 1.  Since TOP = 3, control is transffered to Step 2. 2.  ITEM = ZZZ. 3. TOP&nbs… Read More
  • Bubble Sort (Bubble Sort) BUBBLE(DATA, N) Here DATA is an array with N elements. This algorithm sorts the elements in DATA. 1.   Repeat Steps 2 and 3 for K = 1 to N - 1. 2.   Set PTR : = 1.    … Read More
  • Stack Lesson One What is mean Stack? Stack: A Stack, also called a last-in-first-out(LIFO) system, is a linear list in which insertions and deletions can take place only at one end, called the top If You want to learn about the technology,… Read More
  • Searching A LINK LIST LIST Is Sorted:   Algorithm:  SRCHSL(INFO, LINK, START, ITEM, LOC) LIST is a Sorted list in memory. This algorithm finds the location LOC of the node where ITEM first appears in LIST, or set… Read More

0 comments:

Post a Comment