You are on page 1of 11
I M-Tech I Sem CSE ADS &A Unit-1 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur bus: Introduction to Data Structures, Singly Linked Lists, Doubly Linked Lists, Circular Lists - Algorithms. Stacks and Queues: Algorithm Implementation using Linked Lists. UNIT I INTRODUCTION TO DATA STRUCTURES ly Linked Lists Algorithm: > A Singly Linked List, also called a Chain, is a linked list in which each node represents one element. The operations we can perform on singly linked list are creation, insertion, deletion, display and search. A singly linked list is also called as One Way List. > Example: The following is a singly linked list with 10,2030 and 40 values. Each value is in a separate Node. Head is a pointer which points to the first node. Head node is also called Start node. [HEAD] NULL 10 . 20 . 30 " 40 J 1.Representing chains in java > To represent chains in java first we define a node in java a. Defining a node in java > The node structure of single linked list contains two fields: Data field and Link field. Data field is used to maintain the data element and link field which points to the next node. Each node has only one link field and the last node has a NULL. data | link 4 > The dala field holds data element(s) and the linkfield(s) stores the address of its previous node. > The representation of node in java is shown below. class Node int data; Node link; ing a Cl ingle Linked List class va > Linked lists elements can be stored anywhere in the memory. To maintain the specific sequence of these elements, we need to maintain link(s), > To design a chain in java first we declare two nodes HEAD and ptr. HEAD is a start node & ptris a node which we can use to travel to the last node. Initially we can declare as ‘null’ about these two nodes which is shown below. In java itis represented as class Node int data; Node link; Node(int data) { this. data=data; this.link=null; + } class SLIST Node HEAD=null; Node ptr=null; void create(int n) Node temp=new Node(n); temp.data=n; Page 1 M.Tech I Sem CSE ADS &A Unit-1 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur temp.link=null; } } 2.Chain Manipulation Operations (or)Operations of Single Linked List The following are the operations of Single Linked List 1)Create 2)insert 3)Delete 4)Display 1.Create: > Assume that we want to create a linked list which consists of the following nodes 10,20,30 > Let ptr be pointer to the linked list. > The algorithm is given below. Step 1: Create a new node called “temp” Step 2: Let temp.data=n(value) & temp.link=NULL Step 3: if (HEAD==NULL) then HEAD=temp &ptr=HEAD Step 4: else ptr link=temp & ptr=temp 2.Insert: > There are various positions where a node can be inserted 4, Inserting at the front 2. Inserting at the end 3. Inserting at any other position Linserting at the front: > Assume that we want to insert a node with a data field of 40 at the front, > Itis shown in the following figure. HEAD nll 1 TT -—[ 2 I 20 30 40| “Hemp > The algorithm is given below. Step 1: Create a new node called “temp” Step 2: temp.data=n(value) & temp link=NULL Step 3: temp.link=HEAD Step 4: HEAD=temp; 2Inverting at the end: > Let ptr be a pointer to a linked list. Assume that we want to insert a node with a data field of 50 at the end. Itis shown in the following figure. > The algorithm is given below. Step 1: Create a new Node called temp Step 2: temp.data=n(value) and temp link=NULL Step 3: Move ptr to the last node Step 4: ptrlint Page2 M.Tech I Sem CSE ADS &A Unit-1 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur Inserting at any other position: > Let ptr, ptriare pointers to a linked list. Assume that we want to insert a node with a data field of 60 at position 3. Itis shown in the following figure > The algorithm is given below. Step 1: Create a new node “temp” Step 2: temp.data=n(value) and temp.link=NULL Step 3: Node ptr=HEAD Step 4: Move ptr to the 2 position. Let ptr1=ptr. Step 5: Now move ptr to the next (3") position Step 6: ptr Jink=temp & temp.link=ptr 2.Delete: > There are various ways where a node can be deleted 1. Deleting at the front 2. Deleting at the end 3. Deleting at any other position 1.Deleting at the fro: > Assume that we want to delete a node 10 at the front. > Itis shown in the following figure. HEAD nul T a --1 10 20 30 50 Li > The algorithm is given below. Step 1: if(HEAD!=null) Free first node from memory Step 2: HEAD=HEAD link 2.Deleting at the end: > Assume that we want to delete a node 50 at the last. Let ptr,ptr1 are pointers to a linked list. »® It is shown in the following figure. HEAD tr pr 10 20 > The algorithm is given below. Step 1: Node ptr1=null Step 2: Move ptr to the last previous node. Let ptr1=ptr Step 3: Move ptr to the next node Step 4: Free last node from memory(ptr1 .ink=null) 3.Deleting at any other position: > Assume that we want to delete a node with a data field of 30 at position 3. Let ptr, ptrtare pointers toa linked list. It is shown in the following figure. HEAD ptrt ptr aa 10 20 | -}-{30] + 50] Co > The algorithm is given below. Step 1: Node ptr1=Null & Node ptr=HEAD Page3 TM.Tech I Sem CSE ADS &A Unit-1 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur Step 2: Move ptr to the 2" position. Let ptr1=ptr. Step 3: Now move ptr to the next (3) position Step 4: Free 3rd node from memory(ptr' ink=ptr.link) © To print the data in a list, first we print the contents of ptr’s data field, then we replace ptr with the address in its link field and continue. > The algorithm is given below. Step 1: Node ptr=HEAD Step 2: while(ptt!=NULL) then Step 3: System out printin(ptr.data) Step 4: ptr=ptr.link 2) Doubly Linked Lists-Algorithms: > A Doubly Linked List, is a linked list in which each node represents one element and two pointers. > The first pointer points to the left link (llink) node and the right link (rlink) node. > The operations we can perform on doubly linked list are creation, insertion, deletion, display and search. A doubly linked list is also called as Two Way List & we can move from left to right & right to left. > Example: The following is a doubly linked list with 10,20,30 values. Each value is in a separate Node. Head is a pointer which points to the first node. Head node is also called Start node. eaof| | 10] Fh | 20 | fo] pry 1Representing doubly linked list in java > To represent doubly linked list in java first we define a node in java a. Defining a node in java > The node structure of doubly linked list contains three fields: one Data field and two Link fields. > Data field is used to maintain the data element and one link field is a pointer which points the left node(|link) and other link field is another pointer which points to the right node(tlink) > Each node has two link fields and from left to right the last node has null & from right to left the first node has nul —Lnink| data > The data field holds data element(s) and the linkfield(s) stores the address of its left and right nodes. > The representation of node in java is shown below. class Node int data; Node riink, link; y esigning a Doubly Linked List class in ja" Linked lists elements can be stored anywhere in the memory > To maintain the specific sequence of these elements, we need to maintain link(s) > To design a chain in java first we declare two nodes HEAD and ptr. HEAD is a start node & ptris a node which we can use to travel to the last node. > Initially we can declare as null about these two nodes which is shown below. > In java itis represented as class Node int data Node rlink,llink; Node(int data) { Page 4 M.Tech I Sem CSE ADS &A Unit-1 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur this.data=data; this. fink=null this. link=null; y } class DLIST Node HEAD=null Node ptr=null void create(int n) it Node temp=new Node(n); temp data=n; temp.rlink=null; temp.link=null t } 2. Operations of Doubly Linked List The following are the operations of Doubly Linked List 1)Create 3)Delete 2)insert 4)Display 1.Create: > Assume that we want to create a double linked list which consists of the following nodes 10,20,30. > Let ptr be pointer to the linked list. ptr [reaol | [of FH | 20 [FL Joo] mun] temp > The algorithm is given below. Step 1: Create a new node called “temp” Step 2: Let temp.data=n(value) , temp.rlink=null & temp.llink=null Step 3: if (HEAD==null) then HEAD=temp &ptr=HEAD Step 4: else ptr.rlink=temp, temp.llink=ptr & ptr=temp 2Ansert: > There are various positions where a node can be inserted 4, Inserting at the front 2. Inserting at the end 3. Inserting at any other position Linserting at the front: > Assume that we want to insert a node with a data field of 40 at the front. > Itis shown in the following figure. temp > The algorithm is given below. Step 1: Create a new node called “temp” Step 2: Let temp.data=n(value) , temp.rlink=null & temp.link=null Step 3: temp.rlink=HEAD, HEAD. llink=temp & HEAD=temp 2Inverting at the end: > Let ptr be a pointer to a linked list. Assume that we want to insert a node with a data field of 50 at the end. > Itis shown in the following figure. Page 5 M.Tech I Sem CSE ADS &A Unit-1 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur > The algorithm is given below. Step 1: Create a new Node called temp Step 2: Let temp.data=n(value) , temp.rlink=null & temp.llink=null Step 3: Move ptr to the last node Step 4: pt.tink=temp, temp link=ptr & ptr=temp 3.Inserting at any other posi > Let ptr, ptriare pointers to a linked list. Assume that we want to insert a node with a data field of 60 at position 3. > Itis shown in the following figure. eank—T | 10 | Ft 50{ [4 null temp’ > The algorithm is given below. Step 1: Create a new node “temp” Step 2: Let temp.data=n(value) , temp.rlink=null & temp.llink=null Step 3: Node ptr=HEAD Step 4: Move ptr to the 2" position. Let ptr1=ptr. Step 5: Now move ptr to the next (3) position Step 6: ptr1.rlink=temp, temp.rlink=ptr, ptr.llink=temp & temp. llink=ptrt slete: There are various ways where a node can be deleted 1. Deleting at the front 2. Deleting at the end 3. Deleting at any other position 1 Deleting at the fro: ‘Assume that we want to delete a node 10 at the front > Itis shown in the following figure. > The algorithm is given below. Step 1: if( HEAD!=NULL) Free first node from memory Step 2: HEAD=HEAD. rlink, HEAD. Ilink=NULL Deleting at the end: > Assume that we want to delete a node 50 at the last. Let ptr,ptr are pointers to a linked list. > Itis shown in the following figure. fo HEAD 10] 20) 30} F-4}50) F-4_ null L—_T > The algorithm is given below. Step 1: Node ptrt!=NULL Page 6 IM.Tech I Sem CSE ADS &A Unit-1 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur Step 2: Move ptr to the last node Step 3: Free last node from memory (ptr=ptr.llink & ptr.rlink=NULL) 3.Deleting at any other position: > Assume that we want to delete a node with a data field of 30 at position 3. Let ptr, ptrlare pointers to a linked list. Itis shown in the following figure. pit air HEAD 10 20| 30| 50] To) nut CT > The algorithm is given below. Step 1: Node ptr1!=null & Node ptr=HEAD Step 2: Move ptr to the 2 position. Let ptr1=ptr. Step 3: Now move ptr to the next (3) position Step 4: Free 3rd node from memory (ptr1.rlink=ptr.rlink, ptr=ptr.rlink, ptr.llink=ptr1) 4.Display > To print the data in a list, first we print the contents of ptr's data field, then we replace ptr with the address in its link field and continue in both directions > The algorithm is given below. Step 1: Node ptr=HEAD Elements from Left to Right Step 2: while(ptr!=null) then Step 3: System.out printin( ptr.data) & ptr=ptr.rlink Step 4: Move ptr to the last node Elements from Right to Left Step 5: while(ptr!=null) then Step 6: System.out.printin (ptr.data) & ptr=ptrllink (3) Circular Lists-Algorithms: > In circular list the first element points to the last element and the last element points to the first element. It can be two types. i.e. Circular Single linked list and circular double linked list @Cireular Single Linked List: > In singly linked list, the next pointer of the last node points to the first node. (a)Inserting a node: > Let ptr, ptrtare pointers to a linked list. Assume that we want to insert a node with a data field of 30 at position 3. > It is shown in the following figure. Py Br 10 20 | -}--+_40] +—|_50 30 temp > The algorithm is given by. Step 1: Create a new node “temp” Step 2: temp.data=n(value) and temp .ink=null Step 3: Let first node be ptr Step 4: Move ptr to the 2" position. Let ptr1=ptr. Step 5: Now move ptr to the next (3) position Step 6: ptr ink=temp & temp.link=ptr Page7 I M-Tech I Sem CSE ADS &A Unit-1 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur (byDeleting a node: > Assume that we want to delete a node with a data field of 30 at position 3. Let ptr, ptrlare pointers toa linked list. Itis shown in the following figure > The algorithm is given below. Step 1: Node ptr1=null & Let first node be ptr Step 2: Move ptr to the 2 position. ptr1= Step 3: Now move ptr to the next (3") position Step 4: Free 3rd node from memory(ptr1 link=ptr link) (c)Displaying the Circular single linked list elements: > To print the data in a list, first we print the contents of ptr’s data field, then we replace ptr with the address in its link field and continue. The algorithm is given below. Step 1: Let first node be ptr & Let ptr1=ptr Step 2: while(ptr!=ptr1) then Step 3: System.out printin (ptr.data) & ptr=ptr link Gi) Circular Doubly Linked List: In circular doubly linked list, the llink pointer of the last node points to the first node and the rlink pointer of the first node points to the last node making the circular in both directions. > One link consists of tink [data J stink] > Itis shown in the following figure Hol Fy [20] Fy {so} Fy j4o| Fy _ [50] a \¢ different operations are. (a)nserting a node: Let ptr, piriare pointers to a linked list. Assume that we want to insert a node with a data field of 60 at position 3. Itis shown in the following figure. Pit Br 1 —T 20] $f Jao] Ff [50 60 >. remo 10 > The algorithm is given by Step 1: Create a new node “temp” Step 2: Let temp.data=n(value) , temp.rlink=null & temp.link=null Step 3: Let first node be ptr Step 4: Move ptr to the 2" position. Let ptr1=ptr. Step 5: Now move ptr to the next (3) position Step 6: ptr1.rlink=temp, temp.rlink=ptr, ptr.llink=temp & temp. llink=ptrt b)Deleting a node: Page 8 IM.Tech I Sem CSE ADS &A Unit-1 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur > Assume that we want to delete a node with a data field of 30 at position 3. Let ptr, ptriare pointers to a linked list. itis shown in the above figure. > The algorithm is given by. Step 1: Node *ptr1!=null & Let first node be ptr. Step 2: Move ptr to the 2 position. Let ptr1=ptr. Step 3: Now move ptr to the next (3'%) position Step 4: Free 3rd node from memory (ptr1 rlink=ptr.rlink, ptr=ptr.rlink, ptr.llink=ptr1) ‘ > To print the data in a list, first we print the contents of ptr’s data field, then we replace ptr with the address in its link field and continue. > The algorithm is given below Step 1: Let first node be ptr & Let ptr1=ptr Elements from Left to Right Step 2: while(ptr!=ptr1) then Step 3: System.out printin( ptr.data) & ptr=ptr.rlink Step 4: Move ptr to the last node, Let ptr1=ptr Elements from Right to Left Step 5: while(ptr!=ptr1) then Step 6: System.out printin(ptr.data) & ptr=ptr.llink STACKS AND QUEUE: (a)Linked Staci > The linked stack is shown in the following figure. tior > The algorithm is given by void push(int x) Node temp=new Node(x): if(lemp==null) { ‘System.out printin("Stack is Full! Insertion is not possiblel!!"); return; } else { temp data=x; temp link=top; top=temp; ‘System out printin(x+" element is successfully pushed"); } Page 9 M.Tech I Sem CSE ADS &A Unit-1 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur Gi)The pop0 operation > The function pop() is used for deletion. The algorithm is given by void pop() { inty, ifitop == null) ‘System.out printin( "Stack is Empty!!! Deletion is not possible!!!"); return; else y=top.data; top=top link; ‘System out printin(y#" element is successfully popped”); (iii) The display( operation: > The function display is used to display the elements from the stack. > The algorithm is given by void display() Node if(top: ull) ‘System out printin("The stack is empty. No elements in the stack"); retum; ptr=top: System out printin("The elements of the stack are:") while(ptr'=null) ‘System.out printin(ptr datas" "); ptr=ptr.ink; } } (b)Linked Queues: > Let us add element 50. It is shown in the following figure. front temp rear > The algorithm for addition is given by void addq(int x) { Node ptr=new Node(x); if(ptr==null) System.out printin("Overfiow’); retum; else ptr.data=x; Page 10 M.Tech I Sem CSE ADS &A Unit-1 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur front=ptr; rear=ptr, front link=null; rear Jink=null; } else { rear jink=null System.out printin(x+" element is successfully inserted"); > The algorithm is given by. void deleteq() System out printin("Queue is Empty!!! Deletion is not possible!!"); retum; } else { y=front data; front=front link: ‘System.out printin(y+" element is successfully deleted"); } } (iii) The display() operation: > The algorithm for display is given by void display() ptr=front if(front { ‘System out printin("The stack is empty. No elements in the stack"); return; } else { ‘System.out printin("The elements of the queue are:"); while(ptr!=null) System.out.printin(ptr.data+" "); ptr=ptr.link; ih } } Note: The above stack and queues refer only algorithms Note: For algorithm implementation refer lab programs of linked stack and linked queues.. Page Il

You might also like