Data structure on-machine exam (Kai Shen Edition)

Source: Internet
Author: User

Insert Sort

#include <cstdio>

using namespace Std;

const int MAXN = 1111;

int ARRAY1[MAXN];

int ARRAY2[MAXN];

1 2 4 7-1

void Insert (int v) {

for (int i = 0;i <= v; i++) {

if (i = = v) {//If you need to insert into the last position

Array2[i] = Array1[v];

Return

}

if (Array2[i] > Array1[v]) {//Find insertion position

Empty the position, that is, the element that starts here is all moved back one

for (int j = v; j > i; j--)

ARRAY2[J] = array2[j-1];

Array2[i] = Array1[v];

Return

}

}

}

int main () {

int n;

printf ("Please enter the number of elements with sorting:");

scanf ("%d", &n);

printf ("Enter the value of the collection element:");

for (int i = 0; i < n; i++)

scanf ("%d", &array1[i]);

Select sort

Array2[0] = array1[0];

for (int i = 1; i < n; i++)

Insert (i); Insert Element Array1[i]

printf ("The element after sorting is:");

for (int i = 0; i < n; i++)

printf ("%d", array2[i]);

return 0;

}

Binary tree operation

#include <cstdio>

#include <algorithm>

using namespace Std;

struct node{

int value; Node value

Node *left; Left child knot Point

Node *right; Right child knot Point

};

int n;

int num = 0;//leaf node number

int num2 = Depth of 0;//tree

node* buildtree (int m) {

if (M > N) return NULL;

printf ("Please enter the value of the%d/%d node:", M,n);

Node *node = (node*) malloc (sizeof (node));

int V;

scanf ("%d", &v);

node, value = V;

Node-to-left = Buildtree (M * 2);

Node-right = Buildtree (M * 2 + 1);

return node;

}

void Pre (Node *node) {//Pre-order traversal

if (node = = NULL)

Return

printf ("%d", node-value);

Pre (node-to-left);

Pre (node-right);

Return

}

void Mid (Node *node) {//middle sequence traversal

if (node = = NULL)

Return

Mid (node-to-left);

printf ("%d", node-value);

Mid (node-right);

Return

}

void End (Node *node) {//post-post traversal

if (node = = NULL)

Return

Mid (node-to-left);

Mid (node-right);

printf ("%d", node-value);

Return

}

void Count_num (Node *node) {

int son = 0;

if (node-to-left = NULL) son++;

else Count_num (node-to-left);

if (node-right = = NULL) son++;

else Count_num (node-right);

if (son = = 2)

num++;

Return

}

void Count_deep (Node *node,int deep) {

num2 = deep > num2? deep:num2;

if (node-to-Left! = NULL)

Count_deep (Node-left,deep + 1);

if (node-right! = NULL)

Count_deep (Node-right,deep + 1);

Return

}

int main () {

printf ("Please enter the number of nodes of the binary tree:"); scanf ("%d", &n);

Node *root;

printf ("Enter the node value in the order in which the pre-order traversal: \ n");

root = Buildtree (1);

printf ("Pre-order Traversal result:"); Pre (Root);p rintf ("\ n");

printf ("middle order traversal Result:"); Mid (Root);p rintf ("\ n");

printf ("post-post traversal result:"); End (Root);p rintf ("\ n");

Count_num (root); Number of leaf nodes counted.

printf ("The number of leaf nodes in a binary tree is:");

printf ("%d\n", num);

Count_deep (root,1); Depth of the statistical tree

printf ("The depth of the binary tree is:");

printf ("%d\n", num2);

}

Bubble sort

#include <cstdio>

using namespace Std;

const int MAXN = 1111l;

int main () {

int ARRAY[MAXN];

int n;

printf ("Enter the number of elements to be sorted:");

scanf ("%d", &n);

printf ("Please enter%d elements of the collection:", n);

for (int i = 0; i < n; i++)

scanf ("%d", &array[i]);

for (int i = 0; i < n; i++)

for (int j = 0; J < n-i-1; j + +) {

if (Array[j] > array[j + 1]) {

int temp = Array[j];

ARRAY[J] = array[j + 1];

Array[j +1] = temp;

}

}

printf ("The set element after sorting is:");

for (int i = 0; i < n; i++)

printf ("%d", array[i]);

}

Select sort

#include <cstdio>

using namespace Std;

const int MAXN = 1111;

int ARRAY[MAXN];

int main () {

int n,min;

printf ("Please enter the number of elements in the collection:");

scanf ("%d", &n);

printf ("Please enter the value of%d collection elements:", n);

for (int i = 0; i < n; i++)

scanf ("%d", &array[i]);

Make selection sort

for (int i = 0; i < n; i++) {

min = i;

for (int j = i + 1; j < N; j + +) {

if (Array[j] < array[min])

min = j;

}

if (min! = i) {

int temp = Array[i];

Array[i] = Array[min];

Array[min] = temp;

}

}

printf ("The set after sorting is:");

for (int i = 0; i < n; i++)

printf ("%d", array[i]);

printf ("\ n");

}

Set up, insert, merge, delete a specified node in an ordered list

#include <cstdio>

#include <algorithm>

using namespace Std;

/**************************

Shukai Han

[Establishment, insertion,

Merge, delete specified nodes]

**************************/

struct node{//node structure

int value;

Node *next;

};

void Insert (Node *head,int value) {//Inserts an element into an ordered list

Node *p = head, next;

Node *q = head;

while (P! = NULL) {

if (p, value > Value) {

Node *tmp = (node*) malloc (sizeof (Node));

TMP, value = value;

Q-next = tmp;

TMP--next = p;

Return

}

p = p and next;

Q = q-Next;

}

If you need to insert the list at the end

Node *tmp = (node*) malloc (sizeof (Node));

TMP, value = value; TMP, next = NULL;

Q-next = tmp;

}

Node *createlist (int n) {

Node *head = (node*) malloc (sizeof (Node)); Head node Memory request

Head, next = NULL;

Node *q = head;

int V; Node value

printf ("Please enter the value of%d linked list elements:", n);

for (int i = 0; i < n; i++) {

scanf ("%d", &v);

Insert (HEAD,V);

}

return head;

}

void print (Node *head) {//chain-list traversal

Node *now = head, next;

printf ("The element of the list is:");

while (now = NULL) {

printf ("%d", now-value);

now = next;

}

printf ("\ n");

}

node* Combine (Node *head1,node *head2) {//merge linked list, stored in a new link table returned

Node *head = (node*) malloc (sizeof (Node));

Head, next = NULL;

Node *p = Head1, Next, *q = Head2 next;

while (P! = NULL) {

Insert (head,p-value);

p = p and next;

}

while (q! = NULL) {

Insert (head,q-value);

Q = q-Next;

}

return head;

}

void Delete (Node *head,int pos) {//delete a linked table node at a specified location

int now = 1;

Node *p = head;

Node *q = head, next;

while (q! = NULL) {

if (now = = pos) {

P-Next = Q-Next;

Free (q);

Return

}

Q = q-Next;

p = p and next;

now++;

}

printf ("Delete error!\n");

}

int main () {

int n1,n2;

Node *head1,*head2,*head;

printf ("Please enter the number of elements in List 1:");

scanf ("%d", &n1);

Head1 = CreateList (n1);//Create List function

Print (HEAD1);

printf ("Please enter the number of elements in List 1:");

scanf ("%d", &n2);

head2 = CreateList (n2);//Create List function

Print (HEAD2);

Head = Combine (head1,head2); Merging of linked lists

Print (head);

printf ("Please enter the first few elements of the list to delete:");

int k;

scanf ("%d", &k);

Delete (HEAD,K);

Print (head);

}

Operations of ordered sequential tables

#include <cstdio>

using namespace Std;

const int MAXN = 11111;

int list[maxn],l = 0;

int list2[maxn],l2 = 0;

void Insert (int *_list,int &l,int v) {//Inserts an element with a value of V into the length L order table _list

for (int i = 0; I <= L; i++) {

if (i = = L) {//If it is inserted into the last

_list[l++] = v;

Return

}

if (_list[i] > V) {

All elements starting from I move right, freeing up empty space to place V

for (int j = L; j > i; j--)

_LIST[J] = _list[j-1];

_list[i] = v;

l++; Sequential table plus length

Return

}

}

}

void Delete (int *_list,int &l,int POS) {//delete length as L in order table POS element

if (pos <= 0 | | pos > L) {

printf ("error!\n");

Return

}

for (int i = pos-1; i < L-1; i++)//position POS except for elements all move forward one bit, overwriting

_list[i] = _list[i + 1];

l--; Chain list Shortening

printf ("Delete succeeded! \ n ");

Return

}

void Combine (int *list1,int *list2,int &l1,int &l2) {

Implement table Merge, results are stored in table 1, table 2 is automatically deleted

for (int i = 0; i < L2; i++)//is to insert the elements of table 2 sequentially into table 1

Insert (List1,l1,list2[i]);

while (L2! = 0)//delete Table 2

Delete (list2,l2,1);

}

void print (int *_list,int L) {//Traversal order table

for (int i = 0; i < L; i++)

printf ("%d", _list[i]);

printf ("\ n");

}

int main () {

int n,pos;

Enter the first sequence of tables

printf ("Please enter the number of sequential table (1) elements:");

scanf ("%d", &n);

printf ("Please enter%d sequential table elements:", n);

for (int i = 0; i < n; i++) {

int x;

scanf ("%d", &x);

Insert (LIST,L,X);

}

printf ("The elements in the sequential table are:");

Print (list,l);

Enter the second sequential table, as above

printf ("Please enter the number of sequential table (2) elements:");

scanf ("%d", &n);

printf ("Please enter%d sequential table elements:", n);

for (int i = 0; i < n; i++) {

int x;

scanf ("%d", &x);

Insert (LIST2,L2,X);

}

printf ("The elements in the sequential table are:");

Print (LIST2,L2);

Delete operation test, delete the table (1)

printf ("Please output the location you want to delete:");

scanf ("%d", &pos);

Delete (List,l,pos);

Print (list,l);

Combine (LIST,LIST2,L,L2);

Print (list,l);

printf ("Table (2) of length:%d", L2);

return 0;

}

Two-point Search

#include <cstdio>

using namespace Std;

/*************************

In an ordered sequence

Find a number to determine its location

*************************/

int main () {

int array[] = {1,2,3,4,5,6,7,8,9,100,200,300}; Collection

int L = 12; Table length

int n,v;

printf ("Ordered sequence is:");

for (int i = 0; i < L; i++)

printf ("%d", array[i]);

printf ("\ n Please enter the element value to look for:");

scanf ("%d", &v);

int L = 0,r = L-1,ok = 0;

while (L <= R) {

int mid = (L + r)/2;

if (array[mid] = = v) {

printf ("%d element%d", V,mid + 1);

OK = 1; Marked to find the

Break

}

else if (Array[mid] > V) r = Mid;

else L = mid + 1;

}

if (!ok) printf ("Did not find the specified element \ n");

}

Data structure on-machine exam (Kai Shen Edition)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.