数据结构我好爱:链表作业->静态链表的插入与删除

就以SDD个人的看法来说:

静态链表与我们传统正常的单链表区别不大;

我们可以想作正常的单链表即可,只是电脑帮助你分配了一块空间,然后你快乐地使用&取地址从而进行连接,它的地址是16位制的;

而静态链表就纯靠个人,首先你去malloc一块空间,然后你的地址可以想做数组的下标,这是电脑操作系统为你分配的相对地址,而你的静态链表的任务就是靠自己malloc得到的空间以及相对地址:0~(n-1)来实现连接。

总结:就是你自己去找了一块空间的地址:0~(n-1),不能使用&取址符号,然后要求将单链表联系起来(不想画图,脑补脑补)

1.首先是结构体的选择:

        单个结点node,data为指针域,next为指针(ps:0~(n-1)故用整数型即可)

        used数组就是为了判断地址x(x\epsilon \left ( 1,n-1 \right ))是否有存值 0  or  1 .

typedef struct{
	int data;
	int next;
}*nPtr,node;

typedef struct {
	nPtr node;
	int* used;
}*static_Link,SLINk;

初始化:

        虚拟头结点head;将next == -1 作为末尾结点。

static_Link Initialize() {
	static_Link head = (static_Link)malloc(sizeof(SLINk));
	head->node = (nPtr)malloc(sizeof(node) * space);
	head->used = (int*)malloc(sizeof(int) * space);
	head->node[0].next = -1;
	head->node[0].data = -1;
	head->used[0] = 1;
	for (int i = 1; i < space; i++) {
		head->used[i] = 0;
	}
	return head;
}

打印:

void Print(static_Link head) {
	int p=0;
	while (p!=-1) {
		printf("%d ", head->node[p].data);
		p = head->node[p].next;
	}
}

 插入结点:

        第一步:寻找尾结点

        第二步:待插入结点

        第三步:连接(水到渠成)

void Insert(static_Link head,int data) {
	int p,q=0;
	for (int i = 0; i < space; i++) {
		if (head->node[i].next == -1) {
			q = i;//to find the tail
		}
	}
	for (int i = 1; i < space; i++) {
		if (head->used[i] == 0) {
			p = i;
			head->used[i] = 1;
			break;
		}
	}
	head->node[p].data = data;
	head->node[p].next = -1;
	head->node[q].next = p;
}

 删除结点:

        寻找到删除结点的前一位置即可,将used[i]置为0,不必free

void Delete(static_Link head,int pos) {
	pos += 1;//head and subscript
	static_Link p = head;
	if (head->node[0].next == -1) {
		return;
	}
	for (int i = 0; i < space; i++) {
		if (p->node[i].next == pos) {
			p->node[i].next = p->node[i + 1].next;
			p->used[i+1] = 0;
		}
	}

}

运行截图:

#include<stdio.h>
#include<malloc.h>

#define space 10

typedef struct{
	int data;
	int next;
}*nPtr,node;

typedef struct {
	nPtr node;
	int* used;
}*static_Link,SLINk;

static_Link Initialize() {
	static_Link head = (static_Link)malloc(sizeof(SLINk));
	head->node = (nPtr)malloc(sizeof(node) * space);
	head->used = (int*)malloc(sizeof(int) * space);
	head->node[0].next = -1;
	head->node[0].data = -1;
	head->used[0] = 1;
	for (int i = 1; i < space; i++) {
		head->used[i] = 0;
	}
	return head;
}

void Print(static_Link head) {
	int p=0;
	while (p!=-1) {
		printf("%d ", head->node[p].data);
		p = head->node[p].next;
	}
}

void Insert(static_Link head,int data) {
	int p,q=0;
	for (int i = 0; i < space; i++) {
		if (head->node[i].next == -1) {
			q = i;//to find the tail
		}
	}
	for (int i = 1; i < space; i++) {
		if (head->used[i] == 0) {
			p = i;
			head->used[i] = 1;
			break;
		}
	}
	head->node[p].data = data;
	head->node[p].next = -1;
	head->node[q].next = p;
}

void Delete(static_Link head,int pos) {
	pos += 1;//head and subscript
	static_Link p = head;
	if (head->node[0].next == -1) {
		return;
	}
	for (int i = 0; i < space; i++) {
		if (p->node[i].next == pos) {
			p->node[i].next = p->node[i + 1].next;
			p->used[i+1] = 0;
		}
	}

}

void Test() {
	static_Link head = Initialize();
	Insert(head, 6);
	Insert(head, 1);
	Insert(head, 7);
	Insert(head, 9);
	Insert(head, 6);
	Insert(head, 4);
	Insert(head, 2);
	Insert(head, 3);//6 1 7 9 6 4 2 3
	printf("删除p=7前:");
	Print(head);
	putchar('\n');
	printf("删除p=7后:");
	Delete(head,7);
	Print(head);
}

int main() {
	Test();
	return 0;
}

 老师代码:yyds

#include <stdio.h>
#include <malloc.h>

#define DEFAULT_SIZE 5

typedef struct StaticLinkedNode{
	char data;

	int next;
} *NodePtr;

typedef struct StaticLinkedList{
	NodePtr nodes;
	int* used;
} *ListPtr;

/**
 * Initialize the list with a header.
 * @return The pointer to the header.
 */
ListPtr initLinkedList(){
	// The pointer to the whole list space.
	ListPtr tempPtr = (ListPtr)malloc(sizeof(StaticLinkedList));

	// Allocate total space.
	tempPtr->nodes = (NodePtr)malloc(sizeof(struct StaticLinkedNode) * DEFAULT_SIZE);
	tempPtr->used = (int*)malloc(sizeof(int) * DEFAULT_SIZE);

	// The first node is the header.
	tempPtr->nodes[0].data = '\0';
	tempPtr->nodes[0].next = -1;

	// Only the first node is used.
	tempPtr->used[0] = 1;
	for (int i = 1; i < DEFAULT_SIZE; i ++){
		tempPtr->used[i] = 0;
	}// Of for i

	return tempPtr;
}// Of initLinkedList

/**
 * Print the list.
 * @param paraListPtr The pointer to the list.
 */
void printList(ListPtr paraListPtr){
	int p = 0;
	while (p != -1) {
		printf("%c", paraListPtr->nodes[p].data);
		p = paraListPtr->nodes[p].next;
	}// Of while
	printf("\r\n");
}// Of printList

/**
 * Insert an element to the given position.
 * @param paraListPtr The position of the list.
 * @param paraChar The given char.
 * @param paraPosition The given position.
 */
void insertElement(ListPtr paraListPtr, char paraChar, int paraPosition){
	int p, q, i;

	// Step 1. Search to the position.
	p = 0;
	for (i = 0; i < paraPosition; i ++) {
		p = paraListPtr->nodes[p].next;
		if (p == -1) {
			printf("The position %d is beyond the scope of the list.\r\n", paraPosition);
			return;
		}// Of if
	} // Of for i

	// Step 2. Construct a new node.
	for (i = 1; i < DEFAULT_SIZE; i ++){
		if (paraListPtr->used[i] == 0){
			// This is identical to malloc.
			printf("Space at %d allocated.\r\n", i);
			paraListPtr->used[i] = 1;
			q = i;
			break;
		}// Of if
	}// Of for i
	if (i == DEFAULT_SIZE){
		printf("No space.\r\n");
		return;
	}// Of if

	paraListPtr->nodes[q].data = paraChar;

	// Step 3. Now link.
	printf("linking\r\n");
	paraListPtr->nodes[q].next = paraListPtr->nodes[p].next;
	paraListPtr->nodes[p].next = q;
}// Of insertElement

/**
 * Delete an element from the list.
 * @param paraHeader The header of the list.
 * @param paraChar The given char.
 */
void deleteElement(ListPtr paraListPtr, char paraChar){
	int p, q;
	p = 0;
	while ((paraListPtr->nodes[p].next != -1) && (paraListPtr->nodes[paraListPtr->nodes[p].next].data != paraChar)){
		p = paraListPtr->nodes[p].next;
	}// Of while

	if (paraListPtr->nodes[p].next == -1) {
		printf("Cannot delete %c\r\n", paraChar);
		return;
	}// Of if

	q = paraListPtr->nodes[p].next;
	paraListPtr->nodes[p].next = paraListPtr->nodes[paraListPtr->nodes[p].next].next;
	
	// This statement is identical to free(q)
	paraListPtr->used[q] = 0;
}// Of deleteElement

/**
 * Unit test.
 */
void appendInsertDeleteTest(){
	// Step 1. Initialize an empty list.
	ListPtr tempList = initLinkedList();
	printList(tempList);

	// Step 2. Add some characters.
	insertElement(tempList, 'H', 0);
	insertElement(tempList, 'e', 1);
	insertElement(tempList, 'l', 2);
	insertElement(tempList, 'l', 3);
	insertElement(tempList, 'o', 4);
	printList(tempList);

	// Step 3. Delete some characters (the first occurrence).
	printf("Deleting 'e'.\r\n");
	deleteElement(tempList, 'e');
	printf("Deleting 'a'.\r\n");
	deleteElement(tempList, 'a');
	printf("Deleting 'o'.\r\n");
	deleteElement(tempList, 'o');
	printList(tempList);

	insertElement(tempList, 'x', 1);
	printList(tempList);
}// Of appendInsertDeleteTest

/**
 * The entrance.
 */
void main(){
	appendInsertDeleteTest();
}// Of main

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值