聯發科筆試題編程部分
(1)實現鏈表的逆置 node
- node *inverselinklist(node *head)
-
- {
- node *p1,*p2,*p3;
- if(NULL==head||NULL==head->next) {
- return head;
- }
- p1=head;
- p2=p1->next;
- while (p2) {
- p3=p2->next;
- p2->next=p1;
- p1=p2;
- p2=p3;
- }
- head->next=NULL;
- head=p1;
- return head;
- }
(2)用普通算法實現兩個有序鏈表的合併 算法
- node *mergelinklist(node *head1,node *head2)
- {
- node *p1,*p2,*temp=NULL,*cur=NULL,*head=NULL;
- p1=head1;
- p2=head2;
- while (p1!=NULL&&p2!=NULL) {
-
- if (p1->value<p2->value) {
- temp=p1;
- p1=p1->next;
- }
- else
- {
- temp=p2;
- p2=p2->next;
- }
- if (NULL==head) {
- cur=temp;
- head=cur;
- }
- else
- {
- cur->next=temp;
- cur=temp;
- }
-
- }
- temp=(p1?p1:p2);
- while (temp!=NULL) {
- cur->next=temp;
- cur=cur->next;
- temp=temp->next;
- }
- cur->next=NULL;
- return head;
- }
(3)用遞歸算法實現兩個有序列表的合併 app
- node *mergeRecursion(node *head1,node *head2)
- {
- if(NULL==head1)
- {
- return head2;
- }
- if(NULL==head2)
- {
- return head1;
- }
- node *head=NULL;
- if (head1->value<=head2->value) {
- head=head1;
- head->next=mergeRecursion(head1->next,head2);
- }
- else
- {
- head=head2;
- head->next=mergeRecursion(head1,head2->next);
- }
- return head;
- }
(4)判斷單鏈表中是否存在環 oop
- bool IsExitsLoop(slist *head)
- {
- slist *slow = head, *fast = head;
- while ( fast && fast->next )
- {
- slow = slow->next;
- fast = fast->next->next;
- if ( slow == fast ) break;
- }
- return !(fast == NULL || fast->next == NULL);
- }
(5)二分查找法 spa
- int binSearch(int key,int length, int array[])
- {
- int mid=0;
- int start = 0;
- int end = length - 1;
- while (start <= end) {
- mid = (end - start) / 2 + start;
- if (key == array[mid]) {
- return mid;
- }
- if (key < array[mid]) {
- end = mid - 1;
- } else if (key > array[mid]) {
- start = mid + 1;
- } else {
- return mid;
- }
- }
- return -1;
- }
歡迎關注本站公眾號,獲取更多信息