01/10/2018, 14:59
Danh sách liên kết đơn bị break
Các bác ơi sao code của em cảm giác đúng hết mà sao nhập xuất xong bị break là sao ta??? à nếu em sử dụng hàm AddLast để tạo danh sách thì bị lỗi ngay hàm AddLast nữa, em không biết sao
// Bai1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
typedef struct node
{
int data;
node *pNext;
}NODE;
typedef struct
{
NODE *pHead;
NODE *pTail;
}LIST;
NODE* CreatNODE(int value)
{
NODE *p = new NODE;
if (p == NULL)
{
printf("Loi khong du bo nho");
exit(0);
}
p->data = value;
p->pNext = NULL;
return p;
}
NODE* AddFirst(LIST &D,int value)
{
NODE *p = CreatNODE(value);
if (D.pHead == NULL)
D.pHead = D.pTail = p;
p->pNext = D.pHead;
D.pHead = p;
return p;
}
NODE* AddLast(LIST &D, int value)
{
NODE *p = CreatNODE(value);
if (D.pHead == NULL)
D.pHead = D.pTail = p;
D.pTail->pNext = p;
D.pTail = p;
return p;
}
void CreatLIST(LIST &D, int &n)
{
printf("ListLength=");
scanf_s("%d", &n);
for (int i = 0; i < n; i++)
{
int value = 0;
printf("NODE %dth=", i);
scanf_s("%d", &value);
AddFirst(D, value);
}
}
void PrintLIST(LIST D)
{
NODE *p=D.pHead;
if (D.pHead == NULL)
printf("
Danh sach rong
");
else
{
for (p = D.pHead; p; p = p->pNext)
printf("%d ", p->data);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int n = 0;
int k = 0;
int x = 0;
NODE *pHead = NULL;
NODE *pTail = NULL;
LIST D;
CreatLIST(D, n);
PrintLIST(D);
return 0;
}
Bài liên quan
Lưu ý rằng thao tác trên node duy nhất là trường hợp đặc biệt.