CS212: DATASTRUCTURES Lecture 3: Searching 1. SinglyLinked list 2 A linked list is a series of connected nodes. Each node contains at least: – A piece.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Linked Lists.
1/27 COP 3540 Data Structures with OOP Chapter 5 Linked Lists.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
EC-241 Object-Oriented Programming
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.
Ceng-112 Data Structures I 1 Chapter 3 Linear Lists.
Linked List
Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.
LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.
Linked Lists A linked list is a series of connected nodes Each node contains at least –A piece of data (any type) –Pointer to the next node in the list.
1 Linked Lists It’s a conspiracy!. 2 Linked list: a data structure used to represent an ordered list Consists of a sequence of nodes A node consists of.
Linked Lists Spring Linked Lists / Slide 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert,
Josephus Problem: Build the Circular Linked List
Week 3 - Wednesday.  What did we talk about last time?  Started linked lists.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
1/26 COP 3540 Data Structures with OOP Chapter 5 - Part 2 Linked Lists Abstract Data Types and Sorted Lists.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Introduction to Data Structures and Algorithms
1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
1 COP 3540 Data Structures with OOP Chapter 5 - Part 3 Linked Lists Doubly-Linked Lists and Iterators.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
Linked List First next Data Linked List Link next Data Link next Data Link next Data Link Null.
1 CSE 2341 Object Oriented Programming with C++ Note Set #18.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Lists CS 3358.
Linked Lists Damian Gordon.
Java collections library
COP 3538 Data Structures with OOP
Linked Lists.
Data Structures and Algorithms IT12112
Chapter 13 Collections.
Data Structures ADT List
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Linked List Insert After
class PrintOnetoTen { public static void main(String args[]) {
Data Structures ADT List
Introduction to Algorithms and Data Structures
CS148 Introduction to Programming II
Data Structures & Algorithms
Developing Java Applications with NetBeans
Developing Java Applications with NetBeans
Recursive Linked Lists
Programming II (CS300) Chapter 07: Linked Lists
CS148 Introduction to Programming II
Linked Lists.
Linked List Insert After
Linked Lists Dr. Jose Annunziato.
Linked Lists.
Presentation transcript:

CS212: DATASTRUCTURES Lecture 3: Searching 1

SinglyLinked list 2 A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node in the list Head: pointer to the first node. The last node points to NULL.

Example 1 3  Write program that create an Integer linked list: {50,60,70,80,90}.  Add a member function to the class Int_list which:  Add node at a list.

Example 1 – C++ 4 //We use two classes: Node and List #include using namespace std; // we start with class node for nodes. class node{ public: node(int y){ info=y; next=0;} int info; node *next;};

Example 1 – C++ 5 // Declare class list class list{ private: node *head, *tail; int count; public: list(){head=tail=0; count=0;} // Declare addnode function in next slide };

Example 1 – C++ 6

7 //Using List : void main() {list s; s.addNode(10); s.addNode(12); s.addNode(11); s.addNode(13); s.addNode(5); s.addNode(10); }

Example 1 – Java 8  Node Class class Node { public int iData; public Node next; public Node(int dd){ iData=dd; } public void displayLink(){ System.out.print("{"+iData+"} "); }

Example 1 – Java 9  List Class class List{ private Node head; private Node last; public int count; public List(){ // next by default initialized to NULL count=0; head = null; last = null; }

Example 1 – Java 10  List Class – insertNode method public void insertNode(int key){ // insert in order Node newNode = new Node(key); // make new node Node pre = null; Node current = head; // start at first node "head" // until end of list, while(current != null && key > current.iData) { pre = current; current = current.next; // go to next item } if (current == null || key < current.iData){ if(pre==null){ // at beginning of list head = newNode;// head --> newNode newNode.next = current; }// newNode --> Ploc else{ // not at beginning, maybe in a middle or ending of the list pre.next = newNode; // old Ppre --> newNode newNode.next = current; }// newNode --> Ploc count++; // increament the counter } else // if a key is already exist in a list System.out.println("Duplicated data"); } // end insertNode()

Example 1 – Java 11  List Class – displayList method public void displayList(){ System.out.print("List (first-->last): "); Node current = head; while (current != null){ current.displayNode(); current = current.next; } System.out.println(""); }

Example 1 – Java 12  List Class – insertFirst method public void insertFirst(int dd){ Node newNode = new Node(dd); newNode.next = head; head = newNode; }

Example 1 – Java 13  Main Method  Output package javaapplication12; public class JavaApplication12 { public static void main(String[] args) { List s = new List(); s.insertNode(50); s.insertNode(60); s.insertNode(70); s.insertNode(80); s.insertNode(90); s.displayList(); s.insertNode(66); s.displayList(); s.insertFirst(88); s.displayList(); }