Data Structure Introduction And Linked List, Lecture Slide - Computer Science, Slides of C programming

Data structure introduction and motivation, Object references, Recursive data structure listnode, References of links, Linked list a list of listnodes, Examples operations enumerations, Examples operators insertatfront, Examples operations removefromfront, Examples operations insertatback, Insertatback with list node, Graphical representations of insertatback, Graphical representations of removefromfront, Graphical representations of insertatfront

Typology: Slides

2010/2011

Uploaded on 10/05/2011

christina
christina 🇺🇸

4.6

(23)

393 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS 112 Introduction to
Programming
Lecture #34:
Data Structures:
Introduction and Linked Lists
http://flint.cs.yale.edu/cs112/
2
Outline
qAdmin. and review
ØIntroduction to data structures
qLinked lists
3
Data Structures: Motivation
rSome typical operations:
maccess the i-th element
minsert
at the front
at the back
in the middle
mdelete
the first element
the last element
the middle element
rQuestion: how many operations do we have to perform to
implement each of the above operations on an array?
data structures study how and what data are stored in a computer so that
operations can be implemented efficiently
4
Outline
qAdmin. and review
rIntroduction to data structures
ØLinked lists
5
Object References
rRecall that an object reference is a variable
that stores the address of an object
rA reference can also be called a pointer
student
John/
40725
Graphical representation:
class Student
{
private string name;
private int id;
}6
Recursive Data Structure: ListNode
object references can be used to create links between objects
class Student
{
private string name;
private int id;
public Student( string name, int id )
{
this.name = name; this.id = id;
}
public string ToString()
{
return String( “{0}/{1}”,
name, id )
}
}
class ListNode
{private object data;
// recursive data structure
private ListNode next;
public ListNode( object data)
{
this.data = data;
next = null;
}
public ListNode( object data,
ListNode next)
{
this.data = data;
this.next = next;
}
}
pf3
pf4
pf5

Partial preview of the text

Download Data Structure Introduction And Linked List, Lecture Slide - Computer Science and more Slides C programming in PDF only on Docsity!

CS 112 Introduction to

Programming

Lecture #34:

Data Structures:

Introduction and Linked Lists

http://flint.cs.yale.edu/cs112/

Outline

q Admin. and review

ÿ Introduction to data structures

q Linked lists

Data Structures: Motivation

r Some typical operations:

m access the i-th element

m insert

  • at the front
  • at the back
  • in the middle

m delete

  • the first element
  • the last element
  • the middle element

r Question: how many operations do we have to perform to

implement each of the above operations on an array?

data structures study how and what data are stored in a computer so that

operations can be implemented efficiently

Outline

q Admin. and review

r Introduction to data structures

ÿ Linked lists

Object References

r Recall that an object reference is a variable

that stores the address of an object

r A reference can also be called a pointer

student

John/

Graphical representation:

class Student

private string name;

private int id;

Recursive Data Structure: ListNode

object references can be used to create links between objects

class Student

private string name;

private int id;

public Student( string name, int id )

this.name = name; this.id = id;

public string ToString()

return String( “{0}/{1}”,

name, id )

class ListNode

private object data;

// recursive data structure

private ListNode next ;

public ListNode( object data)

this.data = data;

next = null;

public ListNode( object data,

ListNode next)

this.data = data;

this.next = next;

References as Links: Example

Student ted = new Student(“Ted”, 58821);

ListNode n1 = new ListNode( ted );

Student jane = new Student(“Jane”, 40725);

ListNode n2 = new ListNode(jane, n1);

ListNode firstNode = n2;

Jane/

Ted/

firstNode

the student object is

stored somewhere else;

a ListNode only stores

a reference; all data are

shown here to save space

Linked List: a List of ListNodes

firstNode

Ann/

Ted/

Jane/

Example Operation: Enumeration

Class LinkList

ListNode firstNode = null;

public void PrintAll()

ListNode current = firstNode;

while ( current != null )

Console.WriteLine( current.data );

current = current.next;

} // end of PrintAll

Example Operation: InsertAtFront

public void InsertAtFront( object nData )

ListNode newNode = new ListNode( nData );

newNode.next = firstNode;

firstNode = newNode;

Jane/

newNode

firstNode

Ted/

Ann/

A Graphical Representation of the

InsertAtFront Operation

Jane/

newNode

(a) firstNode

Ted/

Ann/

(b)

Jane/

newNode

firstNode

Ted/

Ann/

firstNode = newNode;

newNode.next = firstNode;

Example Operation:

RemoveFromFront

public object RemoveFromFront( )

if ( firstNode == null ) return null;

object result = firstNode.data;

firstNode = firstNode.next;

return result;

Jane/

firstNode

Ted/

Ann/

 2001 Prentice Hall, Inc.

All rights reserved.

Outline

LinkedListLibrar

y.cs

68 // construct empty List with "list" as its name 69 public List() : this( "list" ) 70 { 71 } 72 73 // Insert object at front of List. If List is empty, 74 // firstNode and lastNode will refer to same object. 75 // Otherwise, firstNode refers to new node. 76 public void InsertAtFront( object insertItem ) 77 { 78 lock ( this ) 79 { 80 if ( IsEmpty() ) 81 firstNode = lastNode = 82 new ListNode( insertItem ); 83 else 84 firstNode = 85 new ListNode( insertItem, firstNode ); 86 } 87 } 88 89 // Insert object at end of List. If List is empty, 90 // firstNode and lastNode will refer to same object. 91 // Otherwise, lastNode's Next property refers to new node. 92 public void InsertAtBack( object insertItem ) 93 { 94 lock ( this ) 95 { 96 if ( IsEmpty() ) 97 firstNode = lastNode = 98 new ListNode( insertItem ); 99

Method to insert an

object at the front of

the list

Test if list is empty

Get list lock

If list is empty, create new

node and set firstNode

and lastNode to refer to it

If list is not empty, insert

object by setting its next

reference to the first node

Method to insert object

into back of list

Get list lock

Test if list is empty

If list is empty create a new

node and set firstNode and

lastNode to reference it

 2001 Prentice Hall, Inc.

All rights reserved.

Outline

LinkedListLibrar

y.cs

100 else 101 lastNode = lastNode.Next = 102 new ListNode( insertItem ); 103 } 104 } 105 106 // remove first node from List 107 public object RemoveFromFront() 108 { 109 lock ( this ) 110 { 111 object removeItem = null; 112 113 if ( IsEmpty() ) 114 throw new EmptyListException( name ); 115 116 removeItem = firstNode.Data; // retrieve data 117 118 // reset firstNode and lastNode references 119 if ( firstNode == lastNode ) 120 firstNode = lastNode = null; 121 122 else 123 firstNode = firstNode.Next; 124 125 return removeItem; // return removed data 126 } 127 } 128 129 // remove last node from List 130 public object RemoveFromBack() 131 { 132 lock ( this ) 133 { 134 object removeItem = null;

If list is not empty, create a new

node and set the last node’s next

reference to the new node

Method to remove an object

from the front of list

Get lock

Throw exception

if list is empy

Set removeItem equal

to data in first node

If there is only one node in the

list, set firstNode and lastNode

references to null

If there is more then one

node, set firstNode to

Return data reference the second node

stored in node

Method to remove an object

from the back of the list

 2001 Prentice Hall, Inc.

All rights reserved.

Outline

LinkedListLibrar

y.cs

136 if ( IsEmpty() ) 137 throw new EmptyListException( name ); 138 139 removeItem = lastNode.Data; // retrieve data 140 141 // reset firstNode and lastNode references 142 if ( firstNode == lastNode ) 143 firstNode = lastNode = null; 144 145 else 146 { 147 ListNode current = firstNode; 148 149 // loop while current node is not lastNode 150 while ( current.Next != lastNode ) 151 current = current.Next; // move to next node 152 153 // current is new lastNode 154 lastNode = current; 155 current.Next = null; 156 } 157 158 return removeItem; // return removed data 159 } 160 } 161 162 // return true if List is empty 163 public bool IsEmpty() 164 { 165 lock ( this ) 166 { 167 return firstNode == null; 168 } 169 }

Set removeItem equal to

the data in the last node

If there is only one node, set firstNode

and lastNode to refer to null

Loop until next to

last node is reached

Set lastNode to refer

to the next to last node

Set reference of new

last node to null

Return data of old last node

Method to test if list is empty

 2001 Prentice Hall, Inc.

All rights reserved.

Outline

LinkedListLibrar

y.cs

171 // output List contents 172 virtual public void Print() 173 { 174 lock ( this ) 175 { 176 if ( IsEmpty() ) 177 { 178 Console.WriteLine( "Empty " + name ); 179 return; 180 } 181 182 Console.Write( "The " + name + " is: " ); 183 184 ListNode current = firstNode; 185 186 // output current node data while not at end of list 187 while ( current != null ) 188 { 189 Console.Write( current.Data + " " ); 190 current = current.Next; 191 } 192 193 Console.WriteLine( "\n" ); 194 } 195 } 196 197 } // end class List 198

Method to output the list

Tell user if list is empty

Output data in list

 2001 Prentice Hall, Inc.

All rights reserved.

Outline

LinkedListLibrar

y.cs

199 // class EmptyListException definition 200 public class EmptyListException : ApplicationException 201 { 202 public EmptyListException( string name ) 203 : base( "The " + name + " is empty" ) 204 { 205 } 206 207 } // end class EmptyListException 208 209 } // end namespace LinkedListLibrary

Handles illegal operations

on an empty list

 2001 Prentice Hall, Inc.

All rights reserved.

Outline

ListTest.cs

1 // Fig 23.5: ListTest.cs 2 // Testing class List. 3 4 using System; 5 using LinkedListLibrary; 6 7 namespace ListTest 8 { 9 // class to test List class functionality 10 class ListTest 11 { 12 static void Main( string[] args ) 13 { 14 List list = new List(); // create List container 15 16 // create data to store in List 17 bool aBoolean = true; 18 char aCharacter = '$'; 19 int anInteger = 34567; 20 string aString = "hello"; 21 22 // use List insert methods 23 list.InsertAtFront( aBoolean ); 24 list.Print(); 25 list.InsertAtFront( aCharacter ); 26 list.Print(); 27 list.InsertAtBack( anInteger ); 28 list.Print(); 29 list.InsertAtBack( aString ); 30 list.Print(); 31 32 // use List remove methods 33 object removedObject; 34

Create list of objects

Insert objects at beginning

of listinto list using

InsertAtFront method

Insert objects at end of

listinto list using

InsertAtBack method

Print the list

Create data to put in list

 2001 Prentice Hall, Inc.

All rights reserved.

Outline

ListTest.cs

35 // remove data from list and print after each removal 36 try 37 { 38 removedObject = list.RemoveFromFront(); 39 Console.WriteLine( removedObject + " removed" ); 40 list.Print(); 41 42 removedObject = list.RemoveFromFront(); 43 Console.WriteLine( removedObject + " removed" ); 44 list.Print(); 45 46 removedObject = list.RemoveFromBack(); 47 Console.WriteLine( removedObject + " removed" ); 48 list.Print(); 49 50 removedObject = list.RemoveFromBack(); 51 Console.WriteLine( removedObject + " removed" ); 52 list.Print(); 53 } 54 55 // process exception if list empty when attempt is 56 // made to remove item 57 catch ( EmptyListException emptyListException ) 58 { 59 Console.Error.WriteLine( "\n" + emptyListException ); 60 } 61 62 } // end method Main 63 64 } // end class ListTest 65 }

Remove objects from

front of list using method

RemoveFromFront

Remove objects from

back of list using method

RemoveFromBack

Print the list after

each remove

If remove is called on

an empty list tell user

 2001 Prentice Hall, Inc.

All rights reserved.

Outline

ListTest.cs

Program Output

The list is: True

The list is: $ True

The list is: $ True 34567

The list is: $ True 34567 hello

$ removed The list is: True 34567 hello

True removed The list is: 34567 hello

hello removed The list is: 34567

34567 removed Empty list