Doubly linked List

i had wrote this program which provide 3 cases add and delete the second half and to print . the delete function is not working . please help me
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  #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 if (*tail==*head)
	{
		node *cur;
		cur=*head;

	
		ptr->next=NULL;;
		ptr->prev=cur;
		//cur->next->prev=ptr;
		cur->next=ptr;
		*tail=ptr;
	}

	else
	{
		node *cur;
		for(cur=*head;cur!=NULL;cur=cur->next)
			count++;
		cur=*head;

		for(int i=1;i<count/2;i++)
			cur=cur->next;

		
		ptr->next=cur->next;
		cur->next=ptr;
		ptr->prev=cur;
		cur->next->next->prev=ptr;
		
		
	}
}

void print ( node * tail)
{
	if (tail !=NULL)
	{
		cout << "The link list contains:\n";
		for (node *cur = tail; cur != NULL; cur = cur->prev)
			cout << cur->data <<" ";
			cout << "\nDone!\n ";
	}
	else 
		cout << "It is empty!\n ";

	cout<<endl;
	system("pause");
}



void del(node * head, node **tail)
{
	int count=0;
	node *cur;
		for(cur=head;cur!=NULL;cur=cur->next)
			count++;
		cur=head;

		for(int i=1;i<count/2;i++)
			cur=cur->next;

		node *temp=cur;

		while(cur!=NULL)
		{
			temp->next=cur->next;
			cur=cur->next;
			delete temp;
			*tail=cur;
		}
		


		
}


int main()
{
	int x,y;
	node *head=NULL,*tail=NULL;
	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 2:del(head,&tail);break;
		case 3:print(tail);
			break;
		case 4:cout<<"Bye Bye"<<endl;
			break;
		default :cout<<"invaild input"<<endl;
		}
	}while(x!=4);
	system("pause");
	return 0;
}
Topic archived. No new replies allowed.