dsdon

Màu nền
Font chữ
Font size
Chiều cao dòng

Danh sách móc nối đơn:

#include<conio.h>

#include<string.h>

#include<iostream.h>

#include<stdio.h>

#include<alloc.h>

#include<iomanip.h>

struct sv

{

char masv[10];

char hoten[20];

float dtb;

};

struct dssv

{

sv ptu;

dssv*next;

}*l;

void nhap (dssv*&l)

{

dssv*q;

char s[10];

l=new dssv; q=l;

cout<<"nhap ma sv:";

gets(l->ptu.masv);

cout<<"nhap ho ten:";

gets(l->ptu.hoten);

cout<<"nhap dtb:";

cin>>l->ptu.dtb;

cout<<"nhap ma sv:";

gets(s);

while (strcmp (s,""))

{

q->next=new dssv;

q=q->next;

strcpy (q->ptu.masv,s);

cout<<"nhap hoten:";

gets(q->ptu.hoten);

cout<<"nhap dtb:";

cin>>q->ptu.dtb;

cout<<"nhap masv:";

gets(s);

}

q-> next = NULL;

}

void hien(dssv*l)

{

while (l!= NULL)

{

cout<<l->ptu.masv<<setw(20)<<l->ptu.hoten;

cout<<setw(10)<<l->ptu.dtb<<"

";

l=l->next;

}

}

void sapxep(dssv*&l)

{

dssv*p,*q;sv tg;

if(l!=NULL)

{

p=l;

while(p->next!=NULL)

{

q=p->next;

while(q!=NULL)

{

if(p->ptu.dtb>q->ptu.dtb)

{

tg=q->ptu;

q->ptu=p->ptu;

p->ptu=tg;

}

q=q->next;

}

p=p->next;

}

}

}

void chend(dssv*&l)

{

dssv *q=new dssv;

cout<<"nhap ma sv:";

gets(q->ptu.masv);

cout<<"nhap ho ten:";

gets(q->ptu.hoten);

cout<<"nhap dtb:";

cin>>q->ptu.dtb;

q->next=l;

l=q;

}

void chenc(dssv*&l)

{

dssv *p,*q=new dssv;

cout<<"nhap ma sv:";

gets(q->ptu.masv);

cout<<"nhap ho ten:";

gets(q->ptu.hoten);

cout<<"nhap dtb:";

cin>>q->ptu.dtb;

q->next=NULL;

if (l==NULL) l=q;

else

{

p=l;

while(p->next!=NULL) p=p->next;

p->next=q;

}

}

void chensap(dssv*&l)

{

dssv *p,*q=new dssv;

cout<<"nhap ma sv:";

gets(q->ptu.masv);

cout<<"nhap ho ten:";

gets(q->ptu.hoten);

cout<<"nhap dtb:";

cin>>q->ptu.dtb;

if (l==NULL||q->ptu.dtb<l->ptu.dtb)

{

q->next=l;

l=q;

}

else

{

p=l;

while(p->next!=NULL&&p->next->ptu.dtb<q->ptu.dtb) p=p->next;

q->next=p->next;

p->next=q;

}

}

void delc(dssv*&l)

{

if(l!=NULL)

{

dssv*p=l,*q;

if (l->next==NULL)

{ delete l;l=NULL; }

else

{

while (p->next->next!=NULL) p=p->next;

q=p->next;

p->next=NULL;

delete q;

}

}

}

void deld(dssv*&l)

{

if(l!=NULL)

{

dssv*p=l;

l=l->next;

delete p;

}

}

void del(dssv *&l)

{

if(l!=NULL)

{

dssv*p=l,*q;

while (p->next!=NULL)

if (p->next->ptu.dtb<5)

{

q=p->next;p->next=q->next;delete q;

}

else p=p->next;

if(l->ptu.dtb<5)

{

q=l;l=l->next;delete q;

}

}

}

main()

{

nhap(l);

hien(l);

l=NULL;

cout<<"danh sach sau khi sap xep:

";

sapxep (l);hien(l);

cout<<"danh sach sau khi chen dau:

";

chend(l);hien(l);

cout<<"danh sach sau khi chen cuoi:

";

chenc(l);hien(l);

cout<<"danh sach sau khi chen phan tu la:

";

chensap(l);

cout<<"danh sach sau khi xoa la:

";

del(l);

deld(l);

delc(l);

hien(l);

getch();

}

Mảng 1 chiều

#include "iostream.h"

#include "conio.h"

#include "stdio.h"

#include "math.h"

#include "iomanip.h"

//=====================================

//Nhap mang:

void nhap(int a[], int &n)

{

cout<<"

Nhap n:";

cin>>n;

for (int i=0; i<n; i++)

{

cout<<"

a["<<i<<"]=";

cin>>a[i];

}

}

//=====================================

//Xuat mang ra man hinh:

void hien(int a[], int n)

{

for (int i=0; i<n; i++)

cout<<setw(5)<<a[i];

}

//=====================================

//Tim phan tu max va dem so phan tu max:

void max(int a[], int n)

{

int m=a[0], dem=1;

for (int i=1; i<n; i++)

{

if (m==a[i])

dem++;

if (m<a[i])

{

m=a[i];

dem=1;

}

}

cout<<"

Max="<<m;

cout<<"

So phan tu max la:"<<dem;

}

//=====================================

//Xoa cac phan tu bang x:

void xoax(int a[], int &n)

{

int x;

cout<<"

Nhap x:";

cin>>x;

int d=0;

while (d<n && a[d]!=x)

d++;

for (int i=d+1; i<n; i++)

if (a[i]!=x)

{

a[d]=a[i];

d++;

}

n=d;

cout<<"

Sau khi xoa phan tu bang x:"<<endl;

for (int i=0; i<n; i++)

cout<<setw(5)<<a[i];

}

//=====================================

//Sap xep tang dan:

void saptang(int a[], int n)

{

cout<<"

Sap xep tang dan:"<<endl;

int tg;

for (int i=0; i<n; i++)

for (int j=i+1; j<n; j++)

if (a[i]>a[j])

{

tg=a[i];

a[i]=a[j];

a[j]=tg;

}

cout<<"

Sau khi sap xep tang dan:"<<endl;

for (int i=0; i<n; i++)

cout<<setw(5)<<a[i];

}

//=====================================

//Chen x vao mang da sap, thoa man thu tu sap:

void chenx(int a[], int &n)

{

int d=0;

int x;

cout<<"

Nhap phan tu x:";

cin>>x;

while (d<n && a[d]<x)

d++;

for (int i=n; i>d; i--)

a[i]=a[i-1];

a[d]=x;

n++;

cout<<"

Sau khi chen phan tu x:"<<endl;

for (int j=0; j<n; j++)

cout<<setw(5)<<a[j];

}

//Main:

main()

{

int a[1000], n;

nhap(a,n);

hien(a,n);

//max(a,n);

//xoax(a,n);

//saptang(a,n);

//chenx(a,n);

getch();

}

Danh sách liên kết đơn:

struct sv

{

char masv[10];

char hoten[20];

float dtb;

};

struct node

{

sv ptu;

node *next, *pre;

};

struct dssv

{

node *l, *r;

}ds;

void nhap (dssv &ds)

{

char s[10];

ds.l=new node;

ds.r=ds.l;

ds.l->pre=NULL;

cout<<"Nhap ma sv: ";

{

fflush(stdin);

gets(ds.l->ptu.masv);

}

cout<<"Nhap ho ten: ";

{

fflush(stdin);

gets(ds.l->ptu.hoten);

}

cout<<"Nhap diem TB: ";

cin>>ds.l->ptu.dtb;

cout<<"Nhap ma sv: ";

{

fflush(stdin);

gets(s);

}

while (strcmp (s,""))

{

ds.r->next=new node;

ds.r->next->pre=ds.r;

ds.r=ds.r->next;

strcpy (ds.r->ptu.masv,s);

cout<<"Nhap ho ten: ";

{

fflush(stdin);

gets(ds.r->ptu.hoten);

}

cout<<"Nhap diem TB: ";

cin>>ds.r->ptu.dtb;

cout<<"Nhap ma sv: ";

{

fflush(stdin);

gets(s);

}

}

ds.r->next = NULL;

}

void hiennguoc(dssv ds)

{

while (ds.r!= NULL)

{

cout<<ds.r->ptu.masv<<setw(20)<<ds.r->ptu.hoten;

cout<<setw(10)<<ds.r->ptu.dtb<<"

";

ds.r=ds.r->pre;

}

}

void hienxuoi(dssv ds)

{

while (ds.r!= NULL)

{

cout<<ds.l->ptu.masv<<setw(20)<<ds.l->ptu.hoten;

cout<<setw(10)<<ds.l->ptu.dtb<<"

";

ds.l=ds.l->pre;

}

}

/*

void sapxep(dssv*&l)

{

node *p,*q;

sv tg;

if (ds.l!=NULL)

{

p=ds.l;

while(p->next!=NULL)

{

q=p->next;

while(q!=NULL)

{

if(p->ptu.dtb>q->ptu.dtb)

{

tg=q->ptu;

q->ptu=p->ptu;

p->ptu=tg;

}

q=q->next;

}

p=p->next;

}

}

}

void chend(dssv*&l)

{

dssv *q=new dssv;

cout<<"Nhap ma sv: ";

{

fflush(stdin);

gets(q->ptu.masv);

}

cout<<"Nhap ho ten: ";

{

fflush(stdin);

gets(q->ptu.hoten);

}

cout<<"Nhap diem TB: ";

cin>>q->ptu.dtb;

q->next=l;

l=q;

}

void chenc(dssv*&l)

{

dssv *p,*q=new dssv;

cout<<"Nhap ma sv: ";

{

fflush(stdin);

gets(q->ptu.masv);

}

cout<<"Nhap ho ten: ";

{

fflush(stdin);

gets(q->ptu.hoten);

}

cout<<"Nhap diem TB: ";

cin>>q->ptu.dtb;

q->next=NULL;

if (l==NULL) l=q;

else

{

p=l;

while(p->next!=NULL) p=p->next;

p->next=q;

}

}

void chensap(dssv*&l)

{

dssv *p,*q=new dssv;

cout<<"Nhap ma sv: ";

{

fflush(stdin);

gets(q->ptu.masv);

}

cout<<"Nhap ho ten: ";

{

fflush(stdin);

gets(q->ptu.hoten);

}

cout<<"Nhap diem TB: ";

cin>>q->ptu.dtb;

if (l==NULL||q->ptu.dtb<l->ptu.dtb)

{

q->next=l;

l=q;

}

else

{

p=l;

while(p->next!=NULL&&p->next->ptu.dtb<q->ptu.dtb) p=p->next;

q->next=p->next;

p->next=q;

}

}

void delc(dssv*&l)

{

if(l!=NULL)

{

dssv*p=l,*q;

if (l->next==NULL)

{

delete l;

l=NULL; 

}

else

{

while (p->next->next!=NULL) p=p->next;

q=p->next;

p->next=NULL;

delete q;

}

}

}

void deld(dssv*&l)

{

if(l!=NULL)

{

dssv*p=l;

l=l->next;

delete p;

}

}

void del(dssv *&l)

{

if(l!=NULL)

{

dssv*p=l,*q;

while (p->next!=NULL)

if (p->next->ptu.dtb<5)

{

q=p->next;p->next=q->next;delete q;

}

else p=p->next;

if(l->ptu.dtb<5)

{

q=l;l=l->next;delete q;

}

}

}

Key:

#include <cstring>

#include <cmath>

#include <conio.h>

#include <iostream>

#include <cstdio>

using namespace std;

struct node

{

int key;

node *left, *right;

}*t;

void insert(int x, node *&t)

{

if (t==NULL)

{

t= new node;

t->key=x;

t->left=NULL;

t->right=NULL;

}

else if (x<t->key)

insert(x,t->left);

else

insert(x,t->right);

}

void nhap(node *&t)

{

int n,x;

t=NULL;

cout<<"

Nhap so nut:";

cin>>n;

for (int i=1;i<=n;i++)

{

cout<<"

Nhap nut thu "<<i<<" : ";

cin>>x;

insert(x,t);

}

}

void hienpre(node *t)

{

if (t!=NULL)

{

cout<<t->key<<" ";

hienpre(t->left);

hienpre(t->right);

}

}

void hienin(node *t)

{

if (t!=NULL)

{

hienin(t->left);

cout<<t->key<<" ";

hienin(t->right);

}

}

void hienpost(node *t)

{

if (t!=NULL)

{

hienpost(t->left);

hienpost(t->right);

cout<<t->key<<" ";

}

}

void del(int x,node *&t)

{

node *p,*q;

if (t!=NULL)

{

if (t->key==x)

if (t->left==NULL)

{

p=t;

t=t->right;

delete p;

}

else if (t->right==NULL)

{

p=t;

t=t->right;

delete p;

}

else

{

p=t->left;

if (p->right==NULL)

{

p->right=t->right;

q=t;

t=p;

delete q;

}

else

{

while (p->right->right!=NULL)

p=p->right;

q=p->right;

t->key=q->key;

p->right=q->left;

delete q;

}

}

else if(t->key>x)

del(x,t->left);

else

del(x,t->right);

}

}

int main()

{

freopen("input.in","r",stdin);

nhap(t);

//freopen("output.txt","w",stdout);

cout<<"

Hien dau:

";

hienpre(t);

cout<<"

Hien giua:

";

hienin(t);

cout<<"

Hien sau:

";

hienpost(t);

int x;

cout<<"

Nhap x:

";

cin>>x;

del(x,t);

cout<<"

Hien dau:

";

hienpre(t);

cout<<"

Hien giua:

";

hienin(t);

cout<<"

Hien sau:

";

hienpost(t);

getch();

return 0;

}

Bạn đang đọc truyện trên: Truyen2U.Pro