1 Tổng hợp hàm trong DSLK Đơn, đôi, vòng, cây nhị phân tìm kiếm C++ Fri Oct 18, 2013 4:25 pm
DANH SÁCH LIÊN KẾT ĐƠN
+ Cấu trúc:
* Khởi tạo:
* Xóa 1 node đầu danh sách
+ Cấu trúc
* Tạo 1 node
* Xóa đầu
+ Cấu trúc:
- Code:
struct Node
{
int data;
Node *next;
};
typedef struct Node* List;
* Khởi tạo:
- Code:
List CreateNewNode(int data)
{
List temp=new Node();
if(temp==NULL)
{
cout<<"Khong du bo nho";
exit(0);
}
temp->data=data;
temp->next=NULL;
return temp;
}
- Code:
void AddNewNodeAtFirst(List newnode, List &pHead)
{
if(pHead==NULL) //Neu danh sach rong thi gan pHead=newnode
pHead=newnode;
else
{
newnode->next=pHead;
pHead=newnode;
}
}
- Code:
void AddNewNodeAtLast(List newnode, List &pHead)
{
if(pHead==NULL) //Neu danh sach rong thi gan pHead=newnode
pHead=newnode;
else
{
List temp=pHead;
while(temp->next!=NULL) //Duyet het danh sach tim Node Cuoi Cung
temp=temp->next;
temp->next=newnode;
}
}
- Code:
void AddNewNodeAtAfter(List nodetimthay,List newnode,List pHead)
{
while(pHead->next!=NULL)
pHead=pHead->next;
if(pHead->data==nodetimthay->data) //Neu Node la node cuoi cung thi Add vao cuoi
AddNewNodeAtLast(newnode,pHead);
else
{
newnode->next=nodetimthay->next;
nodetimthay->next=newnode;
}
}
* Xóa 1 node đầu danh sách
- Code:
void RemoveNodeAtFirst(List &pHead)
{
if(pHead==NULL)
cout<<"Danh Sach Rong";
else
{
List temp=pHead;
pHead=pHead->next;
delete temp;
}
}
- Code:
void RemoveNodeAtLast(List &pHead)
{
if(pHead==NULL)
cout<<"Danh Sach Rong";
else
{
List temp=pHead;
List Truoc;
while(temp->next!=NULL)
{
Truoc=temp;
temp=temp->next;
}
Truoc->next=NULL; //Gan Phan Tu Truoc Cuoi Bang NULL
delete temp; //Xoa Phan Tu Cuoi Di
}
}
- Code:
void RemoveNodeAny(List &pHead,int vitrixoa)
{
List temp=pHead;
List Truoc;
while(temp!=NULL && temp->data!=vitrixoa)
{
Truoc=temp;
temp=temp->next;
}
Truoc->next=Truoc->next->next;
delete temp;
}
- Code:
void RemoveAllList(List &pHead)
{
if(pHead==NULL)
cout<<"Danh sach rong";
else
{
while(pHead!=NULL)
RemoveNodeAtFirst(pHead);
cout<<"Da Xoa Toan Bo Danh Sach";
}
}
+ Cấu trúc
- Code:
struct Node
{
int data;
Node* next;
Node* pre;
};
typedef struct Node* list;
* Tạo 1 node
- Code:
list CreateNewNode(int data)
{
list temp=new Node();
if(temp==NULL)
{
cout<<"Khong du bo nho";
exit(0);
}
temp->data=data;
temp->next=NULL;
temp->pre=NULL;
return temp;
}
- Code:
void AddNewNodeAtFirst(list newnode,list &pHead,list &pTail)
{
if(pHead==NULL)
pHead=pTail=newnode;
else
{
newnode->next=pHead;
pHead->pre=newnode;
pHead=newnode;
}
}
- Code:
void AddNewNodeAtLast(list newnode,list &pHead,list &pTail)
{
if(pHead==NULL)
pHead=pTail=newnode;
else
{
pTail->next=newnode;
newnode->pre=pTail;
pTail=newnode;
}
}
- Code:
void AddNewNodeAfter(list &pHead,list &pTail,list newnode,list NodeTimThay)
{
if(NodeTimThay==NULL)
{
cout<<"Khong tim thay, Ban co muon them node nay vao dau danh sach(Y/N)";
char chon;
chon=getch();
if(chon=='Y' || chon=='y')
AddNewNodeAtFirst(newnode,pHead,pTail);
}
else
{
if(NodeTimThay->next==NULL)
AddNewNodeAtLast(newnode,pHead,pTail);
else
{
newnode->next=NodeTimThay->next;
NodeTimThay->next->pre=newnode;
NodeTimThay->next=newnode;
newnode->pre=NodeTimThay;
}
}
}
* Xóa đầu
- Code:
void RemoveNodeAtFirst(list &pHead,list &pTail)
{
if(pHead==NULL)
cout<<"Danh Sach Rong";
else
{
if(pHead==pTail)
{
delete pHead;
delete pTail;
pHead=pTail=NULL;
}
else
{
list temp=pHead;
pHead=pHead->next;
pHead->pre=NULL;
delete temp;
}
}
}
- Code:
void RemoveNodeAtLast(list &pHead,list &pTail)
{
if(pHead==NULL)
cout<<"Danh Sach Rong";
else
{
if(pHead==pTail)
{
delete pHead;
delete pTail;
pHead=pTail=NULL;
}
else
{
list temp=pTail;
pTail=pTail->pre;
pTail->next=NULL;
delete temp;
}
}
}
- Code:
void RemoveNodeAny(list &pTail,list &pHead,list NodeTimThay)
{
if(NodeTimThay->data==pTail->data)
RemoveNodeAtLast(pHead,pTail);
else
if(NodeTimThay->data==pHead->data)
RemoveNodeAtFirst(pHead,pTail);
else
{
list temp=NodeTimThay->pre;
temp->next=NodeTimThay->next;
delete NodeTimThay;
}
}