Cai Dat Danh Sach Lien Ket Vong
// CaiDat_DSLK_Vong.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef struct node
{
int info;
node *link;
}node;
node *fist;
void xuat()
{
node *p;
p=fist;
if(p!=NULL)
{
do{
cout<<p->info<<endl;
p=p->link;
}while(p!=fist);
}
else
cout<<"\n Khong co phan tu \n";
}
void rong()
{
fist=NULL;
}
void themdau(int x)
{
node *p,*q;
p=new node;
p->info=x;
q=fist;
if(fist!=NULL)
p->link=fist;
else
p->link=p;
if(q!=NULL)
{
while(q->link!=fist)
{
q=q->link;
}
q->link=p;
}
fist=p;
}
void xoadau(int &x)
{
node*p,*q;
p=fist;
q=fist->link;
if(p->link==fist)
{
delete p;
fist=NULL;
}
else
{
while(q->link!=fist)
{
q=q->link;
}
x=p->info;
fist=fist->link;
delete p;
}
q->link=fist;
}
void themcuoi(int x)
{
node*p,*q;
p=new node;
p->info=x;
q=fist;
if(fist==NULL)
{
p->link=fist;
}
else
{
while(q->link!=fist)
{
q=q->link;
}
q->link=p;
}
p->link=fist;
}
void xoacuoi(int &x)
{
node*p,*q;
q=fist;
if(fist==NULL)
{
cout<<"\n Khong co phan tu de xoa \n";
}
else if(q->link==fist)
{
delete q;
fist=NULL;
}
else
{
while(q->link!=fist)
{
p=q;
q=q->link;
}
x=q->info;
delete q;
p->link=fist;
}
}
void tim(int &x)
{
node*p,*q;
q=fist;
do
{
if(q->info!=x)
{
p=q;
q=q->link;
}
else
break;
}while(q->link!=fist);
if(q==fist)
{
xoadau(x);
}
else
{
x=q->info;
p->link=q->link;
delete q;
}
}
void main()
{
rong();
int chon = -1,x;
printf("\n CHON THAO TAC THUC HIEN : \n");
while(chon!= 0)
{
cout << "1.Xuat"<<endl;
cout << "2.Them phan tu vao dau danh sach" <<endl;
cout << "3.Xoa phan tu dau danh sach" <<endl;
cout << "4.Them phan tu cuoi danh sach" <<endl;
cout << "5.Xoa phan tu cuoi danh sach" <<endl;
cout << "6.Tim va xoa" <<endl;
cout<<" Chon : ";
cin >> chon;
switch(chon)
{
case 1:
xuat();
break;
case 2: cout<<" Nhap phan tu can them : ";
cin>>x;
themdau(x);
break;
case 3:
xoadau(x);
break;
case 4:cout<<" Nhap phan tu can them : ";
cin>>x;
themcuoi(x);
break;
case 5:
xoacuoi(x);
break;
case 6:cout<<" Nhap phan tu can tim : ";
cin>>x;
tim(x);
break;
}
}
}
0 nhận xét:
Đăng nhận xét