danh sach lien ket don
// list.cpp : Defines the entry point for the console application.
//
#include <iostream.h>
#include <conio.h>
struct node{
int info;
node *next;
};
node *taonode(int x){
node *p;
p=new node;
p->info=x;
p->next=NULL;
return p;
}
void chendau(int x , node *&first){
node *p;
p=taonode(x);
if(first==NULL) first=p;
else{
p->next=first;
first=p;
}
}
void chencuoi(int x , node *&first){
node * p , *q;
p=taonode(x);
if(first==NULL) first=p;
else{
q=first;
while(q->next!=NULL){
q=q->next;
}
q->next=p;
}
}
void taoDSLK(node *&first){
int x ,n ;
cout<<"Nhap n:";cin>>n;
for(int i=1;i<=n;i++){
cout<<"nhap x:";
cin>>x;
chendau(x,first);
}
}
void taoDS(node *&first){
int x , n;
cout<<"Nhap n:";cin>>n;
for(int i=1;i<=n;i++){
cout<<"nhap x:";
cin>>x;
chencuoi(x,first);
}
}
void xuatDSLK(node *first){
node *r=first;
while(r!=NULL){
cout<<r->info<<"->";
r=r->next;
}
cout<<"NULL";
cout<<endl;
}
void dao2dau(node *&first){
node *r1=first->next ;
node *r2=r1->next;
if(r2==NULL){
r1->next=first;
first->next=NULL;
first=r1;
}
else{
first->next=r2;
r1->next=first;
first=r1;
}
}
void dao(node *&first)
{
node * p , *r ;
p=r=first;
if(first->next->next==NULL)
dao2dau(first);
else{
while(r->next!=NULL)
r=r->next;
while(p->next!=r)
p=p->next;
r->next=first->next;
p->next=first;
first->next=NULL;
first=r;
}
}
void daonguocds(node * &first)
{
node *d = NULL, *s, *t ;
if (first != NULL)
{
s = first;
t = s->next;
while (t != NULL)
{
s->next = d;
d = s;
s = t;
t = t->next;
}
s->next = d;
first = s;
}
}
node* TimQ(int k ,node *first){
node *Q=first;
int dem=1;
while(dem<k && Q!=NULL){
Q=Q->next;
dem++;
}
return Q;
}
void chen(node*p , node *q ,node *&first){
if(first!=NULL && q!=NULL){
if(q==first){
p->next=first;
first=p;
}
else{
node *r=first;
while (r->next!=q)
{
r=r->next;
}
r->next=p;
p->next=q;
}
}
}
void xoadau(node *&first){
node *r=first;
first=first->next;
delete(r);
}
void xoacuoi(node *&first){
node *r=first;
if(first->next==NULL)
delete(first);
else{
node*q=r->next;
while(q->next!=NULL){
r=q;
q=q->next;
}
r->next=NULL;
delete(q);
}
}
void xoaBK(node *Q, node *&first){
if(Q==first) xoadau(first);
else{
node *r=first;
while(r->next!=Q)
r=r->next;
r->next=Q->next;
delete(Q);
}
}
void xoaam(node *&first){
node *r=first;
node *q=first;
while(r!=NULL){
r=r->next;
if(q->info<0) xoaBK(q,first);
q=r;
}
}
void swap(int &a , int &b){
int t=a;
a=b;
b=t;
}
void sort(node *first){
node *r=first;
node *q;
while (r->next!=NULL)
{
q=r->next;
while (q!=NULL)
{
if(q->info < r->info) swap(q->info,r->info);
q=q->next;
}
r=r->next;
}
}
void tron(node *&first , node *&first1 , node*&first2){
node *q=first;
node *p=first1;
while(q!=NULL && p!=NULL){
if(q->info < p->info){
chencuoi(q->info,first2);
q=q->next;
xoadau(first);
}
else{
chencuoi(p->info,first2);
p=p->next;
xoadau(first1);
}
}
while (q!=NULL)
{
chencuoi(q->info,first2);
q=q->next;
xoadau(first);
}
while (p!=NULL)
{
chencuoi(p->info,first2);
p=p->next;
xoadau(first1);
}
}
int ktsnt(int n)
{
if(n<=1) return 0;
for(int i=2;i<n;i++)
if (n%i==0)
return 0;
return 1;
}
void insnt(node *&first)
{
node *p=first;
if(p==NULL) return ;
while(p!=NULL)
{
if(ktsnt(p->info))
cout<<p->info<<" ";
p=p->next;
}
}
void main(){
node *first=NULL;
node *first1=NULL;
node *first2=NULL;
taoDS(first);
// sort(first);
xuatDSLK(first);
insnt(first);
// taoDS(first1);
//sort(first1);
// xuatDSLK(first1);
//tron(first,first1,first2);
// xuatDSLK(first2);
//cout<<first1;
//dao(first);
//xuatDSLK(first);
//chen(10,3,first);
//xoadau(first);
//xoacuoi(first);
//node *Q=TimQ(2,first);
//xoaBK(Q,first);
//node*P=taonode(3);
//chen(P,Q,first);
// xoaam(first);
//xuatDSLK(first);
getch();
}
Bạn đang đọc truyện trên: Truyen247.Pro