Sokurenko 42 Junior Poster
Sokurenko 42 Junior Poster

post your code we will comment

Sokurenko 42 Junior Poster

+1 for undefined, cause printf in your project library maybe written differently or that can be compiler specific no ?
anyway why would someone write like that and care about it?? :))

Sokurenko 42 Junior Poster

malloc new bigger size, copy all data there :) free old memmory and make old pointer to point to new memmory i think this is the concept

Sokurenko 42 Junior Poster

After 7 years one would hope said junior computer student has been in the industry for a few years and can finally understand pointers... ;o)
Maybe it would be better if people stop resurrecting long dead threads with worthless posts.

sorry i needed fast solution and found this thread on google, maybe this answer will help to someone

Sokurenko 42 Junior Poster

you might use my stack for examle it should work

#include <stdio.h>
#include <stdlib.h>

#define ER_EMPTY_STACK -1

#define MSG_EMPTY_STACK "Stack is Empty\n"
#define LOG_ENABLED
struct stElem
{
   int iData;
   stElem *stNext;
};

stElem *stStartPtr = NULL;

int iPrintList()
{
   stElem *stCurrent;
   stCurrent = stStartPtr;
   int iElementCount = 1;
   while(stCurrent != NULL)
   {
      printf("#%d [%d] ",iElementCount ,stCurrent->iData);
      stCurrent = stCurrent->stNext;
      iElementCount++;   
   }
   if(iElementCount != 1 )
   {
      printf("\n");
   }
   printf("Total of elements [%d]\n",iElementCount - 1);   
   return iElementCount - 1; 
}

int iPop()
{
   int iSavedData = ER_EMPTY_STACK;
   stElem *stTmp = NULL;
   if(stStartPtr != NULL)
   {
      iSavedData = stStartPtr->iData;
      #ifdef LOG_ENABLED
      printf("Pop [%d]\n", iSavedData);
      #endif
      stTmp = stStartPtr;
      stStartPtr = stStartPtr->stNext;
      free(stTmp);
   }
   else
   {
      iSavedData = ER_EMPTY_STACK;
      printf(MSG_EMPTY_STACK);
   }
   return iSavedData;
}
int iPush(int iData)
{
   stElem *stTmp = NULL;

   if(stStartPtr == NULL)
   {
      stStartPtr = (stElem*)malloc(sizeof(stElem));
      stStartPtr->stNext = NULL;
      stStartPtr->iData = iData;
      #ifdef LOG_ENABLED
      printf("Push to empty stack [%d]\n", stStartPtr->iData );
      #endif
   }
   else
   {
      stTmp = (stElem*)malloc(sizeof(stElem));
      stTmp->iData = iData;
      stTmp->stNext = stStartPtr;
      stStartPtr = stTmp;
      #ifdef LOG_ENABLED
      printf("Push [%d]\n", stTmp->iData);
      #endif
   }

   return 0;
}

void vLinkedListProgram()
{
   int iCounter = 50000;
   printf("Press to allocate");
   getchar();
   while(iCounter-- > 0 && iPush(iCounter) == 0); 
   printf("Press to free");
   getchar();
   while(iPop() != ER_EMPTY_STACK);

   /*iPop();
   iPop();
   iPop();
   iPush(888);
   iPush(999);
   iPrintList();
   iPop();
   iPop();
   iPrintList();*/
}

int main()
{

   while(1)
   {
      vLinkedListProgram();
   }
    return 0;
}
Sokurenko 42 Junior Poster

Thanks for your reply.
Actually i don't know what should i do with push_back! is there any replace things for push_back?!
Thanks

yes, it means you haveto write your own vector,list
or write it to an end of array

Sokurenko 42 Junior Poster

use struct, write your own vector

Sokurenko 42 Junior Poster

dont use printf(buffer); to debug

memset(buffer,0,sizeof(buffer);
//fill it with data
for (i=0;i<sizeof(buffer);i++)//print every element
{
    printf("[%c]",buffer[i]);//or printf("[%d]",buffer[i]);
}
Sokurenko 42 Junior Poster

use array index and no need to return nothing, just print name afterwards :)
by the way you are not modifying char* and returning from a function, you are modifying the data that pointer points to and returning pointer to that data

Sokurenko 42 Junior Poster

I've mentioned the line numbers where i'm facing probs..!!!

    if(head != NULL)//check does head point somewhere on initialised memmory cause after free it should point to NULL
    {
        if(strcmp(del_c,head->c) != 0)//does it have data that we need
        {
           //handle
        }
    }
Sokurenko 42 Junior Poster

please comment your code next time, or explain it to your cat hope this helps :)

void del()
{
   XY:
   char del_c[30];
   printf("Enter the name of city to be deleted: ");
   scanf("%s",del_c);
   struct city *tmp,*prev;
   tmp = head;
   if(strcmp(del_c, head->c) == 0)//if data that head points to matches or just segfault if null pointer
   {
      head = head->next;//make head point to next element
      free(head);//delete next element
   }
   else
   {
      while(strcmp(del_c,tmp->c) != 0)//while does not match // better to look for NULL pointer before accessing
      {
         prev = tmp;
         tmp = tmp->next;//search from head till the system error or we find something before
      }
      prev->next = tmp->next;//we found something or not, lets make something point somewhere after next in linked list
      free(tmp);//free something maybe would be ok if we find something
   }
   if(strcmp(del_c,head->c) != 0 || strcmp(del_c,tmp->c) != 0)//head might not exist - seg fault or tmp
   {
      printf("City is not there in list");
      goto XY;//using goto is bad practise
   }
}
Sokurenko 42 Junior Poster

i think this should work any idea how to do better ?

void vRemoveDelimiters(char *pcBuffer, char* pcDelimiters)
{
   int i = 0;
   int iLen = strlen(pcBuffer);

   for(i = 0 ; i < iLen; i++)
   {
      if( strchr(pcDelimiters, pcBuffer[i]) != NULL )
      {
         memcpy(&pcBuffer[i], &pcBuffer[i + 1], iLen - i);
         iLen--;
         i--;
      }

   }
   return;
}
Sokurenko 42 Junior Poster

maybe this ? please review

void vRemoveDelimiters(char *pcBuffer, char* pcDelimiters)
{
   int j = 0;
   int i = 0;
   int iLen = strlen(pcBuffer);

   for(i = 0 ; i < iLen; i++)
   {
      if( strchr(pcDelimiters, pcBuffer[i]) != NULL )
      {
         j = i;
         if(strchr(pcDelimiters, pcBuffer[j + 1]) != NULL)//next is delimiter, start from this position agayn after
         {
            i--;
         }
         memcpy(&pcBuffer[j], &pcBuffer[j + 1], iLen - i);
         iLen--;
      }

   }

   return;
}