Top Banner
CS212: DATASTRUCTURES Lecture 3: Searching 1
13

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.

Jan 21, 2016

Download

Documents

Malcolm Doyle
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

CS212: DATASTRUCTURES

Lecture 3: Searching

1

Page 2: 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.

2

SinglyLinked 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 in the list

• Head: pointer to the first node.• The last node points to NULL.

Page 3: 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.

3

Example 1

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.

Page 4: 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.

4

Example 1 – C++

//We use two classes: Node and List

#include<iostream>

using namespace std;

// we start with class node for nodes.

class node{

public:

node(int y){

info=y;

next=0;}

int info;

node *next;};

Page 5: 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.

5

Example 1 – C++

// Declare class list

class list{

private:

node *head, *tail;

int count;

public:

list(){head=tail=0; count=0;}

// Declare addnode function in next slide

};

Page 6: 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.

6

Example 1 – C++

Page 7: 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.

7

Example 1 – C++

//Using List :

void main()

{list s;

s.addNode(10);

s.addNode(12);

s.addNode(11);

s.addNode(13);

s.addNode(5);

s.addNode(10);

}

Page 8: 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.

8

Example 1 – Java

Node Class

class Node { public int iData; public Node next; public Node(int dd){ iData=dd; } public void displayLink(){ System.out.print("{"+iData+"} "); }

}

 

Page 9: 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.

9

Example 1 – Java

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;

}

}

Page 10: 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.

10

Example 1 – Java

List Class – insertNode methodpublic 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()

Page 11: 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.

11

Example 1 – Java

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(""); }

Page 12: 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.

12

Example 1 – Java

List Class – insertFirst method

public void insertFirst(int dd){ Node newNode = new Node(dd); newNode.next = head; head = newNode; }

Page 13: 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.

13

Example 1 – Java

Main Method Outputpackage 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(); }}