Introduction to Data Structures - Introduction LinkedList...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction LinkedList and Array Linked lists are a very comon way of arrays of data .The major benifit of linked lists is that we do not specific a fixed size for the list.The more elements we add to the chain,bigger the chain gets. There are more than one type of linked lists in java Singly Linked List Root node linked one way through all the nodes .The last node linked to null. Circular Linked List Circular linked lists have a reference to one node which is the tail node and all the nodes are linked together in one direction forming a circle. The benefit of using circular lists is that appending to the end can be done very quickly. Doubly Linked List Every node stores a reference to its previous node as well as its next. This is good if we need to move back by few nodes and don't want to run from the beginning of the list. Code for the class ExampleArray public class ExampleArray { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int [] arrayA = new int [5]; arrayA[0] = 10; arrayA[1] = 20; arrayA[2] = 30; arrayA[3] = 40;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
arrayA[4] = 50; System. out .println(arrayA. length ); int [] arrayB = new int [6]; int i = 0; for (i = 0; i < arrayA. length ; i++) arrayB[i] = arrayA[i]; arrayB[i] = 60; arrayA = arrayB; System. out .println(arrayA. length ); } } Output
Background image of page 2
Code for the class TeatLinkedList import java.util.LinkedList; public class TestLinkedList { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub LinkedList<Integer> ids = new LinkedList<Integer>(); ids.add(10); ids.add(20); ids.add(30); ids.add(40); ids.add(50); System. out .println(ids.size()); ids.add(60); System. out .println(ids.size()); } } Output
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Stacks and Queues Stacks A stack consists of a sequence of items, which should be thought of as piled one on top of the other like a physical stack of boxes or cafeteria trays. Only the top item on the stack is accessible at any given time. It can be removed from the stack with an operation called pop. An item lower down on the stack can only be removed after all the items on top of it have been popped off the stack. A new item can be added to the top of the stack with an operation called push. We can make a stack of any type of items. If, for example, the items are values of type int, then the push and pop operations can be implemented as instance methods void push (int newItem) -- Add newItem to top of stack. int pop() -- Remove the top int from the stack and return it. It is an error to try to pop an item from an empty stack, so it is important to be able to tell whether a stack is empty. We need another stack operation to do the test, implemented as an instance method boolean isEmpty() -- Returns true if the stack is empty. Queues
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

Introduction to Data Structures - Introduction LinkedList...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online