singly linked list

Pages: 12
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
void insert_at_position(size_t pos, int data){
	node *addNode = new node{data, nullptr};
    node *after(head) , *before(nullptr);
    for( int i = 0 ; i < pos; i++ ){
        before = after;
        after = after->next;
    }
    if(before)
        before->next = addNode;
    else
        head = addNode;
    addNode->next = after;
}

//in main:
    int pos;
    int data;
	cout << "Enter data to add : ";
	cin >> data;
	do{
		cout << "Enter position to add : ";
		cin  >> pos;

		if( pos > length() || pos < 0 ){
			cout << "Invalid length ! " << endl;
		}
	}while( pos > length() || pos < 0 );

	insert_at_position(pos, data);
i can't get it for your first function

which line stated that example :
my node now is :

1
2
3
node 1 : 43
node 2 : 42
node 3 : 50

i insert another node ( 60 ) to position 1

1
2
3
4
node 1 : 60
node 2 : 43
node 3 : 42
node 4 : 50

which line did it stated that?
because when u initialize your linked list. i dont know how to see since i don't know how to innitialize like yours
Here's the program from a few posts ago, so that it cannot be later removed (wrecking the whole thread in the archive):
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
#include <iostream>

using namespace std;

struct node{
	int data;
	node *next;
}*head;

void insertNode( int data ){

	node *temp;
	node *addNode = new node;
	addNode->data = data;
	addNode->next = NULL;

	temp = head;
	if( temp != NULL ){
		while( temp->next != NULL ){
			temp = temp->next;
		}
		temp->next = addNode;
	}
	else{
		head = addNode;
	}
}

void display(){

	node* temp;
	temp = head;

	if( temp == NULL )
		cout << "No data inside !" << endl;

	while( temp != NULL ){
		cout << "Data : " << temp->data << endl;
		temp = temp->next;
	}
}

int length(){
	node *n;
	int c = 0;

	n = head;
	while( n != NULL ){
		n = n->next;
		c++;
	}
	return c;
}

void insert_position(){
	int pos = 0;
	node *addNode = new node;
	node *temp , *temp1;

	cout << "Enter data to add : ";
	cin  >> addNode->data;

	addNode->next = NULL;

	do{
		cout << "Enter position to add : ";
		cin  >> pos;

		if( pos > length() || pos < 0 ){
			cout << "Invalid length ! " << endl;
		}
	}while( pos > length() || pos < 0 );

		if( head == NULL ){
			head = addNode;
		}
		else{
			temp = head;
			for( int i = 1 ; i < pos - 1 ; i++ ){
				temp = temp->next;
			}
			temp1 = temp->next;
			temp->next = addNode;
			addNode->next = temp1;
		}
}

int main(){

	insertNode( 43 );
	insertNode( 42 );
	insertNode( 93 );
	insertNode( 100 );
	display();
	cout << "size : " << length() << endl;

	insert_position();

	display();

	cout << "size : " << length() << endl;

	system( "pause" );
	return 0;
}
I just moved User input from insert function.
full main():
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
int main(){

	insertNode( 43 );
	insertNode( 42 );
	insertNode( 93 );
	insertNode( 100 );
	display();
	cout << "size : " << length() << endl;

    int pos;
    int data;
	cout << "Enter data to add : ";
	cin >> data;
	do{
		cout << "Enter position to add : ";
		cin  >> pos;

		if( pos > length() || pos < 0 ){
			cout << "Invalid length ! " << endl;
		}
	}while( pos > length() || pos < 0 );

	insert_at_position(pos, data);

	display();
	cout << "size : " << length() << endl;
	system( "pause" );
	return 0;
}

The rest of code is yours
i know what is my problem jor..
my code must at least behind the head.
because i already set-ed if headl != NULL only will run the program

so the node cnanot insert before the head if head != NULL..

so how bout my delete node?

i think the algo myself but cannot wrong. something wrong.. small problem

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
void deletenode(){

	node *temp , *current;
	current = NULL;
	temp = head;

	int data;
	cout << "Enter data you want to delete : ";
	cin  >> data;

	if( temp == NULL ){
		cout << "No data inside ! cannot delete " << endl;
	}
	else{
		while( temp != NULL ){
			if( temp->data == data ){
				current = temp->next;
				delete temp;
				temp = current;
			}
			else{
				temp = temp->next;
			}
		}
	}
			
}
problem solved ! thanks
@Felicia123 I would think you would know by now it is extremely bad form to delete you OP because it is "solved".
Hello , Felicia123 can u post ur complete answer here?
@ clayKitty: why don't you start your own thread instead?
Topic archived. No new replies allowed.
Pages: 12