Tải bản đầy đủ (.pdf) (52 trang)

The project final report doubly linked list by c++

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.89 MB, 52 trang )

<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">

<b>BỘ GIÁO DỤC VÀ ĐÀO TẠO </b>

<b>TRƯỜNG ĐẠI HỌC SƯ PHẠM KĨ THUẬT TP. HỒ CHÍ MINH KHOA CÔNG NGHỆ THÔNG TIN </b>

<b>THE PROJECT FINAL REPORTINSTRUCTOR : PhD. LE VAN VINHSUBJECT CODE : DASA230179_22_1_10</b>

<b>STUDENT : NGUYEN THI PHUSTUDENT ID : 21110600</b>

TP. HỒ CHÍ MINH THÁNG 12 NĂM 2022<b>- </b>

</div><span class="text_page_counter">Trang 2</span><div class="page_container" data-page="2">

<b>Name ID Task Completion rate </b>

1. Install Doubly Linked List, Queue Structure,

Hash Tableand their

</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">

<b>I. PROJECT 1.1 : DOUBLY LINKED LIST BY C++ : </b>

− A Linked List is a list which includes one or many NODE, we define a NODE has at least two elements : <b><data_type> data</b> and <b>NODE next</b>. We use the element <b>data to store the users’ data, and the element next</b> to link to the others. We use NODE head and NODE tail to manage our linked list easily.

− A Doubly Linked List contains an extra pointer : <b>NODE previous</b>, typically called the previous pointer, together with the next pointer and data which are there in the Singly Linked List.

− Installing by C++ language programming “struct” data type for the COFFEE

− Then we will install The Doubly Linked List’s functions : 1. Initializing the Doubly Linked List :

− By assigning NODE Head and NODE Tail equals NULL, we have finished

</div><span class="text_page_counter">Trang 4</span><div class="page_container" data-page="4">

2. Creating new NODE :

3. Checking the list whether it is full or empty : bool isEmpty(DoublyLinkedList myList)

4. Showing the data of the list :

− By traversing the linked list, we have the value of it. We traverse from the first NODE (NODE Head) to the last NODE (NODE Tail) and get the data

− If we want to show a list, firstly we must have a function which shows the value or the data of a NODE. By doing that, our code is obvious and logical.

</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">

void showANode(NODE* X) {

cout << "The coffee name : " << X->data.name << endl;

cout << "The concentration : " << X->data.cafein << " %" << endl; cout << "The amount : " << X->data.count << endl;

cout << "The orginal : " << X->data.sourcecost << " VND" << endl; cout << "The agio : " << X->data.salecost << " VND" << endl; }

5. Insertion in Doubly Linked List : ❑<b> At the front of the Doubly Linked List : </b>

void addFirst(DoublyLinkedList& myList, COFFEE X)

</div><span class="text_page_counter">Trang 6</span><div class="page_container" data-page="6">

❑<b> At the end of the Doubly Linked List : </b>

void addLast(DoublyLinkedList& myList, COFFEE X)

</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">

❑<b> At the position of the Doubly Linked List : </b>

void addPosition(DoublyLinkedList& myList, int k, COFFEE X)

</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8">

6. Removing an element out of the list : ❑<b> At the front of the Doubly Linked List : </b>

void removeFisrt(DoublyLinkedList& myList)

</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">

❑<b> At the end of the Doubly Linked List : </b>

void removeLast(DoublyLinkedList& myList)

</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10">

❑<b> At the position of the Doubly Linked List : </b>

void removePosition(DoublyLinkedList& myList, int k)

❑<b> The NODE chosen </b>:

− We make full use of the remove at the front of the list and remove at the end of the list to install this function. If the NODE chosen is the first NODE of the Doubly Linked List we utilise removeFirst function, in case of it is the last NODE of the Doubly Linked List we can use removeLast function. When the NODE isn’t drop in 2 cases mentioned we will install as the diagram :

</div><span class="text_page_counter">Trang 11</span><div class="page_container" data-page="11">

void removeNode(DoublyLinkedList& myList, NODE* p)

7. Working with File handling :

− We have a file “COFFEE.txt”, and then we transfer the data from it to the Doubly Linked List through some procedures :

+ Step 1 : Read file.

+ Step 2 : Define the structure of the file. An element is defined by 5 atributtes : name, cafein, count, original cost and salecost.

+ Step 3 : Use the <b>addLast(DoublyLinkedList& myList) </b>function to insert an element respectively.

</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">

8. Searching by name of the Coffee product :

− By traversing the Doubly Linked List from the first NODE to the last NODE, if the program find the name of the Coffee we want to search, it return its data, In case of not being found, it returns the NULL value. NODE* searchByName(DoublyLinkedList myList, string s)

9. Seaching the List of low cost :

− We use the addLast() method to add an new item with satisfied requirement. In this case, if the cost's Coffee products less than k, return the List of them. DoublyLinkedList searchByLowCost(DoublyLinkedList myList, int k)

10. Sorting for data of the Doubly Linked List :

− In this report, we will install the Selection Sort and Quick Sort Algorithm for this problem. Two atributtes we choose to sort are : name’s and salecost’s products.

− Step 1 : Create and initialize new list named as newList.

</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">

− Step 2 : Use while-loop until my list is empty. − Step 2.1 : Assign NODE Head to NODE q.

− Step 2.2 : Traverse from the second NODE to the last NODE in the list. If the value (name or salecost) of NODE q is less than the element traversed, we will do the next step 2.3 and 2.4. Else we continue traversing to the last NODE.

− Step 2.3 : Use the add last fucntion (addLast(DoublyLinkedList& myList, COFFEE X)) to insert this element

− Step 2.4 : Use the remove node function ( void selectionSort(DoublyLinkedList myList)

− Step 1 : Seperate the Doubly Linked List into two smaller List.

− Step 2 : Check whether myList is empty or not . If it is empty return the program and do not anything.

− Step 3 : Choose the pivot item (It can be a NODE Head) and remove it from List.

− Step 4 : Traverse the Doubly Linked List, and add the item into two List initialized. Remember to remove it

− Step 5 : Use the Recursion Function for two List initialized. − Step 6 : Concatenate new List 1 - pivot - new List 2.

void quickSort(DoublyLinkedList& myList) {

DoublyLinkedList mynewList1, mynewList2; initList(mynewList1); initList(mynewList2); if (myList.Head == myList.Tail) return; NODE* pivot = createNode(myList.Head->data); NODE* p = myList.Head->next;

</div><span class="text_page_counter">Trang 17</span><div class="page_container" data-page="17">

void mergeList(DoublyLinkedList& X, DoublyLinkedList Y)

❑<b> Filtering the low count coffee products : </b>

void removeLowCount(DoublyLinkedList& myList)

− Do you wonder if the customer want to find the cheap products ? To solve this problem we have a fucntion filtering these products to a list, and it structure is the same as the Doubly Linked List. We use the add last function (addLast(DoublyLinkedList& myList, COFFEE X)) to insert the elements meet the require. In this case it is less than <b>k </b>(the cost we choose to compare).

</div><span class="text_page_counter">Trang 18</span><div class="page_container" data-page="18">

DoublyLinkedList requestByCost(DoublyLinkedList myList, long k)

− Instead of using "Struct" as C++, in C# - an oriented programming language, we create classes like each of struct.

❖ Build Class COFFEE :

public class COFFEE {

public string name; public double cafein; public int count; public int original_cost; public int sale_cost; }

❖ Build Class NODE :

public class NODE {

public COFFEE data; public NODE prev; public NODE next; }

❖ Build Class Doubly Linked List :

− We just transfer the code in C++ into C# language by fixing grammar error. public class DoublyLinkedList

{

public NODE Head; public NODE Tail;

</div><span class="text_page_counter">Trang 19</span><div class="page_container" data-page="19">

❑<b> Create new Node : </b>

public NODE createNode(COFFEE X)

❑<b> Check List is empty or full : </b>

public bool isEmpty()

❑<b> Add a new item : </b>

public void addFirst(COFFEE X)

</div><span class="text_page_counter">Trang 20</span><div class="page_container" data-page="20">

❑<b> Remove an old item : </b>

public void removeFisrt()

</div><span class="text_page_counter">Trang 23</span><div class="page_container" data-page="23">

public void quickSort() {

DoublyLinkedList mynewList1 = new DoublyLinkedList(); DoublyLinkedList mynewList2 = new DoublyLinkedList(); mynewList1.initList();

mynewList2.initList();

if (this.Head == this.Tail) return; NODE pivot = createNode(this.Head.data);

</div><span class="text_page_counter">Trang 25</span><div class="page_container" data-page="25">

❑<b> Initializing the Doubly Linked List and Getting String Connection : </b>

string strCon = @"Data Source=MSI;Initial Catalog = Coffee;Integrated Security=True";

DoublyLinkedList myList = new DoublyLinkedList(); COFFEE tmp = new COFFEE();

SqlConnection sqlCon = null;

❑<b> Constructor : </b>

public Form1() {

InitializeComponent();

</div><span class="text_page_counter">Trang 26</span><div class="page_container" data-page="26">

❑<b> Creating Text Box Number 0 : </b>

private void name_Enter(object sender, KeyEventArgs e) {

if (e.KeyData == Keys.Enter) textBox2.Focus(); }

❑<b> Creating Text Box Number1 : </b>

private void cafein_Enter(object sender, KeyEventArgs e) {

if (e.KeyData == Keys.Enter) textBox3.Focus(); }

❑<b> Creating Text Box Number 2 : </b>

private void count_Enter(object sender, KeyEventArgs e) {

try {

if (e.KeyData == Keys.Enter)

</div><span class="text_page_counter">Trang 27</span><div class="page_container" data-page="27">

textBox4.Focus(); }

catch(FormatException ex) {

string notify = ex.Message;

MessageBox.Show("The error : " + notify, "System Announcement",MessageBoxButtons.RetryCancel); }

❑<b> Creating Text Box Number 3 : </b>

private void original_Enter(object sender, KeyEventArgs e)

string notify = ex.Message;

MessageBox.Show("The error : " + notify, "System Announcement", MessageBoxButtons.RetryCancel); }

❑<b> Creating Text Box Number 4 : </b>

private void sale_Enter(object sender, KeyEventArgs e)

DialogResult result = MessageBox.Show("Do you want to save ?", "System Announcement", MessageBoxButtons.YesNo); if(result == DialogResult.No)

</div><span class="text_page_counter">Trang 28</span><div class="page_container" data-page="28">

string notify = ex.Message;

MessageBox.Show("The error : " + notify, "System Announcement", MessageBoxButtons.RetryCancel);

} }

❑<b> Creating Add First Button : </b>

private void addFirstClick(object sender, EventArgs e) {

if(textBox1.Text!=""&&textBox2.Text!=""&& textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "")

❑<b> Creating Add Last Button : </b>

private void addLastClick(object sender, EventArgs e) {

if(textBox1.Text!=""&&textBox2.Text!=""&& textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "")

</div><span class="text_page_counter">Trang 29</span><div class="page_container" data-page="29">

❑<b> Creating Refresh Button : </b>

− When we click it, the concepts in listBox1 will be deleted. private void refresh_Click(object sender, MouseEventArgs e)

❑<b> Creating Remove First method in MenuStrip : </b>

private void removeFirst_Click(object sender, EventArgs e)

❑<b> Creating Remove Last method in MenuStrip : </b>

private void removeLast_Click(object sender, EventArgs e)

</div><span class="text_page_counter">Trang 30</span><div class="page_container" data-page="30">

} }

❑<b> Creating Search method : </b>

− Using the KeyDown Event to search the information of coffee. If it does not exist, create a MessageBox with the text : "There are no result !".

private void search_KeyDown(object sender, KeyEventArgs e)

❑<b> Creating Search low cost Coffee products : </b>

private void cheapProducts_Click(object sender, EventArgs e) {

</div><span class="text_page_counter">Trang 31</span><div class="page_container" data-page="31">

DoublyLinkedList result = myList.searchByLowCost(35000);

❑<b> Creating Sort by Name in MenuStrip : </b>

private void sort_Name(object sender, EventArgs e)

</div><span class="text_page_counter">Trang 32</span><div class="page_container" data-page="32">

❑<b> Creating Sort by Sale Cost in MenuStrip : </b>

private void sortSaleCost_Click(object sender, EventArgs e) {

for (int i = 0; i < listView1.Items.Count; i++) listView1.Items[i].Remove();

DoublyLinkedList result = myList.selectionSort(myList); for (NODE p = result.Head; p != null; p = p.next)

</div><span class="text_page_counter">Trang 33</span><div class="page_container" data-page="33">

❑<b> Creating Show method in MenuStrip : </b>

private void showToolStripMenuItem_Click(object sender, EventArgs e)

❑<b> Creating Close method in MenuStrip : </b>

private void close_Click(object sender, EventArgs e) {

this.Close();

</div><span class="text_page_counter">Trang 34</span><div class="page_container" data-page="34">

− In this report we only choose STACK or QUEUE to install and build the

<b>program from its application. We will build a game “MINESWEEPER” </b>

by C Sharp Window Form (.NET) to be friendly with users.

− A Queue is defined as a linear Data Structure that is open at both ends and the operations are performed in First In First Out (FIFO) order.

− We define a <b>QUEUE</b> to be a list in which all additions to the list are made at one end, and all deletions from the list are made at the other end. The element which is first pushed into the order, the operation is first performed on that.

− Installing by C Sharp language : As a Doubly Linked List, we will do 2 main tasks.

+ Step 1 : We build a class NODE to store two attributes : <b><data_type> data</b>

and <b>NODE next</b>.

+ Step 2 : We build a class QUEUE includes 2 NODES : <b>NODE front</b>

(NODE Head) and <b>NODE rear</b> (NODE Tail) and write its methods. Its main methods have : Enqueue(), Dequeue() and Front().

▪ Enqueue method : It is the same as addLast() function.

▪ Dequeue method : It is the same as removeFirst() function and get the value of NODE front.

▪ Front : Get the value of NODE front but not removing.

❖ Build Class NODE :

<b>public class NODE </b>

public int data; public NODE next;

</div><span class="text_page_counter">Trang 35</span><div class="page_container" data-page="35">

❑<b> Checking the QUEUE whether is empty or not : </b>

public bool isEmpty()

</div><span class="text_page_counter">Trang 36</span><div class="page_container" data-page="36">

<b>− Advanced by .NET Winform : MINESWEEPER is a matrix including many </b>

buttons. So we will build a class ButtonBoom inherits from class Button. We consider a matrix is a two-dimension ButtonBoom Array . In this game we choose matrix’s size [23,23] to play, because it fits the Form1 builded.

− Class Form1 includes : + Matrix of ButtomBoom.

static ButtonBoom[,] mybtn = new ButtonBoom[23, 23]; + Constructor to create Form.

public Form1() {

InitializeComponent(); }

− Class ButtomBoom includes 5 atributtes :

+ bool isBoom = false : to check the buttom whether is boom or not and its default value is false, after that we will build a method to drop some booms randomly.

<b>• True : if it is boom. • False : if it is not boom. </b>

+ int state : to point the buttom whether is opened or not.

<b>• Value 0 : is not opened. • Value 1 : is opened.</b>

+ int boom_arround; to count the surroundings boom. + int i,j : to point the coordinate in matrix or index of button.

❖ Buid Class Form1 :

</div><span class="text_page_counter">Trang 37</span><div class="page_container" data-page="37">

❑<b> Close Button method : </b>

− Design Close Button Image : Choose Properties, then set an image

❑<b> Play Button method : </b>

private void startButton_Click(object sender, EventArgs e) {

setButtonArr(); }

❑<b> Set Button Matrix method : </b>

private void setButtonArr()

</div><span class="text_page_counter">Trang 38</span><div class="page_container" data-page="38">

mybtn[i, j].Size = new Size(40, 40); mybtn[i, j].BackColor = Color.Gray; mybtn[i, j].Top = top;

mybtn[i, j].Left = left;

❑<b> Creating Boom method : </b>

− To drop boom randomly, a number of boom are 80. We choose modulo operation to distribute evenly. When it is chosen a boom, assign isBoom

</div><span class="text_page_counter">Trang 39</span><div class="page_container" data-page="39">

} } }

❑<b> Count Boom surroundings method : </b>

private void countBoomArround() for (int y = j - 1; y <= j + 1; y++)

if ((x < 23 && y < 23) &&(x >= 0 && y >= 0)&&

public bool isBoom = false; public int i,j;

public int boom_arround; public int state;

public void BFS(ButtonBoom bt) {

int[] dx = new int[8] { -1, -1, -1, 0, 0, 1, 1, 1 }; int[] dy = new int[8] { -1, 0, 1, -1, 1, -1, 0, 1 }; QUEUE q = new QUEUE();

</div><span class="text_page_counter">Trang 40</span><div class="page_container" data-page="40">

int j1 = J + dy[k];

if (i1 >= 0 && i1 < 23 && j1 >= 0 && j1 < 23 && Form1.mybtn[i1, j1].boom_arround == 0 && Form1.mybtn[i1, j1].state == 0)

− Firstly, we must draw a number of button to show how many are boom surroundings it. We have 8 positions surroundings one button, so its value is from 0 to 8.

− Secondly, we must show boom or mine by an image to represent boom. I have a picture .PNG named “Bomb.PNG” to represent a boom. When click a button has boom ,it shows an icon ( ) .

private void Zero()

</div><span class="text_page_counter">Trang 41</span><div class="page_container" data-page="41">

this.Font = new Font("Microsoft JhengHei UI", 11);

</div><span class="text_page_counter">Trang 42</span><div class="page_container" data-page="42">

❑<b> Event methods : </b>

− When we open a button, we must assign the value of its state equal 1 to represent it has just been opened. If it is boom, we call Boom() function and do some procedures. In case of not being boom, we show the number of boom surroundings it.

public void Open()

else if (boom_arround == 0) Zero(); else if (boom_arround == 1) One(); else if (boom_arround == 2) Two(); else if (boom_arround == 3) Three(); else if (boom_arround == 4) Four(); else if (boom_arround == 5) Five(); else if (boom_arround == 6) Six(); else if (boom_arround == 7) Seven(); else if (boom_arround == 8) Eight(); }

− Creating Mouse_Click Event, when we click a button, we have 2 situations : its state equals 0 or 1. If its state is 1 we return; and do not anything. When its state equals 0, check its boom surroundings whether is 0 or not 0. If the value is 0, call Open() and BFS() function. In case of not 0, we only call

</div>

×