problem with AVAIL

Write your question here.

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
 #include<iostream>
#include<conio.h>
using namespace std;
struct node
{
    int data;
    node *next;
};
typedef node *list;
void ins_beg(list, int, list);
list head, AVAIL;
int main()
{
    int dat, item;
    char ch;
    list temp;
    head=NULL;
    cout<<"Do you want to enter data??"<<endl;
    cin>>ch;
    while(ch=='y' || ch=='Y')
    {
        cout<<"Enter data"<<endl;
        cin>>dat;
        temp=new node;
        temp->data=dat;
        temp->next=head;
        head=temp;
        cout<<"Enter more data??"<<endl;
        cin>>ch;
    }
    cout<<"Do you wish to insert any node at beginning?"<<endl;
    cin>>ch;
    while(ch=='y'|| ch=='Y')
    {
        cout<<"Enter data"<<endl;
        cin>>item;
        ins_beg(head, item, AVAIL);
        cout<<"Do you wish to insert more items at beginning?"<<endl;
        cin>>ch;
    }
    return 0;
}

void ins_beg(list head, int item, list AVAIL )
{
    if(AVAIL==NULL)
    {
        cout<<"Overflow !!! CAN'T INSERT"<<endl;
    }
    AVAIL=new node;
    AVAIL=AVAIL->next;
    AVAIL->data=item;
    head=AVAIL;
    return;
}
Is there a particular question or problem regarding the above code?
dere is some logical problem in ins_beg function.....please help me
I think to insert at the beginning, the function requires only two parameters, the head of the list, and the data value. Perhaps something like this:
1
2
3
4
5
6
7
void ins_beg(list & head, int item)
{
    list AVAIL = new node;
    AVAIL->data = item;
    AVAIL->next = head;
    head = AVAIL;
}

Note head is passed by reference as it will be modified by this function.
Last edited on
Topic archived. No new replies allowed.