卑微-后知后觉查缺补漏1

	最近开始投简历了,技术美术,第一次笔试,不知道具体会考什么。
	听说有编程题,花了两天复习各路算法,最后编程题考了一道数据结构(单链表),两道图形学。
	前面选择都是专业课的内容(网络考数据链路层,子网掩码),偏简单的八股文。
	但是数据结构能忘的早就还给老师,带着悔恨的心。
	(昨天四点都没睡着,两点前都在罪恶消费,今早还有移动开发的课,一大写的悲)
	
	开始复习
	第一天选择快排,大小堆;程序单链表,链表。
  1. 快排(java,b站随便看的一个)
public class Main{
    static int[] arr={49,38,65,97,76,13,27,49};
    public static void main(String[] args) {
        Quick(arr,0,arr.length-1);
    for (int i=0;i<arr.length-1;i++){
        System.out.println(arr[i]);
    }
    }
    public static void Quick(int[] arr,int left,int right){
        if (arr==null||arr.length==0){
            return;
        }
        if (left>right){
            return;
        }
        int key=arr[left];
        int l=left;
        int r=right;
        while (l!=r){
            while (arr[r]>=key&&l<r){
                r--;
            }
            while (arr[l]<=key&&l<r){
                l++;
            }
            if (l<r){
                int temp=arr[l];
                arr[l]=arr[r];
                arr[r]=temp;
            }
        }
        arr[left]=arr[l];
        arr[l]=key;
        Quick(arr,left,l-1);
        Quick(arr,l+1,right);
    }
}

  1. 堆排序(C,灯哥)
//哈哈哈五个月没敲过C了
#include <stdio.h>
void swap(int arr[], int i, int j) {
	int temp = arr[i];
	arr[i] = arr[j];
	arr[j] = temp;
}
//一侧的一趟排序,n总数,i当前索引
void heapify(int tree[],int n,int i) {
	if (i>=n)
	{//递归出口
		return;
	}
	//两个子节点索引
	int c1 = 2 * i + 1;
	int c2 = 2 * i + 2;
	int max = i;
	//<n子节点不能越界
	if (c1<n&&tree[c1]>tree[max]){
		max = c1;
	}
	if (c2<n&&tree[c2]>tree[max]){
		max = c2;
	}
	//如果最大不是当前节点,那就和子节点换
	if (max!=i)
	{	//子节点的数大往上换
		swap(tree, max, i);
		//递归保证左子树或者右子树是堆
		heapify(tree, n, max);
	}
}
//保证tree是堆
void build_heap(int tree[],int n) {
	int last_node = n - 1;//最后一个点索引
	int parent = (last_node - 1) / 2;
	int i;
	for (i = parent; i >= 0; i--) {
		heapify(tree, n, i);
	}
}
//堆排序
void heap_sort(int tree[],int n) {
	build_heap(tree, n);
	for (int i=n-1;i>=0;i--)
	{
		//n-1最后一个数与根节点交换(大顶堆,顶最大),剪枝
		swap(tree, i, 0);
		heapify(tree, i, 0);
	}
}
int main()
{
	int tree[] = { 8,6,9,10,36,89,8,77,5 };
	int n = sizeof(tree)/sizeof(int);
	heap_sort(tree, n);
	for (int i = 0; i < n; i++)
	{
		printf("%d\n", tree[i]);
	}
	getchar();
	return 0;
}

  1. 单链表( C )
#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <stdlib.h>
struct Node
{
	int data;//4
	struct Node *next;//指针4
};
//有一个头结点,一个尾节点
struct Node *head;
struct Node *create_link(struct Node *head, int l, int n)
{
	head = (struct Node *)malloc(sizeof(struct Node));//8
	head->next = NULL;
	struct Node *tail = head;
	for (int i = 0; i < l - 1; i++)
	{
		struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
		temp->data = i;
		temp->next = NULL;
		tail->next = temp;
		tail = temp;
		if (i == l - n -1)
		{
			printf("倒数第%d个数值为%d\n",n,tail->data);
		}
		
	}
	return head;
};
void print_link(struct Node *head) {
	struct Node *p = head;
	p = p->next;
	while (p != NULL)
	{
		printf("%d\n", p->data);
		p = p->next;
	}
}
//删除链表中=X的数据
void deleteALL(struct Node* head,int x) {
	struct Node *p, *q;
	p = head;
	while (p->next!=NULL)
	{
		q = p->next;
		if (x==q->data)
		{
			p->next = q->next;
		}
		if (q->next==NULL&&x==q->data)
		{
			q = NULL;
			break;
		}
		p = q;
	}
}
//在位置i处插入数据elem
void Insert(struct Node *head, int i, int elem) {
	i = i + 1;//这里+1代表传入的i是索引位置,加不加都行
	struct Node *p, *q;
	p = head;
	int j = 0;
	while (p!=NULL&&j<i-1)
	{//指针p与索引j同步
		j++;
		p = p->next;
	}
	if (p==NULL||j>i-1)
	{
		printf("越界\n");
		return;
	}
	//j=i+1;同时p!=NULL
	q= (struct Node *)malloc(sizeof(struct Node));
	q->data = elem;
	q->next = p->next;
	p->next = q;
}
int main() {
	int l = 0, n = 0;
	scanf("%d %d", &l, &n);
	head = create_link(head, l, n);
	print_link(head);
	printf("删除\n");
	deleteALL(head, 8);
	print_link(head);
	printf("插入\n");
	Insert(head, 8, 8);
	print_link(head);
	getchar();
	return 0;
}
  1. 链表(java,图灵)
    题外话,写题的时候本来打算用java实现的分析分析着就蚌住了
//单链表 节点
public class ListNode {
    int val;
    ListNode next;
    public ListNode(int val) {
        this.val = val;
    }
}
//两个指针
public class LinkedList {
    ListNode head;
    ListNode tail;
    int size;

    public LinkedList() {
        head=null;
        tail=null;
        size=0;
    }
    //插入
    public void insert(int position,int number){
        if (position>number){return;}
        ListNode newNode=new ListNode(number);
        //空表
        if (position==0){
            newNode.next=head;
            head=newNode;
            if (tail==null){
                tail=newNode;
            }
            size++;
        }else if (position==size){
            append(number);
        }else{
            ListNode prev=head;
            for (int i = 0; i <position-1 ; i++) {
                prev=prev.next;
            }
            ListNode next=prev.next;
            newNode.next=next;
            prev.next=newNode;
            size++;
        }
    }
    //在链表尾部插入
    public void append(int number){//number插入的数值
        if (size==0){
            ListNode newNode=new ListNode(number);
            head=newNode;
            tail=head;
            size++;
            return;
        }
        ListNode newNode=new ListNode(number);
        if (tail==null){
            tail=newNode;
        }else {
            tail.next=newNode;
            tail=newNode;
        }
        size++;
    }

    //删除
    public void delete(int number){
        if (head!=null&&head.val==number){
            head=head.next;
            size--;
            if (size==0){//删完没有元素了
                tail=head;
            }
        }else {
            ListNode prev=head;
            ListNode cur=head;
            while (cur!=null&&prev!=null){
                if (cur.val==number){
                    if (cur==tail){
                        tail=prev;
                    }
                    prev.next=cur.next;
                    size--;
                    return;
                }
                prev=cur;
                cur=cur.next;
            }
        }
    }

    //查找
    public int search(int number){
        ListNode cur=head;
        for (int index = 0; cur!=null ; index++) {
            if (cur.val==number){
                return index;//返回索引
            }
            cur=cur.next;
        }
        return -1;
    }

    //更新
        public int updat(int oldValue,int newValue){
        ListNode cur=head;
        for (int index = 0; cur!=null ; index++) {
            if (cur.val==oldValue){
                cur.val=newValue;
                return index;//返回索引
            }
            cur=cur.next;
        }
        return -1;
    }
    public void display(){
        ListNode cur=head;
        while (cur!=null){
            System.out.print(cur.val+",");
            cur=cur.next;
        }
    }
}
public class Test {
    public static void main(String[] args) {
        LinkedList list=new LinkedList();
        list.insert(0,5);
        //list.append(5);
        list.append(6);
        //list.delete(6);
        //list.display();
        System.out.println(list.search(6));
    }
}
	岗位相关[UE面试看起来挺有用](https://www.bilibili.com/video/BV1Ka411C7xd?share_source=copy_web)

完事啦好困好困,下一家冲冲冲,何处是归途~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值