c - Remove element at a given index of a Stack that is a part of linked list of stacks -
i building stacklist. linked-list of stacknodes. each stack on stacknode of constant size (it int array). if stack full use malloc create new stacknode , append stacklist.
i need implement function call
int popat(int index);
which should return element specified index.
scenario:
each stack has length = 10;
stacklist has total of 5 stacks. number of elements in stacklist 10 per stack * 5 sets = 41 - 50 ( @ max can 50). want remove element @ index = 25. means @ 2ed stacknode of stacklist , 4th index in stacknode's stack. when remove 25th element, need push every element on top of down 1 element.
my solution:
copy elements on 2nd stack above 24th element 1 position down. { copy next stacknode->stack's first element end of current stacks //problem here select next stacknode } while ( linked list has stacknodes)
problem:
want shift elements in stack 1 position down. possible without iterating through array(a stack on each stacknode) because if have n number of stacknode need iterate thorough n * stack size. i.e. if 3ed stack start @ 0x8000 actualy data starts @ 0x8004 since remove first element , appended end of 2nd stack. need put value 0x8004 0x8000 , on until 0x8028 0x8024, continue doing other stacks. there better solution using loop move every element. know function memcpy since memory addresses overlap not sure if idea pass in
memcpy(0x8000, 0x8004, 9) //assume stack size 10;
note:
linked list of type stacknode defined following struct
struct stacknode { int* stack; stackset* nextstack; stackset* previousstack; }
hope clear enough :) thanks.
you right, memcpy()
doesn't work if memory areas overlapping.
for such cases there exists different function, memmove()
. works same memcpy()
contains additional checks handle overlapping memory regions.
Comments
Post a Comment