题解 | #遍历链表#

遍历链表

http://www.nowcoder.com/practice/7d348aa8b7d24e01a4f10bd023e2fb54

#include #include using namespace std;

typedef struct linknode{ int data; linknode * next; }linknode, *node;

int Partition(vector &a,int left,int right){ int pivot = a[left]; while(left < right){ while(a[right] >= pivot && left < right) right--; a[left] = a[right]; while(a[left] <= pivot && left < right) left++; a[right] = a[left]; } a[left] = pivot; return left; }

void QuickSort(vector &a, int left, int right){ if(left < right){ int pivotpos = Partition(a, left, right); QuickSort(a, left, pivotpos); QuickSort(a, pivotpos + 1, right); } } int main(){ int num; cin >> num; vector a(num); for(int i = 0;i < num; i++) cin >> a[i]; QuickSort(a, 0, num - 1); int c = num - 1; node head = (node)malloc(sizeof(linknode)); head->next = NULL; while(c != -1){ node p = (node)malloc(sizeof(linknode)); p->data = a[c]; p->next = head->next; head->next = p; c--; }
while(head->next != NULL){ head = head->next; cout << head->data << " "; } }

全部评论

相关推荐

刘湘_passion:太强了牛肉哥有被激励到
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务