mn giúp e bài này với. e chạy bài này nhưng khi nhập xong nó ra cửa sổ exe has stopped working.
Là vì sao ạ?
#include<iostream>
using namespace std;
struct node{
int data;
struct node *next;
};
typedef struct node Node;
Node *getnode(int x){
Node *p=new Node;
Node *head;
if(head == NULL) return NULL;
else {
p->data=x;
p->next=NULL;
return p;
}
}
void init(Node *head){
head=NULL;
}
void themdau(Node *p){
Node * newnode;
p=newnode;
Node *head;
if(head==NULL) head=newnode;
else {
p->next=head;
head=p;
}
}
void in(Node *p){
Node *head=p;
if(head== NULL) cout<<"\n danh sach rong";
cout<<"\n danh sach lien ket: \t";
while(head!=NULL) {
cout<<head->data <<" -> " ;
head=head->next;
}
cout<<head->data;
}
void themcuoi(Node *p){
Node *head;
Node *newnode=p;
int x;
p=getnode(x);
while(head->next!=NULL){
head=head->next;
}
newnode->next=NULL;
head->next=newnode;
}
void nhap(Node *p,int n){
int x;
for(int i=1;i<=n;i++){
cout<<"\n nhap phan tu thu "<<i<< " : ";
cin>>x;
p=getnode(x);
themcuoi(p);
}
}
void chen(Node *p,int vt1){
int dem=0;
int x;
Node *head,*temp;
cout<<"\n nhap vi tri can chen: "; cin>>vt1;
cout<<"\n nhap gia tri: ";cin>>x;
p=getnode(x);
while(head->next!=NULL){
dem++;
}
if(vt1==1) {
if(head==NULL){
head=p;
head->next=NULL;
}
p=head;
head->next=p;
}
else if(vt1>1 && vt1<=dem){
for(int i=1;i<vt1;i++){
p=head;
head=head->next;
}
head->next=p;
p->next=head;
}
else cout<<" ngoai vi tri chen.";
}
int main(){
int n,vt1;
Node *p;
cout<<"\n nhap so node: "; cin>>n;
nhap(p,n);
cout<<"\n danh sach lien ket: ";
in(p);
chen(p,vt1);
return 0;
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?