0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## 部分链表操作总结

shy丶gril 2016-05-18 11:50:15 浏览472

``````#include <iostream>
#include <cstdlib>
using namespace std;

// definition of Node
struct Node
{
int val;
Node *next;
Node(int x) : val(x), next(NULL){}
};

// create a linklist with n nodes
Node* create(int n)
{
Node *head = new Node(rand() % 100);
for (int i = 1; i < n; i++)
{
Node *newnode = new Node(rand() % 100);
pre->next = newnode;
pre = newnode;
}

}

// insert node with value num
{
Node *q;
while (p != NULL && p->val < num)
{
q = p;
p = p->next;
}

Node *newnode = new Node(num);
{
}
else if (p == NULL)
{
q->next = newnode;
}
else
{
newnode->next = q->next;
q->next = newnode;
}

}

// delete node with value num
{
Node *q;
while (p != NULL && p->val != num)
{
q = p;
p = p->next;
}

{
delete p;
}
else if (p == NULL)
{
}
else
{
q->next = q->next->next;
delete p;
}

}

{
int len = 0;
{
++len;
}

return len;
}

{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (p->val > p->next->val)
{
int temp = p->val;
p->val = p->next->val;
p->next->val = temp;
}
p = p->next;
}
}

}

{
{
}

while (cur != NULL)
{
pre->next = cur->next;
cur = pre->next;
}

}

{
{
}

}

{
{
}
cout<<endl;
}

{
Node *p;
{
}
}

int main()
{