1. 程式人生 > 其它 >Vue2手寫原始碼---響應式資料的變化

Vue2手寫原始碼---響應式資料的變化

link.h

#ifndef _LINK_H
#define _LINK_H

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define N 10

typedef int data_type;

//節點
typedef struct node
{
	struct node *per;
	struct node *next;
	data_type data;	
}Node;

//連結串列
typedef struct link
{
	Node *pHead;
	Node *pTail;
	int count;
}Link;

//設定返回值
enum error
{
	ERROR = -1,
	OK
};

//頭尾標誌
enum pos
{
	TAIL = -1,
	HEAD
};

//建立空連結串列
Link *empty_link(void);

//插入節點
int insert_link(Link *pLink,int pos,data_type item);

//刪除節點
int del_link(Link *pLink,int pos,data_type *data);

//輸出連結串列
int show_link(Link *pLink);

//銷燬連結串列
int destruction_link(Link **ppLink);



#endif

link.c

#include "../include/link.h"



//建立空連結串列
Link *empty_link(void)
{
	//建立空連結串列
	Link *pLink = NULL;
	pLink = (Link *)malloc(sizeof(Link));
	if(NULL == pLink)
	{
		perror("malloc error");
	}
	memset(pLink,'\0',sizeof(Link));
	
	//建立空節點
	pLink->pHead = (Node *)malloc(sizeof(Node));
	if(NULL == pLink)
	{
		perror("malloc error");
		return NULL;
	}
	memset(pLink->pHead,'\0',sizeof(Node));
	
	//count賦初值
	pLink->count = 0;
	
	return pLink;
}

//插入節點
int insert_link(Link *pLink,int pos,data_type item)
{
	//建立新節點
	Node *pNew = NULL;
	pNew = (Node *)malloc(sizeof(Node));
	if(NULL == pNew)
	{
		perror("malloc error");
		return ERROR;
	}
	Node *pTmp = NULL;
	pTmp = pLink->pHead;
	
	switch(pos)
	{
		case HEAD:
			pNew->next = pLink->pHead->next;
			pLink->pHead->next = pNew;
			pNew->per = pLink->pHead;
			pNew->data = item; 
			break;
		case TAIL:

			while(pTmp->next != NULL)
			{		
				pTmp = pTmp->next;
			}
			pTmp->next = pNew;
			pNew->per = pTmp;
			pNew->data = item; 
			break;
		default:
			while(--pos)
			{		
				pTmp = pTmp->next;
			}
			pNew->per = pTmp;
			pNew->next = pTmp->next;
			pTmp->next->per = pNew;
			pTmp->next = pNew;
			
			pNew->data = item; 
			break;
	}
	pLink->count++;
	return OK;
}

//刪除節點
int del_link(Link *pLink,int pos,data_type *data)
{
	Node *pTmp = NULL;
	pTmp = pLink->pHead;
	switch(pos)
	{
		case HEAD:
			pTmp = pTmp->next;
			pLink->pHead->next = pTmp->next;
			pTmp->next->per = pLink->pHead;
			*data = pTmp->data;
			free(pTmp);
			pTmp = NULL;	
			break;
		case TAIL:
			while(pTmp->next != NULL)
			{
				pTmp = pTmp->next;
			}
			pTmp->per->next = NULL;
			*data = pTmp->data;
			free(pTmp);
			pTmp = NULL;	
		default:
			while(pos--)
			{
				pTmp = pTmp->next;
			}
			pTmp->per->next = pTmp->next;
			pTmp->next->per = pTmp->per;
			*data = pTmp->data;
			free(pTmp);
			pTmp = NULL;	
			break;
	}
	pLink->count--;
	return OK;
}

//輸出連結串列
int show_link(Link *pLink)
{
	Node *pTmp = NULL;
	pTmp = pLink->pHead;
	while(pTmp->next != NULL)
	{
		pTmp = pTmp->next;
		printf("%d ",pTmp->data);
	}
	printf("\n");
	return OK;
}

//銷燬連結串列
int destruction_link(Link **ppLink)
{
	Node *pTmp = NULL;
	while((*ppLink)->pHead->next != NULL)
	{
		pTmp = (*ppLink)->pHead->next;
		(*ppLink)->pHead->next = pTmp->next;
		free(pTmp);
		pTmp = NULL;
	}
	free((*ppLink)->pHead);
	(*ppLink)->pHead = NULL;
	free(*ppLink);
	*ppLink = NULL;
	return OK;
}

main:

#include "../include/link.h"


int menu(void)
{
	int op = 0;

	printf("1----------insert_link\n");
	printf("2----------del_link\n");
	printf("3----------show_link\n");
	printf("-1----------exit\n");
	scanf("%d",&op);
	return op;
}


int main(void)
{
	Link *p = NULL;
	p = empty_link();
	//printf("OK\n");
	
	int pos = 0;
	int op = 0;
	data_type item = 0;
	int ret = 0;
	
	system("clear"); //在C中呼叫Linux命令
	
	while(1)
	{
		op = menu();
		if(-1 == op)
		break;	
		switch(op)	
		{
			case 1:
				//insert
				printf("請輸入您要插入的位置和資料\n");
				scanf("%d %d",&pos,&item);
				ret = insert_link(p,pos,item);
				if(ret != OK)
				{
					printf("插入失敗\n");
					if(ret == ERROR)
					{
						printf("插入位置有誤\n");
					}
				}
				else
				{
					printf("插入成功\n");
				}
				break;	
			
			case 2:
				//delete
				printf("請輸入您要刪除位置\n");
				scanf("%d",&pos);
				ret = del_link(p,pos,&item);
				if(ret==OK)
				{
					printf("刪除成功,資料為%d\n",item);	
				}			
				break;
			case 3:
				//show	
				show_link(p);
				break;


			default:
				printf("error");
		}	
	}

	destruction_link(&p);
	return 0;	
}