如何从线性链接列表中删除第二个节点C ++

时间:2018-12-04 16:42:39

标签: c++

此编码是删除第一个节点,但是我想如果我学会了删除第二个节点,我将知道如何删除第一个节点,因此这是我删除第一个节点所做的操作,现在该节点将值替换为0。 / p>

private final Handler mHandler;

    DataIncomingThread(Handler handler) {
        mHandler = handler;


    }




        @Override
        public void run () {




            bt.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() {


                @Override


                public void onDataReceived(byte[] bytes, String message) {
                    int packetlength = bytes.length;

public void onDataReceived(byte[] bytes, String message) {
            int packetlength = bytes.length;

            //   new Thread(new DataPacketUnpacker()).start();


            //while (true) {


            while (true) {
                try {


                    //   Log.d("DEBUG BT-console", "IN CONNECTED THREAD RUN Datathread" + listenerbyte);


                    for (byte firstbyte : bytes) {

                        int i = 0;
                        firstbyte = bytes[i];

                        if (firstbyte == 90) {


                            solCh0 = (bytes[1] | bytes[2] >> 4 & 0xFF) & 0xFFF;
                            solCh1 = (bytes[2] << 8 & 0xFF | bytes[3] & 0x0F) & 0xFFF;
                            solCh2 = (bytes[4] | bytes[5] >> 4 & 0xFF) & 0xFFF;
                            solCh3 = (bytes[5] << 8 & 0xFF | bytes[6] & 0x0F) & 0xFFF;
                            solCh4 = (bytes[7] | bytes[8] >> 4 & 0xFF) & 0xFFF;
                            solCh5 = (bytes[8] << 8 & 0xFF | bytes[9] & 0x0F) & 0xFFF;
                            solCh6 = (bytes[10] | bytes[11] >> 4 & 0xFF) & 0xFFF;
                            solCh7 = (bytes[11] << 8 & 0xFF | bytes[12] & 0x0F) & 0xFFF;
                            solCh8 = (bytes[13] | bytes[14] >> 4 & 0xFF) & 0xFFF;
                            solCh9 = (bytes[14] << 8 & 0xFF | bytes[15] & 0x0F) & 0xFFF;
                            solCh10 = (bytes[16] | bytes[17] >> 4 & 0xFF) & 0xFFF;
                            solCh11 = (bytes[17] << 8 & 0xFF | bytes[18] & 0x0F) & 0xFFF;


                            // try {
                            //    DataIncomingThread.run();
                            //}catch (NullPointerException e){
                            //   Log.e("Data to textview", "No data to post");
                            // }


                            Log.i("Data Unpack -console", "Found Solenoid Syncbyte " + solCh0 + " " + solCh4 + " " + solCh7);


                        } else if (firstbyte == -91) {

                            int senCh0 = (bytes[1] | bytes[2] << 4) & 0xFFF;
                            int senCh1 = (bytes[2] >> 4 | bytes[3]) & 0xFFF;
                            int senCh2 = (bytes[4] | bytes[5] << 4) & 0xFFF;
                            int senCh3 = (bytes[5] >> 4 | bytes[6]) & 0xFFF;
                            int senCh4 = (bytes[7] | bytes[8] << 4) & 0xFFF;
                            int senCh5 = (bytes[8] >> 4 | bytes[9]) & 0xFFF;
                            int senCh6 = (bytes[10] | bytes[11] << 4) & 0xFFF;
                            int senCh7 = (bytes[11] >> 4 | bytes[12]) & 0xFFF;
                            int senCh8 = (bytes[12] | bytes[13] << 4) & 0xFFF;
                            int senCh9 = (bytes[13] >> 4 | bytes[14]) & 0xFFF;
                            int senCh10 = (bytes[15] | bytes[16] << 4) & 0xFFF;
                            int senCh11 = (bytes[16] >> 4 | bytes[17]) & 0xFFF;
                            int senCh12 = (bytes[18] | bytes[19] << 4) & 0xFFF;
                            int senCh13 = (bytes[19] >> 4 | bytes[20]) & 0xFFF;
                            int senCh14 = (bytes[21] | bytes[22] << 4) & 0xFFF;
                            int senCh15 = (bytes[22] >> 4 | bytes[23]) & 0xFFF;


                            // try {
                            //     DataIncomingThread.run();
                            //}catch (NullPointerException e){
                            //    Log.e("Data to textview", "No data to post");
                            // }

                            Log.i("Data Unpacker", "Found Sensor syncbyte");

                        } else {

                            //  Log.i("Data unpack", "No data to unpack" + packetlength);
                            try {
                                txtconsole.append(message);
                            } catch (NullPointerException e) {
                                //     Log.i("Data unpacker", "No console message to show");
                            }

                        }
                    }
                } catch (NullPointerException e) {

                    Log.e("Packet handling", "A problem decoding data is " + listenerbyte.toString());

                }
            }


        }
    });

1 个答案:

答案 0 :(得分:0)

您应该在自己喜欢的数据结构书中查找链接列表。 假设head指向列表的开头,并且每个节点都有一个next字段。

// Get the pointer to the second node.
Node * p_second_node = head->next;

// Copy the second node's link to the head node's link:
if (p_second_node)
{
  head->next = p_second_node->next;
}

// Finally, delete the second node:
delete p_second_node;

您应始终检查指向第二个节点的指针是否为nullptr。延迟NULL指针是未定义的行为。

编辑1-删除第一个节点
以下是删除第一个头节点的步骤:

Node * p_first_node = head;
if (head)
{
  head = p_first_node->next;
  delete p_first_node;
}

注意:在以上两种情况下,都会创建一个临时指针来指向要删除的节点。这是必需的,因为更改链接时,将无法访问该节点,并且会发生内存泄漏。临时指针允许您在更改链接后删除节点。