0% found this document useful (0 votes)
23 views9 pages

Tutorial 10 So Land Qu Stion

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views9 pages

Tutorial 10 So Land Qu Stion

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Java113 tutorial 10 ‫ يوجد حلين حل عادي و حل‬Generic

1 public class Student


2 {
3 private String name ;
4 private String id ;
5 private char Passed ;
6
12 public Student(String n , String d , char p)
13 {
14 name = n ;
15 id = d;
16 Passed = p ;
17 }
18
19 public String getId()
20 {
21 return id ;
22 }
23
24 public String getName()
25 {
26 return name ;
27 }
28
29 public char getPassed()
30 {
31 return Passed ;
32 }
33 }
34====================

1 public class Node


2 {
3 private Student data ;
4 private Node next ;
5
6 public Node(Student n)
7 {
8 data = n ;
9 next = null ;
10 }
11
12 public void setNext(Node nextPtr)
13 {
14 next = nextPtr ;
15 }
16
17 public Node getNext()
18 {
19 return next ;
20 }
21

1
22 public void setData(Student n )
23 {
24 data = n ;
25 }
26
27 public Student getData()
28 {
29 return data ;
30 }
31
32 }
33 //-------------------
34 1 public class List
2 {
3 private Node head;
4 private Node tail;
5
6 public List() {
7 head = tail = null;
8 }
9
10 public boolean isEmpty() {
11 return head == null;
12 }
13
14 public int size()
15 {
16 if(isEmpty() )
17 return 0 ;
18
19 int count = 0 ;
20 Node current = head ;
21
22 while(current != null )
23 {
24 count++ ;
25 current = current.getNext() ;
26 }
27 return count ;
28 }
29
32 public void insertAtFront(Student n) {
33 Node newNode = new Node(n);
34
35 if(isEmpty())
36 head = tail = newNode;
37 else{
38 newNode.setNext(head);
39 head=newNode;
40 }
41 }
42
43 public void insertAtBack(Student n) {

2
44 Node newNode = new Node(n);
45
46 if(isEmpty())
47 head = tail = newNode;
48 else{
49 tail.setNext(newNode);
50 tail=newNode;
51 }
52 }
53
54 public Student removeFromFront()
55 {
56 if(isEmpty())
57 return null ;
58
59 Node temp = head ;
60
61 if(head == tail )
62 {
63 head = tail = null ;
64 return temp.getData() ;
65 }
66 else
67 {
68 head = head.getNext() ;
69 return temp.getData() ;
70 }
71 }
72 //----------------------
73
74 public Student removeFromBack()
75 {
76 if(isEmpty())
77 return null ;
78
79 Node temp = tail ;
80
81 if(head == tail )
82 {
83 head = tail = null ;
84 return temp.getData() ;
85 }
86 else
87 {
88 Node current = head ;
89
90 while(current.getNext() != tail )
91 current = current.getNext() ;
92
93 tail = current ;
94 tail.setNext(null) ;
95 return temp.getData() ;
96 }// else

3
97 }
98
99 public int NumberOfPassedStudent()
100 {
101 if(isEmpty() )
102 {
103 return 0 ;
104 }
105
106 int num = 0 ;
107 Node current = head ;
108
109 while(current != null )
110 {
111 if( current.getData().getPassed() == 'P' )
112 num++ ;
113
114
115 current = current.getNext() ;
116 }
117
118 return num ;
119 }
120
121 }//class list
122===============================================

1 public class test


2 {
3 public static void main(String args[])
4 {
5 List list = new List() ;
6
7 Student s1 = new Student("Mohammad" , "94555" , 'P') ;
8 Student s2 = new Student("Saleh" , "94444" , 'U') ;
9 Student s3 = new Student("Salem" , "94333" , 'P') ;
10
11 list.insertAtFront(s2) ;
12 list.insertAtBack(s3) ;
13 list.insertAtBack(s1);
14
15 System.out.println("number of pass student " +
list.NumberOfPassedStudent() ) ;
16
17 int len = list.size() ;
18 for(int i = 0 ; i < len ; i++)
19 {
20 Student s = list.removeFromFront() ;
21 if(s. getPassed() == 'P')
22 list.insertAtBack(s) ;
23 }
24
25 // print

4
26 len = list.size(); // must check length again after delet
27
28 for(int i = 0 ; i < len ; i++)
29 {
30 Student s = list.removeFromFront() ;
31 System.out.println(s.getName() + " " +s.getId() + " " +
s.getPassed() ) ;
32
33 list.insertAtBack(s) ;
34
35 }
36
37 }
38 }
=================================

Generic solution :

1 public class Student


2 {
3 private String name ;
4 private String id ;
5 private char Passed ;
6
11
12 public Student(String n , String d , char p)
13 {
14 name = n ;
15 id = d;
16 Passed = p ;
17 }
18
19 public String getId()
20 {
21 return id ;
22 }
23
24 public String getName()
25 {
26 return name ;
27 }
28
29 public char getPassed()
30 {
31 return Passed ;
32 }
33 }
34====================================

1 public class Node<T>


2 {
3 private T data ;
4 private Node next ;
5

5
6 public Node(T n)
7 {
8 data = n ;
9 next = null ;
10 }
11
12 public void setNext(Node nextPtr)
13 {
14 next = nextPtr ;
15 }
16
17 public Node getNext()
18 {
19 return next ;
20 }
21
22 public void setData(T n )
23 {
24 data = n ;
25 }
26
27 public T getData()
28 {
29 return data ;
30 }
31
32 }
33 //-------------------
34 1 public class List<T>
2 {
3 private Node<T> head;
4 private Node<T> tail;
5
6 public List() {
7 head = tail = null;
8 }
9
10 public boolean isEmpty() {
11 return head == null;
12 }
13
14 public int size()
15 {
16 if(isEmpty() )
17 return 0 ;
18
19 int count = 0 ;
20 Node<T> current = head ;
21
22 while(current != null )
23 {
24 count++ ;
25 current = current.getNext() ;

6
26 }
27 return count ;
28 }
29
30
31
32 public void insertAtFront(T n) {
33 Node<T> newNode = new Node<T>(n);
34
35 if(isEmpty())
36 head = tail = newNode;
37 else{
38 newNode.setNext(head);
39 head=newNode;
40 }
41 }
42
43 public void insertAtBack(T n) {
44 Node<T> newNode = new Node<T>(n);
45
46 if(isEmpty())
47 head = tail = newNode;
48 else{
49 tail.setNext(newNode);
50 tail=newNode;
51 }
52 }
53
54 public T removeFromFront()
55 {
56 if(isEmpty())
57 return null ;
58
59 Node<T> temp = head ;
60
61 if(head == tail )
62 {
63 head = tail = null ;
64 return temp.getData() ;
65 }
66 else
67 {
68 head = head.getNext() ;
69 return temp.getData() ;
70 }
71 }
72 //----------------------
73
74 public T removeFromBack()
75 {
76 if(isEmpty())
77 return null ;
78

7
79 Node<T> temp = tail ;
80
81 if(head == tail )
82 {
83 head = tail = null ;
84 return temp.getData() ;
85 }
86 else
87 {
88 Node current = head ;
89
90 while(current.getNext() != tail )
91 current = current.getNext() ;
92
93 tail = current ;
94 tail.setNext(null) ;
95 return temp.getData() ;
96 }// else
97 }
98
99 public int NumberOfPassedStudent()
100 {
101 if(isEmpty() )
102 {
103 return 0 ;
104 }
105
106 int num = 0 ;
107 Node<T> current = head ;
108
109 while(current != null )
110 {
111 if( ( (Student) (current.getData()) ).getPassed() == 'P' )
112 num++ ;
113
114
115 current = current.getNext() ;
116 }
117
118 return num ;
119 }
120
121 }//class list
122=================================================

1 public class test


2 {
3 public static void main(String args[])
4 {
5 List<Student> list = new List<Student>() ;
6
7 Student s1 = new Student("Mohammad" , "94555" , 'P') ;
8 Student s2 = new Student("Saleh" , "94444" , 'U') ;

8
9 Student s3 = new Student("Salem" , "94333" , 'P') ;
10
11 list.insertAtFront(s2) ;
12 list.insertAtBack(s3) ;
13 list.insertAtBack(s1);
14
15 System.out.println("number of pass student " +
list.NumberOfPassedStudent() ) ;
16
17 int len = list.size() ;
18 for(int i = 0 ; i < len ; i++)
19 {
20 Student s = ( Student) list.removeFromFront() ;
21 if(s. getPassed() == 'P')
22 list.insertAtBack(s) ;
23 }
24
25 // print
26 len = list.size(); // must check length again after delet
27
28 for(int i = 0 ; i < len ; i++)
29 {
30 Student s = ( Student )list.removeFromFront() ;
31 System.out.println(s.getName() + " " +s.getId() + " " +
s.getPassed() ) ;
32
33 list.insertAtBack(s) ;
34
35 }
36
37 }
38 }
39
40

You might also like