开发者

How do I traverse in a single linked list to a specific node with name age and height?

Now say theres a linked list with 4 nodes.With name age and height. Now I want to move the pointer to the second node and 4th node so that I could insert a new node inbetween the 2nd and 3rd node of the linked开发者_StackOverflow中文版 list.

How will I go about this and how will I add values of name, age and height in the new node to be inserted?

The new node's values has to be inserted by the programmer and not the user.

Help will be much appreciated :(


OK... Suppose it:

struct Node
{
    ...
    Node *next;
}

Node second, aux, third;

Algorithm:

second.next = aux;
aux.next = third;


See the following example:

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

typedef struct _node_
{
    char *c1;
    char c2[];
    string str;
}node_type;
int main()
{
    node_type n;
    n.c1 = new char[6];
    n.c2 = new char[6];
    strcpy(n.c1, "HELLO");
    strcpy(n.c2, "HELLO");
    n.str("HELLO");
    cout << n.c1 << endl;
    cout << n.c2 << endl;
    cout << n.str << endl;
    return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜