Linked list-insertion at end

closed account (1vf9z8AR)
Program to enter user input values at the end of the list.
I am confused about what to do in the insertend function.

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
 #include<iostream>
using namespace std;
struct node
{
        int data;
        node* next;
};
node* rear;
void insertend(int x)
{
        node* temp=new node;
        while(temp->next==NULL)
        {
              
                break;
        }
}
void display()
{
        node* np;
        while(np!=NULL)
                cout<<np->data<<"\t";
        np=np->next;
}
int main()
{
        int data;
        char ch;
        rear=NULL;
        do
        {
                cout<<"Enter your data:";cin>>data;
                cout<<endl;
                insertend(data);
                cout<<"Do you want to continue?(y/n)";cin>>ch;
                cout<<endl;
        }while(ch=='y');
        cout<<"Your list-"<<endl;
display();
return 0;
}
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
#include <iostream>

struct list // TO DO : make non-copyable, non-assignable
{
    struct node
    {
        int value ;
        node* next = nullptr ;
    };

    node* first = nullptr ;
};

bool empty( const list& lst ) { return lst.first == nullptr ; }

void push_back( list& lst, int value )
{
    if( empty(lst) ) lst.first = new list::node{value} ;

    else
    {
        auto p = lst.first ;
        while( p->next != nullptr ) p = p->next ; // get to the last node
        p->next = new list::node{value} ; // add the new item as the next of the last node
    }
}

static void destroy( list::node* p ) // recursively delete nodes in reverse order
{
    if( p != nullptr )
    {
        destroy( p->next ) ;
        delete p ;
    }
}

// delete all nodes and make it an empty list
void clear( list& lst ) { destroy(lst.first) ; lst.first = nullptr ; }

void destroy( list& lst ) { clear(lst) ; }

void print( const list& lst )
{
    std::cout << "\n[ " ;
    for( auto p = lst.first ; p != nullptr ; p = p->next ) std::cout << p->value << ' ' ;
    std::cout << "]\n" ;
}

int main()
{
    list lst ;

    int value ;
    while( std::cout << "value? " && std::cin >> value ) push_back( lst, value ) ;

    print(lst) ;

    destroy(lst) ; // clean up once we are done
}

http://coliru.stacked-crooked.com/a/7d3c118bd0adfe46
closed account (1vf9z8AR)
i cant use such complicated program for school. :(

Now i enter values but empty output comes??
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
#include<iostream>
using namespace std;
struct node
{
        int data;
        node *next=NULL;
};
node *start=NULL;
void insertend(int x)
{
        node *temp=new node;
        if(start==NULL)
                temp->data=x;
        else
        {
  while(temp->next!=NULL)
        temp=temp->next;
  temp->data=x;
}
}
void display()
{
        node* disp=new node;
        while(disp->next!=NULL)
        {
                cout<<disp->data<<endl;
        disp=disp->next;
        }
}
int main()
{
        int x;
        char ch;
        do
        {
                cout<<"Enter data";cin>>x;
                cout<<endl;
                insertend(x);
                cout<<"Do you want to continue?(y/n)";cin>>ch;
        cout<<endl;
        }while(ch=='y');
cout<<"Your list:"<<endl;
display();

}
Last edited on
> i cant use such complicated program for school.

There is nothing complicated about it; even career teachers would be able to understand the code.

This is actually simpler (even if it may appear to be more complicated to a career teacher):
use smart pointers instead of raw pointers and resource management is automated.
http://en.cppreference.com/w/cpp/memory/unique_ptr

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
#include <iostream>
#include <memory>

struct list
{
    struct node
    {
        explicit node( int v ) : value(v) {}
        int value ;
        std::unique_ptr<node> next ;
    };

    std::unique_ptr<node> first ;
};

bool empty( const list& lst ) { return !lst.first ; }

void push_back( list& lst, int value )
{
    if( empty(lst) ) lst.first = std::make_unique<list::node>(value) ;

    else
    {
        auto raw_ptr = lst.first.get() ; // we use raw pointers for iteration
        while( raw_ptr->next ) raw_ptr = raw_ptr->next.get() ; // get to the last node
        raw_ptr->next = std::make_unique<list::node>(value) ; // append the new item at the end
    }
}

void print( const list& lst )
{
    std::cout << "\n[ " ;

    // we use raw pointers for iteration
    for( auto raw_ptr = lst.first.get() ; raw_ptr ; raw_ptr = raw_ptr->next.get() )
        std::cout << raw_ptr->value << ' ' ;

    std::cout << "]\n" ;
}

int main()
{
    list lst ;

    int value ;
    while( std::cout << "value? " && std::cin >> value ) push_back( lst, value ) ;

    print(lst) ;
}

http://coliru.stacked-crooked.com/a/c4eb2f6a4d5c096e
Topic archived. No new replies allowed.