PHP Code:
#include"conio.h"
#include"stdio.h"
typedef struct tagNode
{
int info;
struct tagNode *pNext;
}Node;
typedef struct tagList
{
Node *pHead;
Node *pTail;
}List;
void createList(List &l)
{
l.pHead=l.pTail=NULL;
}
Node* getNode(int x)
{
Node* p;
p=new Node;
if(p==NULL)return 0;
else
{
p->info=x;
p->pNext=NULL;
}
return p;
}
void addTail(List &l,Node *p)
{
if(l.pHead==NULL)
{
l.pHead=p;
l.pTail=l.pHead;
}
else
{
l.pTail->pNext=p;
l.pTail=p;
}
}
void addHead(List &l,Node *p)
{
if(l.pHead==NULL)
{
l.pHead=p;
l.pTail=l.pHead;
}
else
{
p->pNext=l.pHead;
l.pHead=p;
}
}
void inds(List l)
{
Node* p;
p=l.pHead;
while(p)
{
printf("%d ",p->info);
p=p->pNext;
}
}
int deleteHead(List &l)
{
int x;
Node *p;
p=l.pHead;
x=p->info;
if(p!=NULL)
{
l.pHead=l.pHead->pNext;
delete(p);
if(l.pHead==NULL)
l.pTail=NULL;
return 1;
}
return 0;
}
void deleteTail(List &l)
{
Node *p,*q;
p=l.pHead;
while(p!=l.pTail)
{
q=p;
p=p->pNext;
}
if(p==l.pHead)
deleteHead(l);
else
{
l.pTail=q;
q->pNext=NULL;
delete(p);
}
}
Node* seachx(List l,int x)
{
Node *p;
p=l.pHead;
while((p!=NULL)&&(p->info!=x))
p=p->pNext;
return p;
}
int timx(List l,int x)
{
Node *p;
p=l.pHead;
while((p!=NULL)&&(p->info==x))
p=p->pNext;
if(p==NULL)return 0;
else
return 1;
}
void insertafterq(List &l,Node *p,Node *q)
{
if(q!=NULL)
{
p->pNext=q->pNext;
q->pNext=p;
if(l.pTail==q)
l.pTail=q;
}
else
addHead(l,q);
}
void dao(List &l)
{
int x;
Node *p,*k;
List q;
createList(q);
p=l.pHead;
do
{
x=p->info;
k=(getNode(x));
addHead(q,k);
p=p->pNext;
}while(p!=NULL);
createList(l);
p=q.pHead;
do
{
x=p->info;
k=(getNode(x));
addTail(l,k);
p=p->pNext;
}while(p!=NULL);
}
void sx(List &l)
{
Node *p,*k;
p=l.pHead;
while(p!=NULL)
{
k=p->pNext;
while(k!=NULL)
{
if(k->info<p->info) {Hoan(k->info,p->info);}
k=k->pNext;
}
p=p->pNext;
}
}
void Hoan(int &a,int &b)
{
int c;
c=a;
a=b;
b=c;
}
int main()
{
List l;
int x,a;
Node *p;
createList(l);
do
{
printf("nhap phan tu x \n");
scanf("%d",&x);
if(x==0)break;
p=(getNode(x));
addTail(l,p);
}while(1);
inds(l);
sx(l);
inds(l);
/**printf("nhap so x muon chen sau no \n");
scanf("%d",&x);
printf("nhap so can chen a \n");
scanf("%d",&a);
insertafterq(l,getNode(a),seachx(l,x));
inds(l);
printf("\n");
deleteHead(l);
inds(l);
printf("\n");
deleteTail(l);
inds(l);
printf("\n");**/
getch();
return 0;
}
mấy anh xem giúp em hàm sx nha nó báo lổi "nhận dạng không tìm thấy" ở hàm hoan();
Comment