小编Reb*_*_87的帖子

如何使用冒泡排序对链表进行排序?

我试图使用冒泡排序来排序链表.我使用curr和trail来遍历列表.curr应该始终领先于一步.到目前为止这是我的代码:

void linked_list::sort ()
{
  int i,j=0;
  int counter=0;
  node *curr=head;
  node *trail=head;
  node *temp=NULL;

  while (curr !=NULL)
  {
    curr=curr->next;    //couting the number of items I have in my list. 
    counter++;          //this works fine.
  }

  curr=head->next;          // reseting the curr value for the 2nd position.

  for (i=0; i<counter; i++)
  {
    while (curr != NULL)
    {
      if (trail->data > curr->data)
      {
        temp=curr->next;      //bubble sort for the pointers.
        curr->next=trail;
        trail->next=temp;

        temp=curr;         //reseting trail and curr. curr gets back to be infront.
        curr=trail; …
Run Code Online (Sandbox Code Playgroud)

c++ sorting pointers linked-list bubble-sort

2
推荐指数
2
解决办法
4万
查看次数

模数的时间复杂度分析

 sum = 0;
 for(i=1;i<2*n;i++)
    for(j=1;j<i*i;j++)
      for(k=1;k<j;k++)
        if (j % i == 1)
          sum++;
Run Code Online (Sandbox Code Playgroud)

我需要用大O表示法来计算这段代码的时间复杂度.我理解如何在非常基础的层面上分析时间复杂度.我的问题是最终内循环中的条件.如何将其集成到我的分析中?

complexity-theory big-o asymptotic-complexity

2
推荐指数
1
解决办法
898
查看次数

如何更改函数中变量的范围?蟒蛇

这似乎是一个非常愚蠢的问题,但是我对Python的范围规则感到困惑。在下面的示例中,我将带有值的两个变量(x,y)发送到应该更改其值的函数。当我打印结果时,变量没有改变。

def func1(x,y):
    x=200
    y=300

x=2
y=3

func1(x,y)

print x,y #prints 2,3
Run Code Online (Sandbox Code Playgroud)

现在,如果这是C ++,我将通过引用(&)将其发送给该函数,从而可以更改其值。那么,Python中的等价物是什么?更重要的是,将对象发送给函数时实际发生了什么?Python是否对这些对象进行了新引用?

python scope function

1
推荐指数
1
解决办法
6917
查看次数

如何为新字符串指定大小?C++

我正在尝试创建一个指向字符串的指针数组.我希望每个字符串只有3个字符.这是我到目前为止的代码:

 string **ptr=new string *[100];    // An array of 100 pointers to strings

for (i=0;i<100; i++)            // Assigning each pointer with a new string
{
    ptr[i]=new string;
    (*ptr[i])[3];
}
Run Code Online (Sandbox Code Playgroud)

我遇到线路问题(*ptr [i])[3]).如果我要创建一个只有3个字符而不是通过指针的srting,我会写:

string str[3];
Run Code Online (Sandbox Code Playgroud)

如何使用指针分配3个字符?谢谢!

c++ arrays string pointers

-3
推荐指数
1
解决办法
176
查看次数