字符串反转 算法

xiangqi 写道
字符串比如“ad2 lsdkf, lksdjf. sdkfj”倒序排列"sdkfj lksdjf. lsdkf, ad2"  每个单词是空格分开,标点符号当作字母,不能用String的自带的一些方法,比如indexof,trim,split等方法。

大概的伪码,字符串大的话可以用 StringBuffer 
Stack T; 
String S; 
for char c : origString 

   if(c == ' ') 
   { 
      T.push(S); 
      S = ""; 
   } 

   S += c; 


T.push(S); 

String destString; 

while(String str = T.pop() != null) 

   if(destString == null) destString = str; 
   else 
      destString += " " + str; 


中文需要单独处理的,一个中文占两个字节,反转时顺序不变。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void reverse(char* s)
{
	int len = strlen(s);
	char* pNewStr = (char*)malloc(len + 1) ;
	char* pNewMove = pNewStr;
	char* pStr = s + len - 1;
	while(pStr >= s)
	{
		unsigned char ch = *pStr;
		if(ch > 127) //中文判断 不太确定,这个条件是否严谨,在本机测试没问题
		{
			*pNewMove = *(pStr - 1);
			pNewMove ++;
			*pNewMove= *pStr;
			pNewMove ++;
	
			pStr -= 2;
		}else
		{
			*pNewMove =*pStr;
			pNewMove ++;
			pStr--;
		}
	}
	pNewStr[len] = '\0';
	strcpy(s,pNewStr);
	free(pNewStr);
}
int main()
{  
	char str[201];
	printf("输入要反转的字符串\n");
	scanf("%s",str);
	reverse(str);
	printf("反转后字符变为:\n %s \n",str);
	system("pause");
	return 0;
}
 
栈的方法

 
 
xiangqi 写道
字符串比如“ad2 lsdkf, lksdjf. sdkfj”倒序排列"sdkfj lksdjf. lsdkf, ad2"  每个单词是空格分开,标点符号当作字母,不能用String的自带的一些方法,比如indexof,trim,split等方法。
大概的伪码,字符串大的话可以用 StringBuffer  Stack T;  String S;  for char c : origString  {     if(c == ' ')     {        T.push(S);        S = "";     }     S += c;  }  T.push(S);  String destString;  while(String str = T.pop() != null)  {     if(destString == null) destString = str;     else        destString += " " + str;  } 
递归方式

struct link_node *link_convert(struct link_node *pre_node, struct link_node *cur_node)

{

     struct link_node *tmp_node;

 

    tmp_node = cur_node->next;

    cur_node->next = pre_node;

     if (NULL == tmp_node)

            return cur_node;

 

    return link_convert(cur_node , tmp_node);

}

建议写变量名,函数名的时候根据设计的代码的功能来写,别人看代码的时候看定义的名字就知道想实现什么功能,
“见名知义”,有利于代码的可读性和维护性。
在main()函数里调用的时候是这样link_convert(NULL, pnode),确保第一个参数为NULL。你自己想想吧,递归链表转置很简单的。


 

C++ 使用递归及非递归两种方法,编程实现单向链表的反转。

不知道你是需要用STL来写还是类似C的写法,给个简单的例子吧

#include "stdafx.h" #include "malloc.h" #include "ostream.h"

typedef struct _Node {  int value;  _Node * pNext; }Node;

Node * InitList() {

 Node * pHead = (Node*)malloc(sizeof(Node));

 Node * pNode = pHead; pHead->value = 0;

 for(int i = 1; i < 50; i ++)  {   pNode->pNext = (Node *)malloc(sizeof(Node));   pNode = pNode->pNext;   pNode->value = i;   pNode->pNext = NULL;  }  return pHead;  } //返回尾节点 Node * Revert1(Node * pHead) {  Node * pfather = pHead;  Node * pNode;  if(!pfather) return NULL;  pHead = pHead->pNext;  pfather->pNext = NULL;  while(pHead != NULL)  {   pNode = pHead->pNext;   pHead->pNext = pfather;   pfather = pHead;   pHead = pNode;  }  return pfather; } //返回尾节点 Node * Revert2(Node * pHead, Node * pfather = NULL) {  Node * ret = NULL;  if(!pHead) return NULL;  if(pHead->pNext)   ret = Revert2(pHead->pNext, pHead);  pHead->pNext = pfather;  if(!ret)   return pHead;  else   return ret; }

void PrintNode(Node * pNode) {  while(pNode)  {   cout<<pNode->value<<" ";   pNode = pNode->pNext;  } } int main(int argc, char* argv[]) {  Node * pNode = Revert2(InitList());  PrintNode(pNode);  return 0; }

revert1是迭代,revert2递归


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值