*
//Chuong trinh sau cho phep ban nhap n cuon sach voi thong tin nhu sau
/*
+Ten sach
+Ten nha xuat ban
*/
//Chuong trinh in ra cac dau sach co cung nha xuat ban
#include<iostream>
#include<string.h>
using namespace std;
//khai báo 1 node
struct node{
char tens[50];
char tennxb[50];
node*next;
};
struct dslk{
node* head;
node* tail;
};
//Thu tuc cho phep tao 1 node khi có gia tri x
//@param x la gia tri cua nut
//@return con tro tro den nut chua gia tri x
//Ham co 2 tham so truyen vao
// a= ten sach
// b= ten nha xuat ban
node* createNode(char *a, char *b){
node *p; p=new node;
if(p==NULL)return NULL;
strcpy(p->tens,a);
strcpy(p->tennxb,b);
p->next=NULL;
return p;
}
///Ham khoi tao 1 dslk
//@param l la danh sach can khoi tao
void init( dslk &l){
l.head=NULL;
l.tail=NULL;
}
//ham them 1 nut vao dau dslk
//param p la nut can them
void addFirst(dslk &l,node*p){
if(l.head==NULL)
l.head=l.tail=p;
else{
p->next=l.head;
l.head=p;
}
}
void addLast(dslk &l,node*p){
if(l.head==NULL)
l.head=l.tail=p;
else{
l.tail->next=p;
l.tail=p;
}
}
//chen node n vao sau node p trong danh sach
void addAfter(dslk&l,node *q,node*new_node){
if(l.head==NULL&&q==NULL)
l.head=l.tail=new_node;
if (q!=NULL){
new_node->next = q->next;
q->next = new_node;
if(q == l.tail)
l.tail = new_node;
}
}
//Ham nay dung de duyet qua danh sach va in gia tri data
//param l la danh sach can duyet
void traverse(dslk l){
node*t=l.head;
while(t!=NULL){
cout<<t->tens<<" ";
t=t->next;
}
cout<<endl;
}
//xoa 1 phan tu dau danh sach
int deleteFirst(dslk&l){
if(l.head==NULL)
return -1;
node*p=l.head;//phan tu can xoa
l.head=p->next;
if(l.head==NULL)
l.tail=NULL;
delete p;
return 1;
}
//Xoa 1 node sau node q
//tra ve 1 neu xoa thanh cong
int deleteAfter(dslk&l,node*q){
if(q==NULL || q->next==NULL)
return -1;
node*p=q->next;
q->next=p->next;
if(p==l.tail)
l.tail=q;
delete p;
return 1;
}
//xoa 1 nút có khóa là key
//tra ve -1 neu ds rong, 0 neu khong tim thay, 1 neu OK
int deleteByKey(dslk&l,char *tmp){
if(l.head==NULL) return -1;
node*sau=l.head;
node*truoc=NULL;
while(sau!=NULL){
if(strcmp(sau->tennxb,tmp)==0) break;
truoc=sau;
sau=sau->next;
}
if(sau==NULL)
return 0;
if(truoc==NULL)
return deleteFirst(l);
return deleteAfter(l,truoc);
}
int main(){
dslk l;
init(l);
int n;
char a[50],b[50];
cout<<"So cuon sach can nhap vao: ";
cin>>n;
cin.ignore(1);
//Nhap n cuon sach vao
for(int i =1;i<=n;++i){
cout<<endl<<"Nhap thong tin cuon sach thu: "<<i<<endl;
node *nod =new node;
cout<<endl<<"Nhap ten sach: ";cin.getline(a,50);
cout<<endl<<"Nhap ten nha xuat ban: ";cin.getline(b,50);
nod = createNode(a,b);
addLast(l,nod);
}
cout<<endl<<"In ra thong tin dau sach vua nhap:";
node *pdau = l.head;
while( pdau != NULL){
cout<<endl<<pdau->tens<<" "<<pdau->tennxb;
pdau = pdau->next;
}
char tmp[50];
while(l.head != NULL){
node* p = l.head->next;
strcpy(tmp,l.head->tennxb);
cout<<endl<<"==============================================";
cout<<endl<<"Nhung cuon sach co cung nha xuat ban: "<<tmp;
cout<<endl<<l.head->tens;
deleteByKey(l,tmp);
while( p!= NULL){
if(strcmp(tmp, p->tennxb)==0){
cout<<endl<<p->tens;
deleteByKey(l,tmp);
}
p = p->next;
}
}
cout<<endl;
system("pause");
return 0;
}
*
//cac_thao_tac_voi_stack.h
/*:
Khai bao kieu Stack
void Init_Stack(Stack &S); //Khoi tao Stack
bool IsEmpty_Stack(Stack S);//Kiem tra tinh rong cua Stack
void Push_Stack(Stack &S, int v); //Them mot node co gia tri v vao Stack
int Pop_Stack(Stack &S); //Xoa mot node cua Stack
void Print_Sack(const Stack &S);//Duyet toan bo stack va in chung
*/
//Chuong trinh sau chuyen mot so co so 10 sang co so 2 su dung Stack
#include<iostream>
using namespace std;
//Khai bao
typedef struct pStack
{
int Top;
pStack *Next;
}*Stack;
//Khoi tao Stack rong
void Init_Stack(Stack &S)
{
S = NULL;
}
bool IsEmpty_Stack(Stack S)
{
if(S == NULL)
return 1; //Neu Stack rong tra ve 1
return 0; //Nguoc lai tra ve 0
}
void Push_Stack(Stack &S, int v)
{
//Khai bao mot node moi
pStack *p;
p = new pStack; //Cap phat bo nho cho node moi
if(p==NULL)
{
std::cout<<"Memory Empty."; //Neu p rong, thong bao het bo nho
return;
}
p->Top = v;
p->Next = S; //Tao link lien ket tu p den Stack
S = p; //Bay gio p la Stack
}
int Pop_Stack(Stack &S)
{
pStack *p; //p de luu node bi xoa
if(IsEmpty_Stack(S))
{
return 0; //Neu Stack rong thi thoat ra
}
p = S; //p la S
S = p->Next; //S link toi node dang sau no
int v = p->Top; // v luu gia tri cua node bi xoa
delete(p); //Giai phong bo nho cho node bi xoa
return v; //Tra ve gia tri cua node bi xoa
}
void Print_Stack(const Stack &S)
{
pStack *p; //p tro vao node can in ra
p = S ;// p la S
while(!IsEmpty_Stack(p))//Trong khi p khong rong
{
std::cout<<" "<<p->Top;
p = p->Next; // p link toi node ke tiep
}
}
int main(){
int n;
cout<<"Nhap n ="; cin>>n;
//Chuyen mot so o he thap phan sang nhi phan va luu vao stack
Stack P;
Init_Stack(P);
//Ham chuyen doi co so
while(n>0){
Push_Stack(P,n%2);
n = n/2;
}
//In ra
Print_Stack(P);
cout<<endl;
system("pause");
return 0;
}
*
0 comments:
Post a Comment