求助

您好,我问2个问题,请大家尽量帮助我。谢谢。
1.实现字符串拷贝功能。
char* strcpy(char* dst,char* src) /把src内存位置的字符串拷贝到dst 内存位置
2.实现链表插入功能。(在pHead 头节点后面,第i个位置插入一个新节点pNode)
struct CNode{CNode *pNext;CNode *pprev;};
BOOL Inerset(CNode *phead,int i, CNOde *pNode)\\插入成功返回TRUE,其他FALSE
This is my new favourite question :)

I'm going to guess the following as the answer to question one:

while(*dst++ = *src++);
My mother language is chinese, but this is an english forum of C/C++
we better key in english rather than chinese
be polite, dude

I don't think it's rude to post in a language other than English. OP's just reducing the number of people who can help him.

Though I hope in the near future things like google translate will be good enough that we can post in whatever language we want. Now it gives
Hello, I asked two questions, please try to help me. Thank you.
1. for string copy functions.
char * strcpy (char * dst, char * src) / the memory location of the string src to dst memory location copy
2. to achieve the list insertion. (behind the head node in pHead, the first i positions to insert a new node pNode)
struct CNode {CNode * pNext; CNode * pprev;};
BOOL Inerset (CNode * phead, int i, CNOde * pNode) \ \ into the successful return TRUE, FALSE other
I don't see questions there..
Thanks, hamsterman, I hope other's users of this forum would not reject other's language too

I would try my best to translate his questions
I would mark my deduction by bold

1 : he define a function looks like
char* strcpy(char* dst,char* src)
he wants to copy the char which src point to to the dst
since strcpy is a function of C, I think he maybe want to know
how could strcpy did this


2 : he wants to implement a linked list(I don't know it is double or singly)
struct CNode{CNode *pNext;CNode *pprev;}; I guess pNext is the node point to the next node, pprev is the node point to the previous node BOOL Inerset(CNode *phead,int i, CNOde *pNode)
this function wants to insert a new node the poistion at int i phead is the first position of the linked list, pNode is the node he wants to insert

ps:I can't make sure I could 100% translate and express it correctly since english is not my mother language
Thanks for the help
Last edited on
In which case I stand by my previous (psychically generated) answer :)
Now the real question is wiether the answer will maintain enough of it's integrity to survive another Google translate and be useful to the OP.

@ stereoMatching: I studied Mandarin for a while and I am convinced that anyone who can learn English as a secondary language, Pin-Yin AND C++ must have magical powers. I DEMAND YOU SHARE YOUR MAGIC POWERS!!! :D seriously though I'm impressed.
樓主,我回答一下你的問題好了
1:
以下是strcpy其中一種可能的實現方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 void STRCPY(char* dst, char* src)
{
  size_t const SRC_LENGTH = strlen(src) + 1;
  for(size_t i = 0; i < SRC_LENGTH; ++i)
    dst[i] = src[i];
}

void STRCPY(char* dst, char const* SRC)
{
  size_t const SRC_LENGTH = strlen(SRC) + 1;
  for(size_t i = 0; i < SRC_LENGTH; ++i)
    dst[i] = SRC[i];
}

void testCStyleChar()
{
  char const* s = "abcde";
 
  char x[10] = "hahaha";
  char y[10];

  STRCPY(y, x); //呼叫void STRCPY(char* dst, char* src)
  cout<<y<<"\n";
  STRCPY(y, s);
  cout<<y<<"\n";//呼叫void STRCPY(char* dst, char const* src)
}

以上的程式可以自行加入溢位檢查
不過即然你都使用C++,還是用std::string吧

1
2
3
4
5
6
void testString()
{
  std::string D("wahahah");
  std::string E = D;
  cout<<D<<"\n"<<E<<"\n";
}

乾淨俐落,簡單明快
如果對速度無特別的要求(stack的速度還是比heap快的多),建議直接用std::string,畢竟比較安全
不要自己動手寫一個動態char陣列,這只會讓事情變的更複雜
Last edited on
資料結構沒學好,多花了不少時間
使用純C實作很不方便,我直接使用C++風格的寫法(透過destructor回收資源比較方便)
其實若借用stl可以寫的更簡單
不過樓主你現在需要學習的應該是指標怎麼指跟記憶體怎麼分配吧?
所以我用純指標實作

這個link list是singly link list
雖然樓主的要求應該是double link list
但是只要會用指標和分配記憶體,要改成double link list應該不難
本資料結構作的非常不完善,僅供參考
(this data structure is cheap and incomplete, just a reference)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED

template<typename T> class linkIterator;
template<typename T> class linkList;

template<typename T>
class linkNode
{
  friend class linkIterator<T>;
  friend class linkList<T>;

  private:
   T m_data;
   linkNode* m_next;
};

template<typename T>
class linkIterator
{
  public:
    explicit linkIterator();

    void operator=(linkNode<T>* node)
    {
      m_node = node;
    }

    T& operator*()
    {
      if(m_node)
        return m_node->m_data;
    }

    void operator++()
    {
      if(m_node)
        m_node = m_node->m_next;
    }

    void operator++(int)
    {
      if(m_node)
        m_node = m_node->m_next;
    }

    bool operator!=(linkNode<T>* node)
    {
      return node != m_node;
    }

    bool operator==(linkNode<T>* node)
    {
      return node == m_node;
    }

  private:
    linkNode<T>* m_node;
};

template<typename T>
linkIterator<T>::linkIterator()
:m_node(0)
{}

template<typename T>
class linkList
{
  public:
    linkList();
    ~linkList();
    linkNode<T>* begin()
    {
      if(m_root)
        return m_root;
    }

    linkNode<T>* end()
    {
      return 0;
    }

    void insert(T newData, size_t const POSITION);
    void push(T newData);
    void pop();

    size_t size()
    {
      return m_size;
    }


  private:
    size_t m_size;
    linkNode<T>* m_root;
    linkNode<T>* m_lastNode;
};

template<typename T>
linkList<T>::linkList()
:m_size(0),
 m_root(0),
 m_lastNode(0)
{}

template<typename T>
linkList<T>::~linkList()
{
  while(m_root)
    pop();
}

template<typename T>
void linkList<T>::insert(T newData, size_t const POSITION)
{
  if(!POSITION)
  {
    linkNode<T> *newNode = new linkNode<T>;
    newNode->m_data = m_root->m_data;
    newNode->m_next = m_root->m_next;
    m_root->m_next = newNode;
    m_root->m_data = newData;
    return;
  }

  if(POSITION < m_size)
  {
    linkNode<T>* currentNode = m_root;
    size_t const INPOS = POSITION - 1;
    for(size_t i = 0; i < INPOS; ++i)
      currentNode = currentNode->m_next;
    
    if(currentNode != m_lastNode)
    {      
      linkNode<T> *newNode = new linkNode<T>;
      newNode->m_data = newData;
      newNode->m_next = currentNode->m_next;
      currentNode->m_next = newNode;
    }
    else
      push(newData);
  }
}

template<typename T>
void linkList<T>::push(T newData)
{
  linkNode<T>* node = new linkNode<T>;
  node->m_data = newData;
  node->m_next = 0;
  if(m_size)
  {
    m_lastNode->m_next = node;
    m_lastNode = node;
  }
  else
  {
    m_root = node;
    m_lastNode = node;
  }
  ++m_size;
}

template<typename T>
void linkList<T>::pop()
{
  if(m_root)
  {
    if(!m_root->m_next)
    {
      delete m_root;
      m_root = 0;
    }
    else
    {
      linkNode<T>* temp = m_root;
      while(temp->m_next != 0 && temp->m_next != m_lastNode)
      {
        temp = temp->m_next;
      }
      delete m_lastNode;
      temp->m_next = 0;
      m_lastNode = temp;
    }
    m_size = (m_size == 0 ? 0 : --m_size);
  }
}

#endif // LINKLIST_H_INCLUDED 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void testLinkList()
{
  linkList<size_t> haha;
  haha.push(0);
  haha.push(1);
  haha.push(2);
  haha.push(3);
  haha.push(4);
  haha.insert(5, 1);
  haha.insert(6, 1);
  haha.insert(7, 0);
  linkIterator<size_t> it;
  for(it = haha.begin(); it != haha.end(); ++it)
    cout<<*it<<"\n";

}

有什麼問題歡迎請教

其實,如果沒有什麼特別的要求,直接用std::list就好了
1
2
3
4
5
6
7
8
9
10
void testList()
{
  std::list<size_t> haha;
  haha.resize(10);
  std::iota(haha.begin(), haha.end(), 0);
  std::list<size_t>::iterator it = haha.begin();
  std::advance(it, 5);
  haha.insert(it, 33);
  std::copy(haha.begin(), haha.end(), std::ostream_iterator<size_t>(cout, "\n") );
}

stl和generic programming是C++的兩大神兵,學C++卻不碰這兩者
會失去很多的樂趣及學習的機會
也將無法發揮出C++的性能
用心學習stl和generic programming不但能提高編程效率
還能讓學習的人學到另一種寫程式的方法和思考方式


I don't know how to make the iterator act like the iterator of stl
like
 
std::vector<T>::const_iterator it;

do anyone know how to do it?
Thank you very much
Last edited on
Topic archived. No new replies allowed.