Home > Back-end >  Do an algorithm with c
Do an algorithm with c

Time:09-23

Great god help him to do? ,
My task is the id number, name, school, degree
Request the following
Write a list class, which requires to achieve the basic operations of a singly linked list, written in MFC application using the list class to demonstrate the working principle of the list
Requirements:
Can achieve the list function
To realize the function to delete any node list
The data contains the data information of each node see "computer programming training task. XLS", on the case, including (student id, name, course name and grades);
Insert data can be in the correct position;
2. New project
1. Create a new project
2. As shown in figure 1, template choose "MFC application
"3. Select the appropriate location and name and click ok

Figure 1 choose template
3. The application types as shown in figure 2: based on the dialog box, use the MFC in the static library, do not use the Unicode library (remove the tick);

4. Select the "next step", the user interface function is shown in figure 3: minimize box, dialog box entitled LinkedList

5. Click "finish", will produce the following documents:

6. If you are using VC6, do not need this step, right-click on the project "LinkedList" point, select properties, modified c/c + + "precompiled header" instead of "do not use the precompiled header, as shown in figure 5, click ok,

7. Environment configuration detection:

Should be shown in the output window:

3. Write CLinkedList class
After completion of the work, in the source file to create a new file "CLinkedList. CPP," in the header file to create a new file "CLinkedList. J h,"
CLinkedList. H contains list class definition, and CLinkedList. CPP, the realization of the linked list class containing
Here provide a CLinkedList. H content example:
#include
using namespace std;

//define singly linked list class

The class CLinkedList
{
Public:
Struct student
{
Long sno.//the list an example, according to their own subject to define the actual
?????
};
Struct node
{
?????
};

Private://data members

????//head pointer
????//the length of the list

Public://a member function
CLinkedList ();//the constructor, the establishment of an empty list
~ CLinkedList ();
?????//insert a node
?????//delete elements
?????//retrieve the length of the list
Node * GetHeadAddress (CLinkedList: : node * * retHead=NULL);
};
After the statement CLinkedList h, need in CLinkedList. CPP implementation singly linked lists of all functions,
For example:
# include "stdafx. H"
# include "CLinkedList. H"


//set up empty list
CLinkedList: : CLinkedList () : the head (NULL), Length (0)
{
return;
}

CLinkedList: : ~ CLinkedList ()
{
Node * p=NULL;
While (the head)
{
??????????
??????????
??????????
??????????
}
return;
}



//calculate length of the list
Int CLinkedList: : GetLength ()
{
Return Length;
}


//insert the new element to the chain head
Int CLinkedList: : ins_linked_list (student x)
{
Node * p=NULL;
?????????? ;//apply for a new node storage
??????????

??????????
?????????? ;//fill in the new node data fields
??????????
??????????
??????????
??????????//fill in the new node pointer field, pointing to the original head node
??????????//head pointer to head a new node
return 1;
}



//delete a node
Int CLinkedList: : del_linked_list (x)
{
The node * p, * q;
Length -;

If (the head - & gt; Data. The sno==x)//remove the first node
{
??????????
??????????
??????????
??????????

}

Q=head;//remove the general node

??????????
??????????
??????????
return(1);
}

//get head node
CLinkedList: : node * CLinkedList: : GetHeadAddress (CLinkedList: : node * * HeadAddress)
{
If (HeadAddress)
* HeadAddress=head;
return head;
}


The rest of the classmates, please use the data structure of knowledge, the function is complete,

Compile testing:
Press Ctrl + F7 or choose to generate the menu? Compile, compile should be displayed in the output window
:
4. Design the user interface
As shown in figure

One use the List Control, and the View attribute is set to "report";
2-5, 6 is a few Edit Control, in addition to 6 Edit Control Read Only attribute needs to be set to "false",
7, 8 is Button
In 2-5 Edit Control respectively add right in front of the "static Text",

4.1 adding control variables:
Insert table 1
control variablesSerial number data type name category
1 CListCtrl oListList Control
2 long Dsno Value
3 cstrings Dname Value
4 cstrings Dsubject Value
5 int Dgrade Value
6 cstrings Dmessage Value
Please according to their own needs, flexible definition,
4.2 add event:
To add "event handler" control 7, 8, code don't add first.
Serial number data type name category
1 CListCtrl oListList Control
2 long Dsno Value
3 cstrings Dname Value
4 cstrings Dsubject Value
5 int Dgrade Value
6 cstrings Dmessage Value

Compile testing:
Press Ctrl + F7 or choose to generate the menu? Compile, compile should be displayed in the output window:

5. Write the code
First of all, for the list class LinkedListDlg. H add some members of the member variable and function:
(LinkedListDlg. H file should be # include "CLinkedList. H")
For example: this example in LinkedListDlg protected: add the following member variables:
CLinkedList LinkedList.
CListCtrl oListList;
Long Dsno;
Cstrings Dname;
Cstrings Dsubject;
Int Dgrade;
Cstrings Dmessage;
In this case: in public LinkedListDlg: add the following member functions:
Void SetDlgDataFromList ();
LinkedList is singly linked list class, SetDlgDataFromList function is set up according to the data in a linked list class dialog data of each control,
In "LinkedListDlg. CPP" find LinkedListDlg OnInitDialog function, inside to join some initialization code:
OListList. SetExtendedStyle (LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related