| Đăng nhập qua LiketLy 

trả lời
  • Câu trả lời được chấp nhận

    cái này không có ai rảnh để ngồi viết cho bạn đâu. theo mình thì thế này

    1.Tạo class Node

    class Node

    {
    puplic:

    *Node next;

    char[] tenhang;

    long dongia;

    uint soluong;

    }

    sau đó tạo list theo mẫu sau:

    CODE LIST

    #include<stdio.h>

    #include<conio.h>

    #include<string.h>

    typedef struct

    {
    char ID[9];

    char Name[30];

    float Mark;

    }Data;

    typedef struct tagNODE

    {
    Data info;

    tagNODE*pNext;

    }NODE;

    typedef struct

    {
    NODE *pHead;

    NODE *pTail;

    }LIST;

    void CreateList(LIST &l)

    {

    l.pHead=NULL;

    l.pTail=NULL;

    }
    NODE* CreateNode(Data x)

    { NODE *p= new NODE;

    if ( p!=NULL)

    {

    p ->info = x;

    p->pNext = NULL;

    }

    return p;

    }
    //-------------------------------------------------------------------------------------------------

    //Input

    void InputProgram(Data &x)

    {
    float temp;

    printf("Student's ID ";);

    fflush(stdin);

    gets(x.ID);

    printf("Name ";);

    fflush(stdin);

    gets(x.Name);

    printf("Mark ";);

    fflush(stdin);

    scanf("%f",&temp);

    x.Mark=temp;

    }
    void AddHead(LIST &l,Data &x)

    {
    InputProgram(x);

    NODE* p = CreateNode(x);

    if (l.pHead==NULL)

    {

    l.pHead = p;

    l.pTail = l.pHead;

    }

    else

    {

    p->pNext = l.pHead;

    l.pHead = p;

    }

    }
    void AddTail(LIST &l,Data &x)

    {
    InputProgram(x);

    NODE* p = CreateNode(x);

    if(l.pHead==NULL)

    l.pHead=l.pTail=p;

    else

    {

    l.pTail->pNext=p;

    l.pTail=p;

    }

    }
    //-------------------------------------------------------------------------------------------------

    //Remove

    void RemoveHead(LIST &l)

    {
    if(l.pHead==NULL)

    {

    printf("Erorr... No element!";);

    return;

    }

    NODE*p=l.pHead;

    l.pHead=p->pNext;

    delete p;

    if(l.pHead==NULL)

    l.pTail=NULL;

    }
    void RemoveTail(LIST &l)

    {
    NODE*p=l.pHead,*q;

    while(p->pNext!=NULL)

    {

    q=p;

    p=p->pNext;

    }

    l.pTail=q;

    q->pNext=NULL;

    delete p;

    }

    void RemoveX(LIST &l, Data &x)

    {
    printf("Please write student's ID you want to remove ";);

    fflush(stdin);gets(x.ID); //scanf("%s",&x.ID);

    NODE*p=l.pHead,*q=NULL;

    while((p!=NULL)&&(strcmp(p->info.ID,x.ID)))

    {

    q=p;

    p=p->pNext;

    }

    if(p==NULL)

    {

    printf("Sorry. Not found!\n";);

    return;

    }

    if(q==NULL)

    RemoveHead(l);

    else

    {

    q->pNext=p->pNext;

    delete p;

    if(l.pHead==NULL)

    l.pTail=NULL;

    }

    printf("Complete...\n";);

    }

    //-------------------------------------------------------------------------------------------------

    //Output

    void PrintList(LIST l)

    {
    NODE *p=l.pHead;

    printf(" THE LINK LIST OF STUDENTS UIT \n\

    ID Name Mark\n";);

    while(p!=NULL)

    {

    printf("%-10s%-20s%-.2f\n", p->info.ID,p->info.Name,p->info.Mark);

    p=p->pNext;

    }

    }
    //--------------------------------------------------------------------------------------------------

    //Search

    void Search(LIST l, Data &x)

    {
    printf("Enter the student's ID you want to search ";);

    fflush(stdin);

    gets(x.ID);

    for(NODE*p=l.pHead;(p!=NULL)&&(strcmp(p->info.ID,x.ID));p=p->pNext);

    if(p==NULL)

    printf("Sorry. Not found!\n";);

    else

    printf("Search result :\n\

    ID Name Mark\n\

    %-10s%-20s%-.2f\n", p->info.ID,p->info.Name,p->info.Mark);

    }

    //-------------------------------------------------------------------------------------------------

    //Menu

    void menu()

    {
    printf("\n***************\n\

    Menu Programs:\n\

    0. Exit\n\

    1. Add a student in to the head of link list\n\

    2. Add a student in to the tail of link list\n\

    3. Remove a student from the head of link list\n\

    4. Remove a student from the tail of link list\n\

    5. Remove student x from the link list\n\

    6. Output\n\

    7. Search a student in the link list\n";);

    }
    //---------------------------------------------------------------------------------------------------

    void main()

    {


    LIST l;

    Data x;

    CreateList(l);

    int c;

    do

    {

    menu();

    printf("What do you choose? ";);

    scanf("%d",&c);

    switch(c)

    {

    case 0:return;

    case 1:AddHead(l,x);break;

    case 2:AddTail(l,x);break;

    case 3:RemoveHead(l);

    printf("Complete...\n";);

    break;

    case 4:RemoveTail(l);

    printf("Complete...\n";);

    break;

    case 5:RemoveX(l,x);break;

    case 6:PrintList(l);break;

    case 7:Search(l,x);break;

    }

    }

    while(1);

    getch();

    }

    Trả lời bởi meokonk
    ăm trước
    0 0
Những câu hỏi tương tự
Câu hỏi từ Thể loại Lập trình - Coding