البرنامج يعمل بشكل سليم ولكن بعد عرض القائمه بعد الاضافه او الحذف يخرج رساله خطأ
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
struct node
{
int number;
struct node *next;
};
typedef node *node_ptr;
node_ptr make_new_node(int x)
{
node_ptr p;
p=new node;
p->number =x;
p->next =NULL;
return p;
}
void order_insert(node_ptr &first,int x)
{
node_ptr p;
if(first==NULL)
{
first=make_new_node (x);
}
else if(x==first->number )
{
cout<<" This value in node is Exist "<<"\n";
}
else if(x<first->number )
{
p=make_new_node (x);
p->next =first;
first=p;
}
else
order_insert(first->next,x);
}
void create_nodes(node_ptr &first,int n)
{
int x;
cout<<" Enter the data in nodes"<<"\n";
for(int i=1; i<=n; i++)
{
cout<<" Node "<<i<<endl;
cin>>x;
order_insert(first,x);
}
}
void display_list(node_ptr first,int n )
{
node_ptr p;
if(first==NULL)
cout<<"NO nodes founds"<<"\n";
else
{
cout<<" The Data in the nodes are "<<"\n";
p=first;
while(p!=NULL)
{
for(int i=1; i<=n; i++)
{
cout<<"Node"<<i<<"\n"<<p->number <<"\n";
cout<<"\n";
p=p->next;
}
}
}
}
void insert_first(node_ptr &first)
{
int x;
cout<<"Enter value in new node"<<"\n";
cin>>x;
node_ptr p;
p=make_new_node(x);
p->next=first;
first=p;
}
void insert_last(node_ptr &first)
{
int x;
cout<<"Entr value in new node "<<"\n";
cin>>x;
node_ptr p,q;
if(first==NULL)
{
first=make_new_node (x);
}
else
{
p=first;
while(p->next!=NULL)
{
p=p->next ;
}
q=make_new_node (x);
p->next =q;
}
}
void insert_middle(node_ptr &first,int n)
{
int x;
cout<<"Entr value in new node "<<"\n";
cin>>x;
node_ptr p,q;
if(first==NULL)
{
first=make_new_node (x);
}
else
{
p=first;
while(p->number!=n &&p->next!=NULL)
{
p=p->next ;
}
node *q =new node;
q->number=n;
q=make_new_node (x);
q->next=p->next;
p->next =q;
}
}
void delete_first(node_ptr &first)
{
if(first==NULL)
{
cout<<"NO nodes found"<<"\n";
}
else
{
node_ptr p;
p=first;
first=first->next;
delete p;
}
}
void delete_last(node_ptr &first)
{
node_ptr p,d;
p=first;
if(first==NULL)
cout<<" NO nodes found"<<"\n";
while(p->next->next!=NULL)
{
p=p->next;
}
d=p->next;
p->next=NULL;
delete d;
}
void delete_middle(node_ptr&q,int m)
{
node_ptr p,d;
p=q;
while(p->next->number!=m &&p->next->next!=NULL)
{
p=p->next;
}
d=p->next;
p->next=d->next;
delete d;
}
int main()
{
int x;
node_ptr first;
first=NULL;
do
{
cout<<"enter 1 to creat nodes"<<"\n";
cout<<"enter 2 display list "<<"\n";
cout<<"enter 3 to insert first "<<"\n";
cout<<"enter 4 to insert last "<<"\n";
cout<<"enter 5 to insert middle "<<"\n";
cout<<"enter 6 to delet first "<<"\n";
cout<<"enter 7 to delet last "<<"\n";
cout<<"enter 8 to delet middle "<<"\n";
cout<<"enter 9 to exit "<<"\n";
cout<<"\n";
cin>>x;
cout<<"\n";
switch(x)
{
case 1:
int n;
cout<<" Enter number of nodes to create"<<"\n";
cin>>n;
create_nodes(first,n);
break;
case 2:
display_list (first,n);
break;
case 3:
insert_first (first);
break;
case 4:
insert_last (first);
case 5:
int y;
cout<<"Enter position number"<<"\n";
cin>>y;
insert_middle(first,n);
break;
case 6:
delete_first(first);
break;
case 7:
delete_last(first);
break;
case 8:
int s;
cout<<"Enter value of node to delete"<<"\n";
cin>>s;
delete_middle(first,s);
break;
case 9:
exit(0);
break;
default:
cout<<"invalid"<<"\n";
cout<<"\n";
}
}
while(x!=9);
getch();
}تم تعديل هذه المشاركة بواسطة احمد النبوي في 13 ديسمبر 2013 في 18:24 — السبب: تنسيق السؤال