Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

tai lieu mon C

#include"conio.h"

#include"stdio.h"

#include"malloc.h"

struct node

{

int data;

node *next;

};

typedef struct node *listnode;

 

void action(listnode *l);

void createnode(listnode *l, int x);

void insertfirst(listnode *l, int x);

void insertlast(listnode *l, int x);

void Travelling(listnode *l);

void timvt(listnode *l, int x);

void Dellete_middle(listnode *l,int x);

void Delfirst(listnode *l);

void Dellast(listnode *l);

void action(listnode *l)

{

*l=NULL;

return;

}

void createnode(listnode *l, int x)

{

listnode p;

p=(listnode)malloc(sizeof(struct node));

if(p!=NULL)

{

p->data=x;

p->next=NULL;

}

*l=p;

return;

}

void insertfirst(listnode *l, int x)

{

listnode p;

p=(listnode)malloc(sizeof(struct node));

if(p!=NULL)

{

p->data=x;

p->next=*l;

}

*l=p;

return;

}

void insertlast(listnode *l, int x)

{

listnode p, r;

p=(listnode)malloc(sizeof(struct node));

r=*l;

if(p!=NULL)

{

p->data=x;

p->next=NULL;

}

while(r->next!=NULL)

r=r->next;

r->next=p;

}

void Travelling(listnode *l)

{

listnode p;

printf("

Danh sach la:  ");

p=*l;

while(p!=NULL)

{

printf("%4d",p->data);

p=p->next;

}

return;

}

void search(listnode *l, int x)

{

listnode p;

int count=0;

for(p=*l;p!=NULL;p=p->next)

{

if(p->data==x)

count++;

}

if(count!=0)

printf("

Tim thay %d trong day",x);

else

printf("

K tim thay %d trong day",x);

}

void timvt(listnode *l, int x)

{

listnode p, r;

int count=0;

for(p=*l;p!=NULL;p=p->next)

{

if(p->data==x)

{

r=p;

count++;

break;

}

}

if(count==0)

printf("

K co pt can xoa trong ds.");

else if(r==*l)

Delfirst(l);

else if(r->next==NULL)

Dellast(l);

else

Dellete_middle(l,x);

}

 

void Dellete_middle(listnode *l, int x)

{

listnode p, r, q, k;

p=*l;

for(p=*l;p!=NULL;p=p->next)

{

if(p->data==x)

{

k=p;

break;

}

}

r=*l;

while(r->next!=k)

r=r->next;

q=k->next;

r->next=q;

free(p);

return;

}

void Delfirst(listnode *l)

{

listnode p;

p=*l;

*l=p->next;

free(p);

return;

}

void Dellast(listnode *l)

{

listnode p, r, k=*l;

p=*l;

while(p->next!=NULL)

p=p->next;

r=p;

while(k->next!=p)

k=k->next;

k->next=NULL;

free(r);

return;

}

void main()

{

listnode l;

int cn, x;

clrscr();

action(&l);

printf("

1. Createnode");

printf("

2. insertfirst");

printf("

3. Insertlast");

printf("

4. Travelling.");

printf("

5. Search.");

printf("

6. Dellete");

do

{

printf("

Chon chuc nang: ");

scanf("%d",&cn);

switch(cn)

{

case 1:

{

printf("

Nhap gtri: ");

scanf("%d",&x);

createnode(&l,x);

break;

}

case 2:

{

printf("

Nhap gtri can them: ");

scanf("%d",&x);

insertfirst(&l,x);

break;

}

case 3:

{

printf("

Nhap gtri can them: ");

scanf("%d",&x);

insertlast(&l,x);

break;

}

case 4:

{

Travelling(&l);

break;

}

case 5:

{

printf("

Nhap pt can tim: ");

scanf("%d",&x);

search(&l,x);

break;

}

case 6:

{

printf("

Nhap pt can xoa: ");

scanf("%d",&x);

timvt(&l,x);

break;

}

}

}while(cn!=0);

}

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

Tags: