笔试面试常考编程题

//链表反转
//1.将2-n节点依次放到1节点后面,1再放到链表尾
//2.将2-n节点依次放到首节点前面
//3.直接变链表指向 这里采用3

 1 //链表反转
 2 //1.将2-n节点依次放到1节点后面,1再放到链表尾
 3 //2.将2-n节点依次放到首节点前面
 4 //3.直接变链表指向 这里采用3 
 5 #include<stdio.h>
 6 #include<string.h>
 7 #include<algorithm>
 8 using namespace std;
 9 
10 struct node{
11     int value;
12     struct node *next;
13 }; 
14 
15 node* CreateNewNode(int value)
16 {
17     node *p = new node();
18     p->value =  value;
19     p->next = NULL;
20     return p;
21 }
22 
23 void AddNewNode(node *p, int value)
24 {
25     node *q = new node();
26     q->value = value;
27     q->next = NULL;
28     while (p->next != NULL)
29         p = p->next;
30     p->next = q;
31 }
32 
33 node* ReverseNode(node *head)
34 {
35     node *p1, *p2, *p3;
36     if (head == NULL || head->next == NULL) 
37         return head;
38     p1 = head;
39     p2 = p1->next;
40     while (p2)
41     {
42         p3 = p2->next;
43         p2->next = p1;
44         p1 = p2;
45         p2 = p3;
46     }
47     head->next = NULL; //head is tail 
48     return p1;
49 }
50 
51 void PrintNode(node *head)
52 {
53     if (head == NULL || head->next == NULL) 
54         return;
55     printf("%d",head->value);
56     node *p;
57     while (head->next != NULL)
58     {
59         p = head->next;
60         printf("->%d", p->value);
61         head = p;
62     }
63 }
64 
65 int main()
66 {
67     int n, value;
68     scanf("%d", &n);
69     scanf("%d", &value);
70     node *p = CreateNewNode(value);
71     for (int i = 2; i <= n; i ++)
72     {
73         scanf("%d", &value);
74         AddNewNode(p, value);
75     }
76     
77     node *q = ReverseNode(p);
78     PrintNode(q);
79 }
View Code

//二叉树三种遍历的非递归实现
//前序遍历 根左右
//中序遍历 左根右
//后续遍历 左右根

 1 //二叉树三种遍历的非递归实现
 2 //前序遍历 根左右 
 3 //中序遍历 左根右 
 4 //后续遍历 左右根
 5 #include<stdio.h>
 6 #include<string.h>
 7 #include<stack>
 8 #include<algorithm>
 9 using namespace std;
10 struct node{
11     char data;
12     struct node *lchild;
13     struct node *rchild; 
14 };
15 stack<node*>st;
16 
17 void createTree(node *&T)
18 {
19     char ch;
20     scanf("%c", &ch);
21 //    printf("%c",ch);
22     if (ch == ' ') T = NULL;
23     else 
24     {
25         T = (node*)malloc(sizeof(node));
26         T->data = ch;
27         createTree(T->lchild);
28         createTree(T->rchild);
29     }
30 }
31 
32 void preOrder(node *T)
33 {
34     while (!st.empty()) st.pop();
35     node *p;
36     p = T;
37     while (p != NULL || !st.empty())
38     {
39         while (p != NULL)
40         {
41             printf("%c ", p->data);
42             st.push(p);
43             p = p->lchild;
44         }
45         if (!st.empty())
46         {
47             p = st.top();
48             st.pop();
49             p = p->rchild; 
50         }
51     }
52     printf("\n"); 
53 }
54 
55 void midOrder(node *T)
56 {
57     while (!st.empty()) st.pop();
58     node *p;
59     p = T;
60     while (p != NULL || !st.empty())
61     {
62         while (p != NULL)
63         {
64             st.push(p);
65             p = p->lchild;
66         }
67         if (!st.empty())
68         {
69             p = st.top();
70             st.pop();
71             printf("%c ", p->data);//与前序遍历差别在于先遍历输出完左边 
72             p = p->rchild; 
73         }
74     }
75     printf("\n");         
76 }
77 
78 int main()
79 {
80     node *T;
81     createTree(T);
82     preOrder(T);
83     midOrder(T);
84     return 0;
85 } 
86 //sample ABD   CE  F   
View Code

//快排 以左基准数拿出跳跃插空

 1 //快排 以左基准数拿出跳跃插空
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 void msort(int a[], int l, int r)
 8 {
 9     if (l >= r) return;
10     int key = a[l];
11     int low = l, high = r;
12     while (low < high)
13     {
14         while (low < high && a[high] > key) high--;
15         a[low] = a[high];
16         while (low < high && a[low] < key) low++;
17         a[high] = a[low];
18     }
19     a[low] = key;
20     msort(a, l, low - 1);
21     msort(a, low + 1, r);
22 }
23 
24 int a[100005];
25 int main()
26 {
27     int n;
28     scanf("%d", &n);
29     for (int i = 1; i <= n; i++)
30         scanf("%d", &a[i]);
31     msort(a, 1, n);
32     for (int i = 1; i <= n; i++)
33         printf("%d ", a[i]);
34     printf("\n");
35 } 
View Code

 

posted on 2016-09-16 18:46  xiao_xin  阅读(621)  评论(1编辑  收藏  举报

导航