Implement a stack using the first element of an array as top - c

I am implementing a stack in C. The task i have been given requires me to use the first element of the array as top.
For example:
If i have the struct:
struct stack {
int arr[MAX];
int top
};
I need to assign the first element of arr(i.e. arr[0]) to top and then implement the stack.
I don't get the question as top is usually assigned the value -1 and is accordingly incremented or decremented. What exactly do I need to do here?

This is a rather strange requirement, because the most natural way to implement the stack is using the last element of the array as the top by adding new elements at the end of the array and removing them from the end.
This way you will satisfy the LIFO requirement of the stack and also avoid moving the rest of the data in the array as you would have if you were adding and removing elements from the beginning of the array.
Also I'm not sure what is the top member of your struct is supposed to do, but the most natural thing to have here is some way to indicate what is the last element of the array currently is (i.e. the top of the stack). So it seems to me that the top member should contain the index of the last element in the array and by doing stack.arr[stack.top] you will retrieve the top element of the stack.

Related

Selection Sort using a pointer and without loops

I am trying to use a recursive function to sort an array in ascending order. The catch is I can't use any for, while, or do/while loops. There are plenty of selection sort resources online but I'm having difficulty finding anything without loops and also including a pointer.
A short step-by-step of what I'm trying to do.
Place the marker at the first element of the array
2.If the marker is pointing at the last element of the array, then stop. Otherwise continue
3.Find the smallest element to the right of the marker
4.If this element is smaller than the element the marker is pointing at, then swap
5.Advance the marker to the next element to the right
6.Go to step 2
At the very least, you can design it using loops, and convert the loops.
Any loop can be converted into the following form:
State state = init();
while (cond(&state))
body(&state);
tail(&state);
And this loop be implemented trivially using recursion.
void recursive(State *state) {
if (!cond(state))
return;
body(state);
recursive(state);
}
State = state = init();
recursive(&state);
tail(&state);
That said, it's a lot easier to tackle this problem head on.
Selection sort is trivial to implement using recursion. As a whole, you are repeatedly sorting an ever shrinking portion of the array. After swapping the first element for smallest element, you need to sort the remainder of the array, which can easily be done using a recursive call.
That leaves the loop to find the smallest element of the array. Again, this is trivial to define in terms of itself. The smallest element of a list is the smallest of first element and the smallest element of the remainder of the list.

Removing a specific element from an array by name in AS3

I would like to remove a specific element from an array, not by index because the index value of that item is not static.
myarray.splice(myclip, 1);
When I use this code instead flash removes the first element in the array.
Is there something I am missing here?
Documentation of splice()
Both parameters need to be integers, the first one is the position of the element you want to delete, and the second one is the amount of elements you want to delete. Try myarray.splice(myarray.indexOf(myclip),1);
Don't know why it would only remove the first element in your snippet, maybe internally it casts myclip to 0? Doesn't matter, use indexOf. If that doesn't work, for loop through the array to get the position first.

C: deleting elements mid array and have previous pointers working

I have several arrays like this (please ignore specific names):
static resource_t coap_cmp_res[MAX_CMPS];
e.g. [cmp1,cmp2,cmp3,cmp4,cmp5,0,0,0]
and a code that uses these elements, for example, coap_cmp_res[4] (cmp5) is associated with a REST resource, call it Res5.
At a certain point in time, I delete an element in that array at position x like this:
rest_deactivate_resource(&coap_cmp_res[x]);
e.g. for x = 2
[cmp1,cmp2,0,cmp4,cmp5,0,0,0]
What I then would like to do is have a single continuous array again like this
e.g. [cmp1,cmp2,cmp4,cmp5,0,0,0,0]
What I do currently is:
for(UInt8 i = x; i < MAX_CMPS; i++){
coap_cmp_res[i] = coap_cmp_res[i+1];
}
which gives [cmp1,cmp2,cmp4,cmp5,cmp5,0,0,0]
then I manually set the last non-zero element to 0.
e.g. [cmp1,cmp2,cmp4,cmp5,0,0,0,0]
So, this looks good, but the problem is that the Res5 is still associated with coap_cmp_res[4] and thus now the value 0, instead of cmp5, which is not what I desire.
I could deactivate and reactivate every resource after x in the array to have the associations working again, but was wondering if there was a more efficient way to go about this.
Hopefully this makes sense.
As the proverb says: "add a level of indirection". An array of resource_t* that point into coap_cmp_res and are stable. Then have Rea5 associated with a pointer, and use the indirection to reach into a valid entry.
static resource_t coap_cmp_res_data[MAX_CMPS];
static resource_t* coap_cmp_res_ptrs[MAX_CMPS]; // points into coap_cmp_res_data
When you remove an element, you update the entries in coap_cmp_res_ptrs, without moving them, and shrink coap_cmp_res_data. Any resource will still refer to the same position in coap_cmp_res_ptrs, and the indirection will take it to the current location of the resource.
An alternative approach, which may prove better in your case (you'd have to profile), is to use node based storage. I.e a linked list.

Array Based Implementation of a Vector

Homework Assistance
Describe an array based implementation of a vector such that inserting and
removing at beginning and end of the vector can be done in constant time. Argue
convincingly.
Obviously this is impossible with a straight-up array. If you remove from the front, there will be a hole that needs to be filled in order to maintain the vector property. Of course, if we grab the next element over, we will need to do this n times, so the runtime will be linear, not constant.
Another way would be to grab the last element and stick it in the front, but what good is a data structure that scrambles your data?
What I have done so far is to create an array. The odd number indices are behind some point in the array (preferably the middle for size purposes, but it can be anywhere), then the even number indices are before that point. That takes up a whole bunch of memory and has lots of open slots if that special point is not the centre point. Worst case being 2n. However, it acts like there are no holes because it will always fill the next element out.
Insertion:
private int front = 0;
private int back = 0;
public void insertAtFront(int element)
{
(front+1));
dataArray[2*(front + 1) + 1] = element;
front++;
}
public void insertAtBack(int element)
{
dataArray[2*(back+1)] = element;
back++;
}
For removal, just decrement the front or the back. Then when accessing the array, only allow the values between front and back to be shown.
First, does this meet the requirements of a vector? Second, when removing, I am having some major issues figuring out how to get past that special centre point. Say you want to remove the entire array from the front, when you added everything from the back.
Thank you for any assistance.
The secret is to use two arrays. The end of the first array is the "front". The end of the second array is the "back".
I don't understand what you're trying to do with even and odd indices. But having a start index and an end index is basically the way to go - leave space empty at the front so that you can add elements there, and increment the start index again if you remove an element.
Another option is to use a circular array to allow you to add/remove both at the front and at the end efficiently.
There are other variations that could be applied: can you also find an implementation such that inserting/removing at the start, the end and (exactly) in the middle is efficient and has O(1) time?

Array to Linked List C

I am trying to convert an array to an linked list.
so basically, im gonna have a structure called "head" which will be the first element
and node, which will be the other elements.
any ideas so i can it started?
I don't see any solution simpler than just iterating through the array and appending the elements to the list.
The standard way to implement linked lists in C is with a single node structure containing a data member and a next pointer. Every time you want a new node, malloc space for it and set the next pointer of the last node in the list to point to it. The last node's next pointer should point to NULL.
You only have to hold onto a regular pointer to the first element. That's your head pointer.
Without using malloc you won't be able to easily add a new node to store data in so its best to just use the array to avoid the mess (but why can't you use malloc now?)

Resources