Trang chủ/ Tanpa Label / Bài tập LIFO:Nhập mảng-xem mảng-đếm số phần tử của mảng-bổ sung đầu-bổ sung cuối-tìm xem có x trong mảng hay không
#include <stdio.h>
#include <conio.h>
typedef struct node
{
int data;
node *next;
};
void xem(node *first)
{
node *p;
p=first;
while(p!=NULL)
{
printf("%5d",p->data);
p=p->next;
}
}
node *nhap(node *first,int n)
{
node *p;
for(int i=0;i<n;i++)
{
p=new(node);
scanf("%5d",&p->data);
p->next=first;
first=p;
}
return first;
}
int dem(node *first)
{
node *p;
int i=0;
p=first;
while(p!=NULL)
{
i++;
p=p->next;
}
return i;
}
node *bosungdau(node *first)
{
node *p;
p=new(node);
printf("\nnhap so can bo sung vao dau:\n");
scanf("%5d",&p->data);
p->next=first;
first=p;
return first;
}
node *bosungcuoi(node *first)
{
node *p,*tg;
tg=new(node);
printf("\nnhap so can bo sung vao cuoi:\n");
scanf("%5d",&p->data);
tg->next=NULL;
p=first;
while(p!=NULL)
p=p->next;
if(first==NULL)
first=tg;
p->next=tg;
return first;
}
node *tangdan(node *first)
{
node *p1,*p2;
p1=first;
while(p1->next!=NULL)
{
p2=p1->next;
while(p2!=NULL)
{
if(p1->data>p2->data)
{
int tg=p1->data;
p1->data=p2->data;
p2->data=tg;
}
p2=p2->next;
}
p1=p1->next;
}
return first;
}
void vitrix(node *first)
{
node *p;
int x;
printf("\nnhap x:\n");
scanf("%d",&x);
p=first;
vitri=0;
i=1;
while(p!=NULL)
{
if(p->data==x)
vitri=i;
i++;
p=p->next;
}
if(vitri==0) printf("\nkhon co phan tu %d trong mang\n",x);
else
printf("vi tri phan tu %d la %d",x,vitri);
}
Không có nhận xét nào:
Đăng nhận xét