Linked list

Feb 13, 2021 at 4:02am
Can someone give me a proper explanation of this program about the linked list?
Have a hard time understanding. Here it is in 3 part :

CREATE LIST
1
2
3
4
5
6
7
8
9
10
11
 struct node {
  int e; // e can be a struct
  node *next;
};
node *create(int element) {
   node *n;
   n = new node;
   n->e = element;
   n->next = NULL
   return n;
}



LOOK UP AN ELEMENT
1
2
3
4
5
6
7
8
9
 bool isFound(node *nptr, int element) {
   bool isF = false;
   node *cur_nptr = nptr;
   while (cur_nptr != NULL && !isF)
      if (cur_nptr->e == element)
         isF = true;
      else
         cur_nptr = cur_nptr->next;
return isF; }



APPEND AN ELEMENT
1
2
3
4
5
6
7
8
9
 void appd(node *nptr, int element) {
   node *cur_nptr = nptr, *n;
   while (cur_nptr->next != NULL)
       cur_nptr = cur_nptr->next;
   n = new node;
   n->e = element;
   n->next = NULL
   cur_nptr->next = n;
return; }
Feb 13, 2021 at 9:06am
Are there any bits that you can understand?
Feb 13, 2021 at 10:36am
IMO, the best way to understand some code is to trace through it using the debugger and watch the variables. or just use pen/paper. Draw out a simple list showing pointers etc, then trace through the code using the list.

Is this C or C++ code? If C++, it's not the best written.

Last edited on Feb 13, 2021 at 10:36am
Feb 13, 2021 at 12:25pm
Must be C++ due to new.

That is not actually a "program" that one could run (with debugger); just one custom type and three functions.
A sample program that uses them:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main() {
  node *list = create( 42 );
  appd( list, 7 );
  if ( isFound( list, 3 ) ) std::cout << "Found 3\n";
  if ( isFound( list, 42 ) ) std::cout << "Found 42\n";

  while ( list ) {
    node *first = list;
    list = list->next;
    delete first;
  }
}
Feb 13, 2021 at 12:50pm
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
#include <iostream>

struct node {
	int e {};
	node *next {};

	node() {}
	node(int e_) : e(e_) {}
};

auto create(int element) {
	return new node (element);
}

bool isFound(const node *nptr, int element) {
	for (; nptr; nptr = nptr->next)
		if (nptr->e == element)
			return true;

	return false;
}

void appd(node *nptr, int element) {
	for (; nptr->next; nptr = nptr->next);

	nptr->next = new node(element);
}

void display(const node* nptr) {
	for (; nptr; nptr = nptr->next)
		std::cout << nptr->e << ' ';

	std::cout << '\n';
}

int main() {
	auto list {create(42)};

	appd(list, 7);
	display(list);

	if (isFound(list, 3))
		std::cout << "Found 3\n";

	if (isFound(list, 42))
		std::cout << "Found 42\n";

	while (list) {
		const auto first {list};

		list = list->next;
		delete first;
	}
}



42 7
Found 42

Feb 13, 2021 at 1:05pm
To understand linked list code, consider a list as a collection of nodes - with the next element of a node pointing to the next node in sequence. The next element of the last node is nullptr to indicate that there are no following nodes.

To iterate the list, start at the first node (list), process its contents as required, then use the next element to get the next node. Continue until all nodes are processed.

To add a node to the end of the list, you first have to iterate the existing list to find the end. Then the next element of the last node of the existing list is then set to the new node to be added.
Topic archived. No new replies allowed.