Skip to content

Commit f363911

Browse files
authored
Add files via upload
1 parent f89f6b8 commit f363911

File tree

1 file changed

+207
-0
lines changed

1 file changed

+207
-0
lines changed

TicketingDS_1.java

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package ticketingsystem;
2+
import java.util.concurrent.locks.Lock;
3+
import java.util.concurrent.locks.ReentrantLock;
4+
5+
6+
public class TicketingDS_1 implements TicketingSystem {
7+
8+
public TicketingDS_1(int routenum, int coachnum, int seatnum, int stationnum){}
9+
Ticket ticket = new Ticket();
10+
11+
public class Node{
12+
protected Node next;
13+
protected int key;
14+
15+
Lock lock;
16+
17+
public Node(int key){
18+
this.key = key;
19+
this.key = key;
20+
this.lock = new ReentrantLock();
21+
}
22+
23+
public void display(){
24+
System.out.println(key + " ");
25+
}
26+
/**
27+
* Lock Node
28+
*/
29+
void lock() {lock.lock();}
30+
/**
31+
* Unlock Node
32+
*/
33+
void unlock() {lock.unlock();}
34+
}
35+
36+
public class MyLinkedList{
37+
public Node head;
38+
public Node current;
39+
40+
//添加节点
41+
public void add(int data) {
42+
//如果头结点为空,为头结点
43+
if(head == null) {
44+
head = new Node(data);
45+
current = head;
46+
} else {
47+
current.next = new Node(data);
48+
current = current.next;
49+
}
50+
}
51+
52+
//打印链表
53+
public void print(Node node) {
54+
if(node == null) {
55+
return;
56+
}
57+
58+
current = node;
59+
while(current != null) {
60+
System.out.print(current.key + " ");
61+
current = current.next;
62+
}
63+
}
64+
65+
//求链表长度
66+
public int get_length(Node head){
67+
if (head == null){
68+
return -1;
69+
}
70+
int length = 0;
71+
current = head;
72+
while(current != null){
73+
length++;
74+
current = current.next;
75+
}
76+
return length;
77+
}
78+
79+
//初始化链表,并且返回表头
80+
public Node init() {
81+
for(int i=0; i<10; i++) {
82+
this.add(i);
83+
}
84+
return head;
85+
}
86+
}
87+
88+
89+
90+
@Override
91+
public Ticket buyTicket(String passenger, int route, int departure, int arrival) {
92+
// TODO Auto-generated method stub
93+
int k = route;
94+
// Add sentinels to start and end
95+
MyLinkedList list = new MyLinkedList();
96+
Node head = list.init();
97+
//list.add(k);
98+
list.print(head);
99+
System.out.println("链表长度为: "+ list.get_length(head));
100+
//创建前驱结点(起点)和当前节点和目标节点(终点)
101+
Node predpred = new Node(departure-1);
102+
Node pred = new Node(departure);
103+
Node curr = pred.next;
104+
Node succ = new Node(arrival);
105+
//predpred.key = departure -1;
106+
//pred.key = departure;
107+
//succ.key = arrival;
108+
109+
110+
for (int i = 1; i <= 8*100; i++){
111+
head.lock();
112+
try {
113+
pred = head;
114+
curr = pred.next;
115+
curr.lock();
116+
try {
117+
while (curr.key < succ.key) {
118+
//System.out.println(predpred.key);
119+
//System.out.println(curr.key + " " + succ.key);
120+
pred.unlock();
121+
pred = curr;
122+
curr = curr.next;
123+
curr.lock();
124+
}
125+
if (curr.key == succ.key) {
126+
predpred.next = curr.next;
127+
ticket.tid = k*1000 + i/100+100 + i%100;
128+
ticket.passenger = passenger;
129+
ticket.route = route;
130+
ticket.coach = i/100+1;
131+
ticket.seat = i%100;
132+
ticket.departure = departure;
133+
ticket.arrival = arrival;
134+
//System.out.println("lock"+ ticket);
135+
return ticket;
136+
}
137+
} finally {curr.unlock();}
138+
} finally {pred.unlock();}
139+
i++;
140+
list.print(head);
141+
System.out.println("新链表长度为: "+ list.get_length(head));
142+
}
143+
144+
145+
//检索每一张座位的区间。看看我的座位的区间在不在别人的区间内。
146+
return null;
147+
}
148+
149+
Ticket ticket2 = new Ticket();
150+
@Override
151+
public int inquiry(int route, int departure, int arrival) {
152+
// TODO Auto-generated method stub
153+
int k = route;
154+
int count = 0;
155+
ticket2.departure = departure;
156+
ticket2.arrival = arrival;
157+
for (int i = 1; i <= 8*100; i++){
158+
ticket2.seat = i%100;
159+
if (ticket2.passenger == null){
160+
if ((departure >= ticket.arrival) | (arrival <= ticket.departure)){
161+
count ++;
162+
}
163+
}
164+
}
165+
166+
return count;
167+
}
168+
@Override
169+
public boolean refundTicket(Ticket ticket) {
170+
// TODO Auto-generated method stub
171+
MyLinkedList list = new MyLinkedList();
172+
Node head = list.init();
173+
Node predpred = new Node(ticket.departure-1);
174+
head.lock();
175+
Node pred = head;
176+
Node succ = new Node(ticket.arrival);
177+
178+
try {
179+
Node curr = pred.next;
180+
curr.lock();
181+
try {
182+
while (curr.key < succ.key) {
183+
pred.unlock();
184+
pred = curr;
185+
curr = curr.next;
186+
curr.lock();
187+
}
188+
if (curr.key == succ.key) {
189+
for (int i = ticket.arrival; i >= ticket.departure; i--){
190+
Node newNode = new Node(i);
191+
newNode.next = curr;
192+
pred.next = newNode;
193+
curr = newNode;
194+
return true;
195+
}
196+
}
197+
} finally {
198+
curr.unlock();
199+
}
200+
} finally {
201+
pred.unlock();
202+
}
203+
204+
return false;
205+
}
206+
207+
}

0 commit comments

Comments
 (0)