01/10/2018, 12:08

Các lệnh trong vòng lặp for không thực thi?

Đây là vòng lặp for bị lỗi


-Nó không in ra danh sách sau khi thêm ạ! nó chỉ in ra danh sách ban đầu ạ!

Đây là code ạ

#include<iostream>
using namespace std;
struct node
{
	int data;
	node *pnext;
};
typedef node N;
struct list
{
	N *phead;
	N *ptail;
};
typedef list L;
void creatlist(L &l)
{
	l.phead = NULL;
	l.ptail = NULL;
}
N *creatnode(int x)
{
	N *p = new N;
	if (p == NULL)
	{
		cout << "khong du bo nho de cap phat"; return NULL;
	}
	p->data = x;
	p->pnext = NULL;
	return p;
}
void addfirst(L &l, N*p)
{
	if (l.phead == NULL)
	{
		l.phead = l.ptail = p;
	}
	else
	{
		p->pnext = l.phead;
		l.phead = p;
	}
}
void addtail(L &l, N *p)
{
	if (l.phead == NULL)
	{
		l.phead = l.ptail = p;

	}
	else
	{
		l.ptail->pnext = p;
		l.ptail = p;
	}
}
void nhap(L &l)
{
	int n;
	cout << "
Nhap vao n node muon them:";
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		int x;
		cout << "
nhap x:"; cin >> x;
		N *q = creatnode(x);
		addtail(l, q);
	}
}
void xuat(L l)
{
	for (N *k = l.phead; k != NULL; k = k->pnext)
	{
		cout << k->data << "  ";
	}
}
void thembatki(L &l)
{
	int x;
	cout << "nhap du lieu cho node ban muon them:"; cin >> x;
	N *p = creatnode(x);
	int x1;
	cout << "
nhap du lieu ma ban mun them tai node nao"; cin >> x1;
	N *q = creatnode(x1);
	if (q == l.phead&&l.phead->pnext == NULL)
	{
		addtail(l, p);
	}
	else
	{
		for (N *k = l.phead; k != NULL; k = k->pnext)
		{
			if (q->data == k->data)
			{
				N *h = creatnode(x);
				N *g = k->pnext;	
				k->pnext = h; //1 2 4 2 5   69
				h->pnext = g;
			}	
		}
	}
	xuat(l);
}
void thembatki1(L &l)
{
	int x;
	cout << "nhap du lieu cho node ma ban muon them:"; cin >> x;
	N *p = creatnode(x);
	int x1;
	cout << "nhap thong tin node:"; cin >> x1;
	N *q = creatnode(x1);
	if (q == l.phead&&l.phead->pnext==NULL)
	{
		addfirst(l, p);
	}
	else
	{
		for (N *k = l.phead; k != NULL; k = k->pnext)
		{
			if (q->data == k->data)
			{
				N *g = creatnode(p->data);
				N *h = k;
				g->pnext = h;
				h = g;
				// 1 3 2 3 4 2 5  69
			}
		}
	}
	xuat(l);
}
int main()
{
	L l;
	creatlist(l);
	int luachon;
	do
	{
		cout << "nhap lua chon";
		cin >> luachon;
		if (luachon == 1) nhap(l);//nhap thong tin node
		else
		{
			if (luachon == 2) //xuất danh sach node
			{
				cout << "

	danh sach lien ket la:";
				xuat(l);
			}
			if (luachon==3) //them 1 node vao sau node có trong danh sach
			{
				thembatki(l);
			}
			if (luachon == 4) //them mot node vao truoc node co trong danh sach
			{
				thembatki1(l);
			}
		}
	} while (luachon != 0);
	system("pause");
	return 0;
}

Mong nọi người giúp đỡ! Em cảm ơn nhiều ạ!

Tao Không Ngu. viết 14:13 ngày 01/10/2018

HI Tran Thanh
Debug.

Tran Thanh viết 14:22 ngày 01/10/2018

da anh,em không hiểu anh ơi

lahata viết 14:20 ngày 01/10/2018

Vòng for thì đùng rồi, chỉ có chổ if hơi khó hiểu, nếu bạn muốn chèn 69 vào sau vi trí 2 thì sửa lại như thế này

if (p->data == k->data)
{
  N* g = creatnode(p->data);
  N* temp = k->pnext;
  k->pnext = g;
  g->pnext = temp;
}
Bài liên quan
0