Chào mn. e mới học danh sách liên kết. e có viết 1 đoạn code . Giờ e muốn thêm hàm chèn phần tử vào vị trí thứ k trong danh sách mà chưa thể làm đc. e đã tham khảo trên mạng nhưng k hiểu lắm. mong mn có thể giúp đỡ e ạ.
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef int infor;
struct ds{
infor data;
ds *next;
};
typedef ds *Node;
typedef Node *List;
Node f;
void unit(Node &f){
f = NULL;
}
ds *them(Node &f, infor x){
ds *p = new ds;
p->data = x;
p->next = f;
f = p;
return p;
}
void nhap(Node &f){
int n;
do{
cout<<"Nhap so luong phan tu n: "<< endl;
cin>>n;
srand(time(NULL));
for(int i = 0; i<n; i++){
int x = -300 + rand()%(300+1 +300);
them(f,x);
}
}while( n< -300 || n> 300);
}
void xuat(Node f){
ds *p = f;
while(true){
cout<<p->data<<" ";
p = p->next;
if(p==NULL){
break;
}
}
}
int main(){
Node f;
unit(f);
nhap(f);
xuat(f);
return 0;
}

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?