problem
why this is not working ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include<iostream>
using namespace std;
struct node {
node *prev;
int data;
node *next;
};
void add(node **head,node **tail,int x)
{
node *loc=*head;
node *ptr=new node;
ptr->data=x;
ptr->next=NULL;
ptr->prev=NULL;
int count=0;
if(*head==NULL)
{
*head=*tail=ptr;
}
else
{
node *cur;
for(cur=*head;cur!=NULL;cur=cur->next)
count++;
cur=*head;
for(int i=0;i<count/2;i++)
cur=cur->next;
for(node *cur=*head;cur!=NULL;cur=cur->next)
count++;
ptr->next=cur->next;
ptr->prev=cur;
(cur->next)->prev=ptr;
cur->next=ptr;
}
}
void print (node *head,node *tail)
{
if(tail!=NULL)
for(node *loc=tail;loc!=NULL;loc=loc->prev)
cout<<loc->data<<" ";
}
int main()
{
int x,y;
node *head,*tail;
do{
system("cls");
cout<<"1.Insert at the middle of the list\n"
<<"2.Delete the second half of the list\n"
<<"3.Print the list\n"
<<"4.Exit\n"
<<"Enter Your Choice : ";
cin>>x;
switch(x)
{
case 1 :{
cout<<"please enter a number to add"<<endl;
cin>>y;
add(&head,&tail,y);
}break;
case 3:print(head,tail);
break;
case 4:cout<<"Bye Bye"<<endl;
break;
default :cout<<"invaild input"<<endl;
}
}while(x!=4);
system("pause");
return 0;
}
|
You have forgot to initialize head and tail.
Thank you , i really don't notice that.
Topic archived. No new replies allowed.