面試整理鏈表

華三題目鏈表相關知識

有一個數組a[1000]存放0–1000;要求每隔二個數刪掉一個數,到末尾時循環至開頭繼續進行,求最後一個被刪掉的數的原始下標位置。
以7個數爲例
\

-創建結構體,結構體指向下一個
-指定頭節點,並且用另外一個指針保存頭節點位置
-將0-999數值放進鏈表裏面,並且
-將首尾相互連接起來
-利用鏈表操作將第三個覆蓋第二個,並且跳到第三個位置
代碼如下

#include<stdio.h>
#include<stdlib.h>
typedef struct list{
	int info;
	struct list *next;
}Linklist;

int main(){
	
	int cnt,b;

	Linklist *star;
	Linklist *ptr,*clean;

	ptr =(Linklist *)malloc(sizeof(Linklist));
	star = ptr;
	star = clean;
	 
	star -> info=0;
	star -> next = NULL;
	for(int i=1;i<1000;i++){
			star->next=(Linklist *)malloc(sizeof(Linklist));
			star=star->next;
			star->info=i;
			star->next=NULL;
	}
	//形成環狀
	star->next=ptr;

	while(ptr!=ptr->next){
		ptr->next->next=ptr->next->next->next;
		ptr=ptr->next->next;
	}
	printf("%d\n",ptr->info);
	//find mar
} 

華三編程題目

在這裏插入圖片描述


//刪除無序鏈表中的重複節點,僅保留一個,使用2指針定位                           
//考慮到這裏需要用到測試框架,這裏必須使用二級指針
ListNode* DeleteDuplication(ListNode** pHead){
    if(pHead == NULL || *pHead == NULL)
        return NULL; 
                     
    //指向當前正在處理的節點;
    ListNode* p = *pHead;
    //用於遍歷p之後的節點;
    ListNode* q = NULL;
    ListNode* r = NULL;
                     
    while(p != NULL){
        q = p;       
        //若後面有節點與當前節點相同,將其統統刪除
        while(q->next != NULL){
            if(q->next->val == p->val){
                //保存需要刪掉的節點
                r = q->next;
                //需要刪掉的節點的前後節點相接
                q->next = r->next;
                free(r);
                //DestroyListNode(r);
            }        
            else{    
                q = q->next;
            }        
        }            
        p = p->next; 
    }                
                     
    return *pHead;   

鏈表的增刪查改 code

#include<stdio.h>
#include<stdlib.h>
typedef struct list{
	int num;
	int info;
	struct list *next;
}Linklist;
//查找插入

Linklist *find_node(Linklist *head,int num){
	Linklist *mv=NULL;
	for(mv=head;mv!=NULL&&mv->num!=num;mv=mv->next);
	return mv;
}
void inser_node_as_middle(Linklist *head,Linklist *newnode,int num){
	Linklist *fin=find_node(head,num);
	newnode->next=fin->next;
	fin->next=newnode;
}
void insert_node_as_head(Linklist *newnode,Linklist **firaddr){
	newnode->next=*firaddr;
	*firaddr=newnode;
}
void inser_node_as_tail(Linklist *head,Linklist *newnode){

	Linklist *mv=head;
	while(mv->next !=NULL){
		mv=mv->next;
	}
	mv->next=newnode;
	newnode->next=NULL;
}

Linklist *delete_node(Linklist *head,int num){
	
	Linklist *tmp2=head;
	Linklist *tmp,*tmp1;
	//刪除頭節點
	if(num==head->num){
		free(tmp2);
		return head->next;
	}
	else{
		Linklist *fd=find_node(head,num);
        	while(tmp2!=fd){
                	tmp1=tmp2;
                	tmp2=tmp2->next;
       		}
                tmp1->next=tmp2->next;
                free(tmp2);
                return head;   	
	}
}
int main(){
	
	int cnt=2,b;

	Linklist *star;
	//相當於頭節點記住頭的位置
	Linklist *ptr;
	Linklist *newnode;

	ptr =(Linklist *)malloc(sizeof(Linklist));
	star = ptr;
	
	 
	star -> info=0;
	star -> num=0;
	star -> next = NULL;
	for(int i=1;i<4;i++){
			star->next=(Linklist *)malloc(sizeof(Linklist));
			star=star->next;
			star->num=i;
			star->info=i;
			star->next=NULL;
	}

	/*
	newnode= (Linklist *)malloc(sizeof(Linklist));
	newnode->info=100;
	newnode->num=10;
	printf("%d\n",(find_node(ptr,cnt))->info);
	*/
	ptr=delete_node(ptr,cnt);
	star=ptr;
	while(star!=NULL){
		printf("%d\n",star->num);
		star=star->next;
		free(ptr);
		ptr=star;
	}
} 

鏈表遞歸模式

#include<stdio.h>
#include<stdlib.h>
struct list{
        int info;
        struct list *next;
};

void create_list(struct list *node,struct list *temp);
void display(struct list *node);


void main(){
        /*distrubute memory to struct dynamic memory*/

        struct list *start =(struct list *)malloc(sizeof(struct list));
        //create others node
        create_list(start,NULL);
        //begin with the first node display the information in the struct
        display(start);
}
void create_list(struct list *node,struct list *temp){
        //create temporary node
        //struct list *temp;

        /* keep the information to the sturct*/

        scanf("%d",&node->info);

        /*if the last one make next node NULL*/

        if(node->info ==-9999){
                temp->next =NULL;
        }
        else{
                temp = node;
                node->next =(struct list *)malloc(sizeof(struct list));
                create_list(node->next,temp);
        }
}
void display(struct list *node){
	struct list *ptr;
	ptr=node;
        while(ptr!=NULL){
                printf("%d -> ",ptr->info);
                ptr=ptr->next;
		free(node);
		node=ptr;
        }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章