Jump to content

Welcome to [ iT ] Forums
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. If you already have an account, login here - otherwise create an account for free today!
Hình ảnh

Mọi người giúp em bài danh sách liên kết vòng này với !!! HELP HELP HELP không biết bị lỗi gì em nhập vô danh sách mà nó xuất liên tục không ngừng :((


  • Please log in to reply
No replies to this topic

#1
quocdung1989

quocdung1989

    Thành viên mới

  • Newbies
  • 1 Bài Viết:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
//khai bao node
struct Node
{
int data;
Node *pNext;
};
//khai bao danh sach
struct List
{
Node*pHead;
Node*pTail;
};
// tao node
Node*getNode(int x)
{
Node *p;
p=new Node;//Cap Phat Vung Nho Cho Node
if(p==NULL)
{
cout<<"Khong Du Bo Nho!";
return NULL;
}
p->data=x;//gan du lieu cho phan tu p
p->pNext=NULL;
return p;
}
//tao danh sach rong
void Init(List &l)
{
l.pHead=l.pTail=NULL;
}

//Them dau danh sach
void addHead(List &l,Node*new_Node)
{
if(l.pHead==NULL)//neu phan tu dau rong
{
l.pHead=l.pTail=new_Node;//phan tu dau=phan tu cuoi=tao node
l.pTail->pNext=l.pHead;
}
else
{
new_Node->pNext=l.pHead;
l.pTail->pNext=new_Node;
l.pHead=new_Node;

}
}
//Them cuoi DS
void addTail(List &l,Node*new_Node)
{
if(l.pHead==NULL)
{
l.pHead=l.pTail=new_Node;
l.pTail->pNext=l.pHead;
}
else
{
new_Node->pNext=l.pHead;
l.pTail->pNext = new_Node;
l.pTail = new_Node ;
}
}
//Xoa Dau DS
int removeHead(List &l)
{
Node *p=l.pHead;
if(p==NULL) return 0;
if(l.pHead==l.pTail)
l.pHead=l.pTail=NULL;
else
l.pHead=p->pNext;
delete p;
return 1;
}
//Them Sau Q
void addAfter (List &l, Node *q, Node* new_Node)
{
if(l.pHead == NULL) //Xâu rong
{
l.pHead = l.pTail = new_Node;
l.pTail->pNext = l.pHead;
}
else
{
new_Node->pNext = q->pNext;
q->pNext = new_Node;
if(q == l.pTail)
l.pTail = new_Node;
}
}
//Xoa Node Sau Q
int removeAfter (List &l, Node *q )
{
Node *p;
if(q != NULL)
{
p = q ->pNext ;
if ( p == q) l.pHead = l.pTail = NULL;
else
{
q->pNext = p->pNext;
if(p == l.pTail)
l.pTail = q;
}
delete p;
}
}

//Ham Xuat Node
void XuatLK(List l)
{
Node *p=l.pHead;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->pNext;
}
cout<<endl;
}

//Ham main
int main()
{
int data,chon,x,k,n,Max;
List l;
Init(l);
Node *m;
Node *t,*q;
do
{ cout<<"1:Nhap Va Xuat Danh Sach"<<endl;
cout<<"2.Them Phan Tu Vao Cuoi Danh Sach"<<endl;
cout<<"3.Xoa Phan Tu Dau Danh Sach"<<endl;
cout<<"Nhap Chon"<<endl;
cin>>chon;
switch(chon)
{
case 1:
cout<<"Nhap So Lan Can Nhap";cin>>k;
for(int i=0;i<k;i++)
{
cout<<"Node=";cin>>x;
cout<<endl;
Node *p=getNode(x);
addHead(l,p);
}
XuatLK(l);
cout<<endl;
break;
case 2:
cout<<"Nhap So Lan Can Nhap";cin>>k;
for(int i=0;i<k;i++)
{
cout<<"Node=";cin>>x;
cout<<endl;
Node *p=getNode(x);
addTail(l,p);
}
XuatLK(l);
cout<<endl;
break;
}
}
while(chon!=0);
}




special thanks to our all time users for being with us

Calvin Music

Close [X]