vector<int> postorderTraversal(TreeNode* root) {

	TreeNode dump(0);
	dump.left = root;
	TreeNode *cur = &dump, *prev = NULL;
	while (cur)
	{
		if (cur->left == NULL)
			cur = cur->right;
		else
		{   //------------------------------- prev为cur的前继------------------------------------------------
			prev = cur->left;
			while (prev->right != NULL && prev->right != cur)
				prev = prev->right;

			if (prev->right == NULL)
			{
				prev->right = cur;
				cur = cur->left;
			}
			else
			{
				printReverse(cur->left, prev);  // call print
				prev->right = NULL;
				cur = cur->right;
			}
		}
	}
	return v;

}
Example #2
0
 vector<int> postorderTraversal(TreeNode* root) {
     vector<int> res;
     TreeNode dummy(0);
     dummy.left = root;
     TreeNode *cur = &dummy, *prev = NULL;
     while (cur) {
         if (cur->left == NULL) {
             cur = cur->right;
         } else {
             prev = cur->left;
             while (prev->right != NULL && prev->right != cur) {
                 prev = prev->right;
             }
             if (prev->right == NULL) {
                 prev->right = cur;
                 cur = cur->left;
             } else {
                 printReverse(cur->left, prev, res);
                 prev->right = NULL;
                 cur = cur->right;
             }
         }
     }
     return res;
 }
Example #3
0
void printReverse(char *s) {
	if(*s=='\0') {
		return;
	}
	printReverse(s+1);
	printf("%c", *s);
}
int main(){
	node* head = NULL; 
	push(&head, 1);
  	push(&head, 2);
  	push(&head, 3);
  	push(&head, 4);
  	printList(head);
  	printReverse(head);
	return 0;
}
//recursive function to print the reverse of the linked list
void printReverse(struct node *head){

	if(head==NULL)
		return;

	printReverse(head->next);

	printf("%d\n", head->data);

}
int main(int argc, const char* argv[]) {
    std::cout << "Enter a string: ";

    std::string base;
    std::getline(std::cin, base);

    printReverse(base);

    return 0;
}
Example #7
0
int main(void)
{
	char str[20] = {'\0'};
	printf("Please enter a string\n");
	gets(str);
	printf("Reversed string is:\n");
	printReverse(str);
	printf("\n");
	
	return 0;
}
Example #8
0
/* Function to reverse the linked list */
void printReverse(struct node* head)
{
  // Base case
  if(head == NULL)
    return;

  // print the list after head node
  printReverse(head->next);

  // After everything else is printed, print head
  printf("%d  ", head->data);
}
Example #9
0
void printReverse(char *str)
{	
	if(*str == '\0')
		return;
	if(*(str + 1) == '\0'){
		printf("%c", *str);
		return;
	}
	else{
		printReverse(str + 1);
		printf("%c", *str);
	}
}
void show(){

	struct node *head = top;

	if(head==NULL){

		printf("Stack is empty\n");
		return;
	}

	printReverse(head);

}
Example #11
0
int main(void) {

	struct node *head = NULL ;
	insertFirst(&head, 10);
	insertFirst(&head, 22);
	insertFirst(&head, 35);
	insertFirst(&head, 75);

	printf("Original List is \n");
	printList(head);

	printf("\nPrinting the list in reverse order\n");
	printReverse(head);


}
Example #12
0
int main(int argc, char *argv[])
{
	int len;
	char *str;

	if (argc <= 1) {
		std::cout << "You must supply me a word.\n";
		return 0;
	}

	str = argv[1];
	len = strlen(argv[1]);
	
	printReverse(argv[1], len);

	return 0;
}
Example #13
0
int main() {
	char *str="LiniL";
	printf("%d\n",isPalindrome(str));
	char *str1="LiiL";
	printf("%d\n",isPalindrome(str1));

	// int arr[10]={2,3,4,6,7,8,9,10,11,12};
	// printf("%d\n", missingNumber(arr, 10));
	int arr[2]={1,2};

	char *ip1=strdup("1.1.1.12");
	char *ip2=strdup("1.1.1.2");
	printf("%d\n", compareIps(ip1,ip2));
	char *s="Linux";
	printReverse(s);
	printf("\n");

	printf("%d\n", findMissing(arr,2));
	printf("%d\n", findMissingBinary(arr,2));
}
Example #14
0
int main()
{
	int userInput;
	struct Node* head;
	struct Node* tail;
	head = NULL;
	tail = NULL;
	
	printWelcomeMsg();
	
	userInput = 0;
	while (userInput != -1) {
		userInput = getSelection("Enter your selection: ");		
		if(userInput == 1) {
			userInput = getSelection("Enter the value to append to head: ");
			insertAtHead(&head, &tail, userInput);
		} else if (userInput == 2) {
			userInput = getSelection("Enter the value to append to tail: ");
			insertAtTail(&tail, &head, userInput);
		} else if (userInput == -1) {
			printExitMessage();
			scanf("%d", &userInput);
			printf("\n");
			if (userInput == 1) {
				printForward(tail);
			} else if (userInput == 2) {
				printReverse(head);
			} else {
				printf("\nExiting without displaying output.");
			}
			return 0;
		}
		else {
			printf("You've made an error. Please try again.\n\n");
		}
	}

	printForward(tail);
	
	return 0;
}
Example #15
0
int main(int argv, char* argc[]){
	int error = 0;
	while(!eof && readBuff() > 0){	
		if(findEndOfString() == -1){
			error = 1;
			clean();		
		} else{
			int i;
			while((i = findEndOfString()) != -1){
				if(error == 1){
					error = 0;
				} else {
					printReverse(0, i);
				}
				normalize(i + 1);
				size -= i + 1;
			}
		}
	}
	return 0;
}
int main(int argc, char * argv[]){
   int count=0;
   int u, v, index, sccNum;
   FILE *in, *out;
   char line[MAX_LEN];
   char* token;
   Graph G = NULL;
   Graph T = NULL;
   List S = newList(); // This list is our stack that determins the order of the vertices
   List R = newList();
   // check command line for correct number of arguments
   if( argc != 3 ){
      printf("Usage: %s <input file> <output file>\n", argv[0]);
      exit(1);
   }

   // open files for reading and writing 
   in = fopen(argv[1], "r");
   out = fopen(argv[2], "w");
   if( in==NULL ){
      printf("Unable to open file %s for reading\n", argv[1]);
      exit(1);
   }
   if( out==NULL ){
      printf("Unable to open file %s for writing\n", argv[2]);
      exit(1);
   }

   /* read each line of input file, then count and print tokens */
   // Assemble a graph object G using newGraph() and addArc()
   while(fgets(line, MAX_LEN, in) != NULL)  {
      count++;
	  // char *strtok(char *str, const char *delim) breaks string str into
	  // a series of tokens using the delimitrer delim. This function returns a pointer to the
	  // last token found in string. A null pointer is returned if there are no tokens left to retrieve.
      token = strtok(line, " \n");
	  
	  // int atoi(const char *str), This function returns the converted integral number as an int value.
	  // If no valid conversion could be performed, it returns zero.
	  // It converts char to int.
	  if(count == 1) {
	     // Takes in the first number as a token, sets a graph of that size
	     G = newGraph(atoi(token));
	  }
	  else {
	     // Here we want to read in both numbers
		 u = atoi(token);
		 token = strtok(NULL, " \n");
		 v = atoi(token);
		 if( u != 0 || v != 0) {
		       addArc(G, u, v);
	     }		   
         else if (u == 0 && v == 0) {
		       break;
         }			   
      } 
   }
   // Print the adjacency list representation of G to the output file
   printGraph(out, G);
   fprintf(out, "\n");
   
   // creating our Stack
   for(int i = 1; i <= getOrder(G); i++) {
      append(S, i);
   }
   
   // Run DFS on G and G-Transpose, processing the vertices in the second call
   // by decreasing finish times from the first call
   DFS(G, S);
   T = transpose(G);
   DFS(T, S);
   
   // Determine the strong components of G
   // Print the strong components of G to the output file in topologically sorted order.
   // Everytime a vertex has a NIL parent in the transpose of G, we have a SCC
   sccNum = 0;
   for(int i = 1; i <= getOrder(G); i++) {
      if(getParent(T, i) == NIL) sccNum++;
   }
   
   
   fprintf(out, "G contains %d strongly connected components:\n", sccNum);
   index = 1;
   moveTo(S, length(S) - 1 );
   while(getIndex(S) != -1 && index <= sccNum) {  
	  fprintf(out, "Component %d:", index);
	  while(getParent(T, getElement(S)) != NIL) {
		 prepend(R, getElement(S));
		 movePrev(S);
	  }
	  prepend(R, getElement(S));
	  printReverse(out, T, R);
	  movePrev(S);
	  index++;   
	  fprintf(out, "\n");
   }

   
   freeList(&S);
   freeList(&R);   
   freeGraph(&G);
   freeGraph(&T);   

   /* close files */
   fclose(in);
   fclose(out);

   return(0);
}
Example #17
0
int main (void) {
  /* program to coordinate the menu options and calls the requested function */

  struct node * first = NULL;   /* pointer to the first list item */
  char option[strMax];          /* user response to menu selection */

  printf ("Program to Maintain a List of Names\n");

  while (1) {
    /* print menu options */
    printf ("Options available\n");
    printf ("I - Insert a name into the list\n");
    printf ("D - Delete a name from the list\n");
    printf ("C - Count the number of items on the list\n");
    printf ("F - Move an item to the front of the list\n");
    printf ("L - Print the last item on the list (iteratively)\n");
    printf ("M - Print the last item on the list (recursively)\n");
    printf ("P - Print the names on the list (iteratively)\n");
    printf ("S - Print the names on the list (recursively)\n");
    printf ("R - Print the names in reverse order\n");
    printf ("Q - Quit\n");

    /* determine user selection */
    printf ("Enter desired option: ");
    scanf ("%s", option);
    
    switch (option[0])
      { case 'I':
        case 'i': 
          addName(&first);
          break;
        case 'D':
        case 'd': 
          deleteName(&first);
          break;
        case 'C':
        case 'c': 
          countList(first);
          break;
        case 'F':
        case 'f': 
          putFirst(&first);
          break;
        case 'L':
        case 'l': 
          printLast(first);
          break;
        case 'M':
        case 'm': 
          printLastRec(first);
          break;
        case 'P':
        case 'p': 
          print(first);
          break;
        case 'S':
        case 's': 
          printRec(first);
          break;
        case 'R':
        case 'r': 
          printReverse(first);
          break;
        case 'Q':
        case 'q':
          printf ("Program terminated\n");
          return 0;
          break;
        default: printf ("Invalid Option - Try Again!\n");
          continue;
      }
  }
}
void printReverse(node *head) {
	if(head == NULL)
		return;
	printReverse(head->next);
	cout<<head->data<<" ";
}
Example #19
0
int main (void) {
  /* program to coordinate the menu options and calls the requested 
     function */ 

  struct node * first = NULL;   /* pointer to the first list item */
  char option[strMax];          /* user response to menu selection */

  printf ("Program to Maintain a List of Robot Actions\n");

  while (1) {
    /* print menu options */
    printf ("Options available\n");
    printf ("I - Insert an action into the list\n");
    printf ("D - Delete an action from the list\n");
    printf ("C - Count the number of items in the list\n");
    printf ("F - Move an item to the front of the list\n");
    printf ("L - Print the last item in the list\n");
    printf ("P - Print the actions in the list and the UIDs of the nodes\n");
    printf ("R - Print the actions and UIDs in reverse order\n");
    printf ("E - Execute all the action commands contained in the list\n");
    printf ("Q - Quit\n");

    /* determine user selection */
    printf ("Enter desired option: ");
    scanf ("%s", option);
    
    switch (option[0])
      { case 'I':
      case 'i': 
        addAction(&first);
        break;
      case 'D':
      case 'd': 
        deleteAction(&first);
        break;
      case 'C':
      case 'c': 
        countList(first);
        break;
      case 'F':
      case 'f': 
        putFirst(&first);
        break;
      case 'L':
      case 'l': 
        printLast(first);
        break;
      case 'P':
      case 'p': 
        print(first);
        break;
      case 'R':
      case 'r': 
        printReverse(first);
        break;
      case 'E':
      case 'e':
        executeActions(first);
        break;
      case 'Q':
      case 'q':
        printf ("Program terminated\n");
        return 0;
        break;
      default: printf ("Invalid Option - Try Again!\n");
        continue;
      }
  }
}