Get Help with C Code: Understanding Linked Lists and Queue Pointers

In summary: Just a curious observation.In summary, the conversation discusses a C program that creates a queue and inserts a value of 2 into it. However, the issue arises when the tail pointer, which points to the tail of the queue, is found to be pointing at the wrong memory address outside of the create_queue function. The solution is to allocate memory on the heap using malloc. Additionally, the term "code" is typically used by scientists and engineers, while "program" is more commonly used in the software industry.
  • #1
cshum00
215
0
1. I have this code

Code:
typedef struct linkedlist {struct linkedlist *next; key_t value;} queue;
const queue *tail;

queue *create_queue(key_t input) {
	queue temp;
	temp.next = NULL;
	temp.value = input;
	tail = &temp;

	printf("%d\n", tail->value);

	return &temp;
}

int main() {
	queue *one;
	one = create_queue(2);

	printf("%d\n", one->value);
	printf("%d\n", tail->value);

	return(0);
}
2. What it does is to
creates a queue called "one" and inserts a value of "2" into it.
Then it prints the value.

The problem is that i have this queue pointer called "tail" which points at the tail of the queue. So when i create the queue (using create_queue function), it obviously will point at the first object. After the program leaves the create_queue functions and goes back to the main function; why the address which the tail pointer is pointing at is not right.

Why after leaving the create_queue function, the tail value changes?

Homework Statement


Homework Equations


The Attempt at a Solution

 
Physics news on Phys.org
  • #2
First off, scientists and engineers are the only ones who call something like this a "code". It is code, but it is a program. Just some general erudition type stuff. [/rant]

Secondly, your problem isn't that tail is pointing at the wrong thing; it's that the memory it is set to point at is local to the create_queue function. The memory address has no meaning outside of the create_queue function. To do what you're doing, you will have to allocate memory on the heap (see malloc).

So yeah, you need to be careful about global and local variables. That may not be something your instructor emphasizes if s/he was raised on FORTRAN, where all variables are global (and where this problem wouldn't happen, mind you).
 
  • #3
AUMathTutor said:
[rant]First off, scientists and engineers are the only ones who call something like this a "code". It is code, but it is a program. Just some general erudition type stuff. [/rant]

It will be a "program" when after being compiled and running. So what i presented you still is just a piece of "code". lol

AUMathTutor said:
Secondly, your problem isn't that tail is pointing at the wrong thing; it's that the memory it is set to point at is local to the create_queue function. The memory address has no meaning outside of the create_queue function. To do what you're doing, you will have to allocate memory on the heap (see malloc).

Right, how can i forget about that. (XD)

P.S.> Thanks for the quick reply.
 
  • #4
It will be a "program" when after being compiled and running. So what i presented you still is just a piece of "code". lol
"Some help with a C code"

I guess you just made a typo. And no, program refers more directly to the code, not to process, which is what you're thinking. It is a piece of code, but it is not "a code". Just letting you in on some lingo so you don't make your software people snicker at you.

As for the rest, don't worry about it. Like I said, I doubt your instructor ever even made a big point out of it.
 

Related to Get Help with C Code: Understanding Linked Lists and Queue Pointers

What is a linked list?

A linked list is a linear data structure where each element, known as a node, contains a data value and a pointer to the next node in the list. This allows for efficient insertion and deletion operations, but accessing a specific element in the list can be slower compared to other data structures.

What is the difference between a singly linked list and a doubly linked list?

In a singly linked list, each node only has a pointer to the next node in the list. This means that traversal can only be done in one direction. In a doubly linked list, each node has both a pointer to the next node and a pointer to the previous node. This allows for traversal in both directions, but takes up more memory.

How do you insert a new element into a linked list?

To insert a new element into a linked list, you will need to adjust the pointers of the nodes surrounding the insertion point. For example, if inserting a new node between nodes A and B, the pointer of node A should now point to the new node, and the pointer of the new node should point to node B.

What is a queue and how is it related to linked lists?

A queue is a data structure that follows the "first in, first out" (FIFO) principle. This means that the first element inserted into the queue will be the first one to be removed. A queue can be implemented using a linked list by using the head of the list as the front of the queue and the tail of the list as the back of the queue.

How do you implement a queue using linked lists in C?

To implement a queue using linked lists in C, you will need to keep track of the front and back of the queue using pointers. When adding an element to the queue, you will insert it at the back of the list and adjust the back pointer. When removing an element from the queue, you will remove it from the front of the list and adjust the front pointer.

Similar threads

Replies
3
Views
1K
Replies
2
Views
3K
Replies
1
Views
9K
Replies
3
Views
822
Replies
3
Views
1K
Replies
1
Views
846
Back
Top