欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
一道百度筆試題的解決方案

 編程題:30 1

注意:要求提供完整代碼,如果可以編譯運行酌情加分。

 

 

1.    一條1百萬(wàn)節點(diǎn)的單向鏈表,鏈表所有節點(diǎn)是按value字段從小到大的順序鏈接;下面是一個(gè)節點(diǎn)的結構

 

   typedef struct node_t{

 

       int value;   /* 節點(diǎn)排序字段 */

 

       int group;   /* 組號: 0,1,2,3,4,5,6,7,8,9 */

 

       struct node_t *pnext;  /* 下個(gè)節點(diǎn)的指針 */

 

   }node_t;

 

   node_t head;    /*該單向鏈表的頭節點(diǎn),全局變量 */

 

   

 

試設計程序:針對各個(gè)group(0-->9),根據value字段排序,輸出各組top 10的節點(diǎn)。(top10是從小到大,取后面的大值top10.

 

要求:盡量減少計算復雜度、遍歷次數,不允許大量的輔助內存

慚愧,寫(xiě)了挺久才寫(xiě)出來(lái)的程序:

#include <stdlib.h>
#include <iostream.h>
#include <string.h>

typedef struct node_t {
 int value;
 int group;
 struct node_t *pnext;
}node_t;

typedef struct queueNode {
 int value;
 struct queueNode *next;
}queueNode;

typedef struct queue_t {
 queueNode *front;
 queueNode *rear;
 int size;
}queue_t;

void initQueueNode(queueNode *node,int value)

 node->value = value;
 node->next = NULL;
}

void initQueue(queue_t *q)
{
 q->front = q->rear = NULL;
 q->size = 0;
}

bool isFullQueue(queue_t *q)
{
 return q->size == 10;
}

void destroyQueue(queue_t *q)
{
 while(q->front)
 {
  queueNode *node = q->front;
  q->front = q->front->next;
  delete node;
 }
 q->front = q->rear = NULL;
 q->size = 0;
 delete q;
}

int pushQueue(queue_t *q,int value)
{
 if(q->size == 10)
 {
  cout << "queue is full" << endl;
  exit(1);
 }
 queueNode *node = new queueNode();
 initQueueNode(node,value);
 if(q->rear == NULL)
  q->rear = node;
 else
 {
  q->rear->next = node;
  q->rear = q->rear->next;
 }
 if(q->front == NULL)
  q->front = node;
 q->size++;
 return 1;
}

int popQueue(queue_t *q)
{
 if(q->front == q->rear)
 {
  cout << "queue is null" << endl;
  exit(1);
 }
 queueNode *node;
 node = q->front;
 q->front = q->front->next;
 delete node;
 q->size--;
 return 1;
}

void init(node_t *p)
{
 node_t *temp;
 for(int i=0; i<1000000; i++)
 {
  temp = new node_t();
  temp->value = i;
  temp->group = rand()%10;
  temp->pnext = NULL;
  p->pnext = temp;
  p = p->pnext;
 }
}

int top[10][10],total[10];
node_t *head,*p;
node_t *temp;
int num;
queue_t *q[10];

void main()
{
 for(int i=0; i<10; i++)
 {
  q[i] = new queue_t();
  initQueue(q[i]);
 }

 p = new node_t();
 head = p;
 init(p);
 p = head->pnext;
 while(p)
 {
  num = total[p->group] == 10 ? 9 : total[p->group];
  if(isFullQueue(q[p->group]))
  {
   popQueue(q[p->group]);   
  }
  pushQueue(q[p->group],p->value);
  total[p->group] = total[p->group] == 10 ? 10 : total[p->group]+1;
  p = p->pnext;
 }

 for(i=0; i<10; i++)
 {
  cout << "Group " << i << endl;
  q[i]->rear = q[i]->front;
  while(q[i]->rear)
  {
   cout << q[i]->rear->value << "  ";
   if(q[i]->rear->next)
    q[i]->rear = q[i]->rear->next;
   else
    break;
  }
  cout << endl;
 }

 p = head->pnext;
 while(p)
 {
  temp = p;
  p = p->pnext;
  delete temp;
 }
 delete head;

 for(i=0; i<10; i++)
 {
  destroyQueue(q[i]);
 }
}

應該說(shuō)這個(gè)程序是比較完善的,用隊列存儲結果,沒(méi)有內存泄露

又看了一下,發(fā)現自己真是會(huì )給自己找事做,用隊列去存儲。。。唉,用數組來(lái)存儲top10,最后做一次冒泡排序,多簡(jiǎn)單

#include <stdlib.h>
#include <iostream.h>
#include <string.h>

typedef struct node_t {
 int value;
 int group;
 struct node_t *pnext;
}node_t;

void init(node_t *p)
{
 node_t *temp;
 for(int i=0; i<1000000; i++)
 {
  temp = new node_t();
  temp->value = i;
  temp->group = rand()%10;
  temp->pnext = NULL;
  p->pnext = temp;
  p = p->pnext;
 }
}

int top[10][10],total[10],num[10];
node_t *head,*p;
node_t *temp;

void main()
{
 int i,j,k,t;

 p = new node_t();
 head = p;
 init(p);
 p = head->pnext;
 while(p)
 {
  top[p->group][num[p->group]] = p->value;
  num[p->group] = (num[p->group]+1)%10;
  total[p->group] = total[p->group] == 10 ? 10 : total[p->group]+1;
  p = p->pnext;
 }

 for(i=0; i<10; i++)
 {
  for(j=0; j<total[i]; j++)
  {
   for(k=total[i]-1; k>0; k--)
   {
    if(top[i][k] < top[i][k-1])
    {
     t = top[i][k];
     top[i][k] = top[i][k-1];
     top[i][k-1] = t;
    }
   }
  }
 }

 for(i=0 ;i<10; i++)
 {
  cout << "Group " << i << endl;
  for(j=0; j<total[i]; j++)
  {
   cout << top[i][j] << "  ";
  }
  cout << endl;
 }

 p = head->pnext;
 while(p)
 {
  temp = p;
  p = p->pnext;
  delete temp;
 }
 delete head;
}

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
由于未分配內存的指針導致段錯誤
C語(yǔ)言基礎——隊列詳細講解!
queue類(lèi)
今年網(wǎng)易最后一道C++筆試題是考了這樣一道題目
C++實(shí)現隊列
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久