ai đi qua giúp tí ngôn ngữ C++ với

PhamHongViEHG

Junior
Joined
Jun 15, 2014
Messages
282
Reactions
269
MR
0.011
Facebook
Follow me on Facebook
Skype
Chat with me via Skype
không biết có nhâm gì không mà chạy được nhưng không đúng phần tìm vị trí... nhập sao nó cũng ra vị trí thứ 1
Code:
#include<conio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdio.h>
using namespace std;
typedef struct node
{
    int data;
    struct node*next;
}node;
typedef struct list
{
    node *phead;
    node *ptail;
}list;
void init(list &l);
node *createnode(int x);
void addhead(list &l,node *new_ele);
void addtail(list &l,node *new_ele);
void input(list &l);
void output(list l);
void themgiua(list &l,node *new_ele,int h);
int timvitri(list &l,int m);
int n;
int main ()
{
    int x,kt,m,k;
    list l;
    input(l);
    output(l);
    cout<<"\nnhap gia tri can chen vao dau: ";
    cin>>x;
    addhead(l,createnode(x));
    output(l);
    cout<<"\nnhap gia tri can chen vao cuoi: ";
    cin>>x;
    addtail(l,createnode(x));
    output(l);
    cout<<"\n nhap m: ";
    cin>>m;
    kt=timvitri(l,m);
    if (kt==0)
        cout<<"m khong xuat hien trong danh sach";
    else
        cout<<"m xuat hien trong danh sach o vi tri thu: "<<kt;
    cout<<"\n nhap k va m: ";
    cin>>k>>m;
    kt=timvitri(l,m);
    if (kt==0)
        cout<<"m khong xuat hien trong danh sach!";
    else
        themgiua(l,createnode(k),m);
    cout<<"sau khi chen k vao truoc m la: ";
    output(l);
    return 0;
}
void init(list &l)
{
    l.phead = l.ptail= NULL;
}
node* createnode(int x)
{
    node *p;
    p = new node;
    if (p == NULL)
        cout<<" cap phat bo nho khong du";
    p->data = x;
    p->next = NULL;
    return p;
}
void addtail(list &l,node *new_ele)
{
    if (l.phead== NULL)
        l.phead=l.ptail=new_ele;
    else
        {
        l.ptail->next=new_ele;
        l.ptail=new_ele;
        }
}
void addhead(list &l,node *new_ele)
{
    if (l.phead == NULL)
        l.phead = l.ptail= new_ele;
    else
        {
        new_ele->next =l.phead;
        l.phead =new_ele;
        }
}
void themgiua(list &l,node *new_ele,int h)
{
    node *p =l.phead;
    for (int i=1;i<h-1;i++)
        p=p->next;
    if (p==NULL)
        cout<<"danh sach ngan hon vi tri da chon";
    else
        {
        new_ele->next = p->next;
        p->next = new_ele;
        }
}
int timvitri(list &l,int m)
{
    int vt=0;
    while (l.phead !=NULL)
        vt++;
    return vt;
}
void input(list &l)
{
    int x,n;
    cout<<"nhap so luong node: ";
    cin>>n;
    init(l);
    for (int i=0;i<n;i++)
        {
        cout<<"nhap phan tu thu "<<i+1<<": ";
        cin>>x;
        node*p= createnode(x);
        addhead(l,p);
        }
}
void  output(list l)
{

/*    for (node* p = l.phead; p; p = p->next)
        cout << p->data << " --> ";
    cout << "NULL";*/
    while (l.phead !=NULL)
        {
        cout<<l.phead->data<<" -> ";
            l.phead=l.phead->next;
        }
        cout<<"NULL";

}
 
int timvitri(list &l,int m)
{
int vt=0;
while (l.phead !=NULL)
vt++;
return vt;
}
 
lên
 
up
 
up
 
PHP:
int timvitri(list &l, int m)
{
    int vt = 0;
    while (l.phead != NULL)
    {
        if(l.phead->data == m )
            return vt + 1;
        l.phead = l.phead->next;
        vt++;
  
    }
    return 0;
}
 
Last edited:
PHP:
int timvitri(list &l, int m)
{
    int vt = 0;
    while (l.phead != NULL)
    {
        if(l.phead->data == m )
            return vt + 1;
        l.phead = l.phead->next;
        vt++;
  
    }
    return vt;
}
chưa đúng lắm.. nó chỉ đúng khi nhập tìm giá trị có trong danh sách thôi à thôi.. 1 điều nữa là dùng cái này sao lại sai phần chèn k vào vị trí m nhỉ :(

bro giúp luôn với.tks

W2IHL0g.jpg
 
OK rồi đấy quê
chưa đúng lắm.. nó chỉ đúng khi nhập tìm giá trị có trong danh sách thôi à thôi.. 1 điều nữa là dùng cái này sao lại sai phần chèn k vào vị trí m nhỉ :(

bro giúp luôn với.tks

W2IHL0g.jpg
PHP:
int timvitri(list &l, int m)
{
    int vt = 0;
    while (l.phead != NULL)
    {
        if(l.phead->data == m )
            return vt + 1;
        l.phead = l.phead->next;
        vt++;

    }
    return 0;
}

Rồi nhé. Nhưng làm theo return thế này thì nếu nó có nhiều số giống nhau không xử lý được hết. Đang ăn cơm nên có gì post đề lên tí xem lại toàn bộ cho. :D
 
Thớt viết code "dơ" thế. Nên đọc qua clean code (mã sạch) nné thớt. Phần này mình học lâu rồi, cũng quên hết rồi, không giúp được.
 
OK rồi đấy quê

PHP:
int timvitri(list &l, int m)
{
    int vt = 0;
    while (l.phead != NULL)
    {
        if(l.phead->data == m )
            return vt + 1;
        l.phead = l.phead->next;
        vt++;

    }
    return 0;
}

Rồi nhé. Nhưng làm theo return thế này thì nếu nó có nhiều số giống nhau không xử lý được hết. Đang ăn cơm nên có gì post đề lên tí xem lại toàn bộ cho. :D
thấy cậu ghi code dưới giống code trên mà khác chi đâu... ăn cơm xong giúp giùm mình với tks hihi :D

dưới đây là đề:
FcWsqLh.jpg
 
Thớt viết code "dơ" thế. Nên đọc qua clean code (mã sạch) nné thớt. Phần này mình học lâu rồi, cũng quên hết rồi, không giúp được.
mới học mà trường mình dạy sao học vậy thôi chứ cũng chưa rành gì cả :(
 
Bạn là nam hay nữ, nếu là nữ thì mình giúp :3
 
thấy cậu ghi code dưới giống code trên mà khác chi đâu... ăn cơm xong giúp giùm mình với tks hihi :D

dưới đây là đề:
FcWsqLh.jpg
Giờ mới xong hết việc. tầm 30 phút sau có code nhé.
 
thấy cậu ghi code dưới giống code trên mà khác chi đâu... ăn cơm xong giúp giùm mình với tks hihi :D

dưới đây là đề:
FcWsqLh.jpg
PHP:
//------------------------------------------------------------------//
//Project: List                                                 //
//Author: Suong Mu                                        //
//Y!: imsuongmu                                            //
//Date: 29-04-2015                                         //
//------------------------------------------------------------------//

#include<iostream>
#include<conio.h>

using namespace std;

typedef struct node
{
    int item;
    node* next;
};

typedef node* listnode;

typedef struct
{
    listnode head;
    listnode tail;
}list;

//-------------------Function Of List-------------------//

//khoi tao list
void ListInit(list* l)
{
    l->head = NULL;
    l->tail = NULL;
}

//kiem tra rong
bool ListEmpty(list l)
{
    if (l.head == NULL)
        return true;
    return false;
}

//them mot phan tu vao dau danh sach
void PushTop(list* l, int x)
{
    listnode p = new node;
    p->item = x;
    p->next = l->head;
    l->head = p;
    /*neu danh sach rong thi phan tu dau trung phan tu cuoi*/
    if (ListEmpty(*l))
        l->tail = p;
}

//them mot phan tu vao cuoi danh sach
void PushBot(list* l, int x)
{
    listnode p = new node;
    p->item = x;
    p->next = NULL;
    /*neu danh sach rong thi phan tu dau trung phan tu cuoi*/
    if (ListEmpty(*l))
    {
        l->head = p;
        l->tail = p;
    } 
    else
    {
        l->tail->next = p;
        l->tail = p;
    }
     
}

//tim kiem vi tri dau tien xuat hien cua mot gia tri
int FindLocal(list l, int x)
{
    int local = 0;
    listnode p = l.head;
    while (p != NULL)
    {
        local++;
        if (p->item == x)
            return local;
        p = p->next;
    }
    return 0;
}

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

//-----------------Function Declaration-----------------//

//khoi tao du lieu
void init(list *l);

//hien thi danh sach
void print(list l);

//chen mot so vao truoc mot phan tu
void insert(list *l, int x, int m);
//------------------------------------------------------//

//---------------------Main Function--------------------//

int main()
{
    list* l = new list;
    int choice;
    int m, k;
    init(l);
    do
    {
        system("cls");
        cout << endl << "0. Thoat" << endl;
        cout << "1. Hien Thi" << endl;
        cout << "2. Them Mot Phan Tu Vao Dau DS" << endl;
        cout << "3. Them Mot Phan Tu Vao Cuoi DS" << endl;
        cout << "4. Tim Vi Tri Mot So" << endl;
        cout << "5. Chen Mot So Vao Truoc Mot Phan Tu" << endl;
        cout << endl << "Chon Chuc Nang: ";
        cin >> choice;
        switch (choice)
        {
        case 1:
            system("cls");
            cout << endl << " Danh Sach: ";
            print(*l);
            break;
        case 2:
            system("cls");
            cout << endl << " Phan tu can them vao dau sanh sach: ";
            cin >> m;
            PushTop(l, m);
            cout << endl << " Them Xong!";
            _getch();
            break;
        case 3:
            system("cls");
            cout << endl << " Phan tu can them vao cuoi sanh sach: ";
            cin >> m;
            PushBot(l, m);
            cout << endl << " Them Xong!";
            _getch();
            break;
        case 4:
            system("cls");
            cout << endl << " So can tim: ";
            cin >> m;
            cout << endl << " Vi tri dau tien tim thay: "<<  FindLocal(*l, m);
            _getch();
            break;
        case 5:
            system("cls");
            cout << " Nhap vao k va m: ";
            cin >> k >> m;
            insert(l, k , m);
            _getch();
            break;
        default:
            break;
        }
    } while (choice);
    system("pause");
    return 0;
}

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

//khoi tao du lieu
void init(list *l)
{
    int n, temp;
    ListInit(l);
    cout << endl << "\tKhoi Tao" << endl << endl;
    cout << "Nhap vao so nut cua danh sach: ";
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cout << endl << "Phan tu thu " << i << " = ";
        cin >> temp;
        PushBot(l, temp);
    }

    cout << endl << " Khoi Tao Xong!" << endl;
    _getch();
}


//hien thi danh sach
void print(list l)
{
    if (ListEmpty(l))
    {
        cout << " Danh Sach Rong !!!" << endl;
        _getch();
        return;
    }

    listnode p = l.head;
    while (p != NULL)
    {
        cout << p->item;
        p = p->next;
        if (p != NULL)
            cout << " <- ";
    }
    _getch();
}

//chen mot so vao truoc mot phan tu
void insert(list *l, int x, int m)
{
    if (FindLocal(*l, m) == 0)
    {
        cout << endl << " Khong ton tai " << m << " tron danh sach !!!" << endl;
        return;
    }
    listnode p = l->head;//bien luu nut hien tai
    listnode temp = p;//bien luu nut vua xet
    if (p->item == m)
        PushTop(l, x);
    p = p->next;
    while (p != NULL)
    {
        /*neu nut hien tai co gia tri bang m ta se them mot nut vao giua nut hien tai va nut vua xet*/
        if (p->item == m)
        {
            listnode k = new node;
            k->item = x;
            k->next = p;
            temp->next = k;
        }
        temp = p;
        p = p->next;
    }
    cout << endl << " Chen Thanh Cong!" << endl;
}
 
PHP:
//------------------------------------------------------------------//
//Project: List                                                 //
//Author: Suong Mu                                        //
//Y!: imsuongmu                                            //
//Date: 29-04-2015                                         //
//------------------------------------------------------------------//

#include<iostream>
#include<conio.h>

using namespace std;

typedef struct node
{
    int item;
    node* next;
};

typedef node* listnode;

typedef struct
{
    listnode head;
    listnode tail;
}list;

//-------------------Function Of List-------------------//

//khoi tao list
void ListInit(list* l)
{
    l->head = NULL;
    l->tail = NULL;
}

//kiem tra rong
bool ListEmpty(list l)
{
    if (l.head == NULL)
        return true;
    return false;
}

//them mot phan tu vao dau danh sach
void PushTop(list* l, int x)
{
    listnode p = new node;
    p->item = x;
    p->next = l->head;
    l->head = p;
    /*neu danh sach rong thi phan tu dau trung phan tu cuoi*/
    if (ListEmpty(*l))
        l->tail = p;
}

//them mot phan tu vao cuoi danh sach
void PushBot(list* l, int x)
{
    listnode p = new node;
    p->item = x;
    p->next = NULL;
    /*neu danh sach rong thi phan tu dau trung phan tu cuoi*/
    if (ListEmpty(*l))
    {
        l->head = p;
        l->tail = p;
    }
    else
    {
        l->tail->next = p;
        l->tail = p;
    }
    
}

//tim kiem vi tri dau tien xuat hien cua mot gia tri
int FindLocal(list l, int x)
{
    int local = 0;
    listnode p = l.head;
    while (p != NULL)
    {
        local++;
        if (p->item == x)
            return local;
        p = p->next;
    }
    return 0;
}

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

//-----------------Function Declaration-----------------//

//khoi tao du lieu
void init(list *l);

//hien thi danh sach
void print(list l);

//chen mot so vao truoc mot phan tu
void insert(list *l, int x, int m);
//------------------------------------------------------//

//---------------------Main Function--------------------//

int main()
{
    list* l = new list;
    int choice;
    int m, k;
    init(l);
    do
    {
        system("cls");
        cout << endl << "0. Thoat" << endl;
        cout << "1. Hien Thi" << endl;
        cout << "2. Them Mot Phan Tu Vao Dau DS" << endl;
        cout << "3. Them Mot Phan Tu Vao Cuoi DS" << endl;
        cout << "4. Tim Vi Tri Mot So" << endl;
        cout << "5. Chen Mot So Vao Truoc Mot Phan Tu" << endl;
        cout << endl << "Chon Chuc Nang: ";
        cin >> choice;
        switch (choice)
        {
        case 1:
            system("cls");
            cout << endl << " Danh Sach: ";
            print(*l);
            break;
        case 2:
            system("cls");
            cout << endl << " Phan tu can them vao dau sanh sach: ";
            cin >> m;
            PushTop(l, m);
            cout << endl << " Them Xong!";
            _getch();
            break;
        case 3:
            system("cls");
            cout << endl << " Phan tu can them vao cuoi sanh sach: ";
            cin >> m;
            PushBot(l, m);
            cout << endl << " Them Xong!";
            _getch();
            break;
        case 4:
            system("cls");
            cout << endl << " So can tim: ";
            cin >> m;
            cout << endl << " Vi tri dau tien tim thay: "<<  FindLocal(*l, m);
            _getch();
            break;
        case 5:
            system("cls");
            cout << " Nhap vao k va m: ";
            cin >> k >> m;
            insert(l, k , m);
            _getch();
            break;
        default:
            break;
        }
    } while (choice);
    system("pause");
    return 0;
}

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

//khoi tao du lieu
void init(list *l)
{
    int n, temp;
    ListInit(l);
    cout << endl << "\tKhoi Tao" << endl << endl;
    cout << "Nhap vao so nut cua danh sach: ";
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cout << endl << "Phan tu thu " << i << " = ";
        cin >> temp;
        PushBot(l, temp);
    }

    cout << endl << " Khoi Tao Xong!" << endl;
    _getch();
}


//hien thi danh sach
void print(list l)
{
    if (ListEmpty(l))
    {
        cout << " Danh Sach Rong !!!" << endl;
        _getch();
        return;
    }

    listnode p = l.head;
    while (p != NULL)
    {
        cout << p->item;
        p = p->next;
        if (p != NULL)
            cout << " <- ";
    }
    _getch();
}

//chen mot so vao truoc mot phan tu
void insert(list *l, int x, int m)
{
    if (FindLocal(*l, m) == 0)
    {
        cout << endl << " Khong ton tai " << m << " tron danh sach !!!" << endl;
        return;
    }
    listnode p = l->head;//bien luu nut hien tai
    listnode temp = p;//bien luu nut vua xet
    if (p->item == m)
        PushTop(l, x);
    p = p->next;
    while (p != NULL)
    {
        /*neu nut hien tai co gia tri bang m ta se them mot nut vao giua nut hien tai va nut vua xet*/
        if (p->item == m)
        {
            listnode k = new node;
            k->item = x;
            k->next = p;
            temp->next = k;
        }
        temp = p;
        p = p->next;
    }
    cout << endl << " Chen Thanh Cong!" << endl;
}
khá hay chạy chuẩn nhưng mình có thắc mắc.. hình như danh sách khi nhập phần tử 1,2,3,4
nó sẽ xuất ra là 4->3->2->1
chèn đầu sẽ là chèn trc con số 4: ví dụ chèn 5: 5->4->3->2->1

mình thấy cái cậu khi chạy nó như này nhập 1,2,3,4
xuất: 1->2->3->4
chèn 5 đầu thì nó lại chèn 5->1->2->3->4
 
PHP:
//------------------------------------------------------------------//
//Project: List                                                 //
//Author: Suong Mu                                        //
//Y!: imsuongmu                                            //
//Date: 29-04-2015                                         //
//------------------------------------------------------------------//

#include<iostream>
#include<conio.h>

using namespace std;

typedef struct node
{
    int item;
    node* next;
};

typedef node* listnode;

typedef struct
{
    listnode head;
    listnode tail;
}list;

//-------------------Function Of List-------------------//

//khoi tao list
void ListInit(list* l)
{
    l->head = NULL;
    l->tail = NULL;
}

//kiem tra rong
bool ListEmpty(list l)
{
    if (l.head == NULL)
        return true;
    return false;
}

//them mot phan tu vao dau danh sach
void PushTop(list* l, int x)
{
    listnode p = new node;
    p->item = x;
    p->next = l->head;
    l->head = p;
    /*neu danh sach rong thi phan tu dau trung phan tu cuoi*/
    if (ListEmpty(*l))
        l->tail = p;
}

//them mot phan tu vao cuoi danh sach
void PushBot(list* l, int x)
{
    listnode p = new node;
    p->item = x;
    p->next = NULL;
    /*neu danh sach rong thi phan tu dau trung phan tu cuoi*/
    if (ListEmpty(*l))
    {
        l->head = p;
        l->tail = p;
    }
    else
    {
        l->tail->next = p;
        l->tail = p;
    }
    
}

//tim kiem vi tri dau tien xuat hien cua mot gia tri
int FindLocal(list l, int x)
{
    int local = 0;
    listnode p = l.head;
    while (p != NULL)
    {
        local++;
        if (p->item == x)
            return local;
        p = p->next;
    }
    return 0;
}

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

//-----------------Function Declaration-----------------//

//khoi tao du lieu
void init(list *l);

//hien thi danh sach
void print(list l);

//chen mot so vao truoc mot phan tu
void insert(list *l, int x, int m);
//------------------------------------------------------//

//---------------------Main Function--------------------//

int main()
{
    list* l = new list;
    int choice;
    int m, k;
    init(l);
    do
    {
        system("cls");
        cout << endl << "0. Thoat" << endl;
        cout << "1. Hien Thi" << endl;
        cout << "2. Them Mot Phan Tu Vao Dau DS" << endl;
        cout << "3. Them Mot Phan Tu Vao Cuoi DS" << endl;
        cout << "4. Tim Vi Tri Mot So" << endl;
        cout << "5. Chen Mot So Vao Truoc Mot Phan Tu" << endl;
        cout << endl << "Chon Chuc Nang: ";
        cin >> choice;
        switch (choice)
        {
        case 1:
            system("cls");
            cout << endl << " Danh Sach: ";
            print(*l);
            break;
        case 2:
            system("cls");
            cout << endl << " Phan tu can them vao dau sanh sach: ";
            cin >> m;
            PushTop(l, m);
            cout << endl << " Them Xong!";
            _getch();
            break;
        case 3:
            system("cls");
            cout << endl << " Phan tu can them vao cuoi sanh sach: ";
            cin >> m;
            PushBot(l, m);
            cout << endl << " Them Xong!";
            _getch();
            break;
        case 4:
            system("cls");
            cout << endl << " So can tim: ";
            cin >> m;
            cout << endl << " Vi tri dau tien tim thay: "<<  FindLocal(*l, m);
            _getch();
            break;
        case 5:
            system("cls");
            cout << " Nhap vao k va m: ";
            cin >> k >> m;
            insert(l, k , m);
            _getch();
            break;
        default:
            break;
        }
    } while (choice);
    system("pause");
    return 0;
}

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

//khoi tao du lieu
void init(list *l)
{
    int n, temp;
    ListInit(l);
    cout << endl << "\tKhoi Tao" << endl << endl;
    cout << "Nhap vao so nut cua danh sach: ";
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cout << endl << "Phan tu thu " << i << " = ";
        cin >> temp;
        PushBot(l, temp);
    }

    cout << endl << " Khoi Tao Xong!" << endl;
    _getch();
}


//hien thi danh sach
void print(list l)
{
    if (ListEmpty(l))
    {
        cout << " Danh Sach Rong !!!" << endl;
        _getch();
        return;
    }

    listnode p = l.head;
    while (p != NULL)
    {
        cout << p->item;
        p = p->next;
        if (p != NULL)
            cout << " <- ";
    }
    _getch();
}

//chen mot so vao truoc mot phan tu
void insert(list *l, int x, int m)
{
    if (FindLocal(*l, m) == 0)
    {
        cout << endl << " Khong ton tai " << m << " tron danh sach !!!" << endl;
        return;
    }
    listnode p = l->head;//bien luu nut hien tai
    listnode temp = p;//bien luu nut vua xet
    if (p->item == m)
        PushTop(l, x);
    p = p->next;
    while (p != NULL)
    {
        /*neu nut hien tai co gia tri bang m ta se them mot nut vao giua nut hien tai va nut vua xet*/
        if (p->item == m)
        {
            listnode k = new node;
            k->item = x;
            k->next = p;
            temp->next = k;
        }
        temp = p;
        p = p->next;
    }
    cout << endl << " Chen Thanh Cong!" << endl;
}
à pro ơi có thể giúp mình sửa cái bài trên của mình dc ko... ko biết nó sai chỗ nào mà chạy ko đúng..xem giúp bài đó lần sau rút kinh nghiệm ^^
 
Gọi mình là bạn là được k thì bro cũng được trên mạng gọi thế nào cũng được nhưng đừng gọi pro ngại. chả giám nhận.


PHP:
int timvitri(list &l,int m)
{
    int vt=0;
    node * p = l.phead;
    while (p !=NULL)
    {
        vt++;
        if(p->data == m)
            return vt;
        p = p->next;
    }

    return 0;
}
 
int timvitri(list &l,int m)
{
int vt=0;
while (l.phead !=NULL)
vt++;
return vt;
}
trong hàm while bạn thêm vào điều kiện để tìm ra phần tử nữa, vòng while trên của bạn đơn thuần chỉ là duyệt qua các phần tử trong danh sách thôi.
 
Gọi mình là bạn là được k thì bro cũng được trên mạng gọi thế nào cũng được nhưng đừng gọi pro ngại. chả giám nhận.


PHP:
int timvitri(list &l,int m)
{
    int vt=0;
    node * p = l.phead;
    while (p !=NULL)
    {
        vt++;
        if(p->data == m)
            return vt;
        p = p->next;
    }

    return 0;
}
à bro mà ghi nhầm... bro là ng ae á mà :D tks cậu nhìu khi nào rảnh thì giúp mình vài bai :p
 

Announcements

Today's birthdays

Forum statistics

Threads
418,773
Messages
7,076,158
Members
170,863
Latest member
duy1326

Most viewed of week

Most viewed of week

Back
Top Bottom