静态链表的实现

SList.h

#pragma once
#include<assert.h>
#include<iostream>
#include<fstream>
#define  MAXSIZE 20
using namespace std;
typedef struct SNode {
	int data;
	int next;//静态链表的next并不是真的指针类型
}SNode, SLInkLIst[MAXSIZE];

void Init_SList(SLInkLIst s);

bool Insert_head(SLInkLIst s, int val);

bool   Insert_tail(SNode * s, int val);//尾插

bool   Is_Empty(SNode* s);//判空

bool is_Full(SNode* s);//判满

int  GetLength(SNode *s);//获取数据结点的个数

int Search(SNode *s, int key);//在s查找第一个key值,找到返回结点地址下标,没有找到返回-1;

void Print_SList(SNode *s);//输出

bool  DelVal(SNode* s, int val);//删除第一个val的值

void  Clear(SNode * s);//清空数据

void  Destroy(SNode  *s);//销毁整个内存nm

SList.cpp

#define _CRT_SECURE_NO_WARNINGS
#include"SList.h"
#include<assert.h>
#include<iostream>
#include<fstream>
using namespace std;

void Init_SList(SLInkLIst s) {
	assert(s != NULL);
	
	s[0].next = 0;
	for (int i = 1; i < MAXSIZE - 1; i++) {
		s[i].next = i + 1;
	}
	s[MAXSIZE - 1].next = 1;
}

bool is_Full(SNode* s) {
	assert(s != NULL);
	return s[MAXSIZE - 1].next = 0;
}

bool Insert_head(SLInkLIst s, int val) {
	assert(s != NULL);
	if (is_Full(s))  return false;
	int pos = s[1].next;
	s[1].next = s[pos].next;
	s[pos].data = val;
	s[pos].next = s[0].next;
	s[0].next = pos;
	return true;;
}

void Print_SList(SNode* s) {//输出
	assert(s != NULL);
	if (s[0].next == 0)	return;

	int i = 0;
	while (s[i].next != 0) {
		cout << s[s[i].next].data << "	";
		i = s[i].next;
	}
	cout << endl;
}


bool   Insert_tail(SNode* s, int val) {//尾插
	assert(s != NULL);
	if (is_Full(s)) return false;
	int pos = s[1].next;
	s[1].next = s[pos].next;
	s[pos].data = val;
	
	int i;
	for ( i = 0; i < MAXSIZE; i++) {
		if (s[i].next == 0) {
			break;
		}
	}
	s[i].next = pos;
	s[pos].next = 0;
	return true;
}

bool   Is_Empty(SNode* s) {//判空
	assert(s != NULL);
	return s[0].next == 0;
}

int  GetLength(SNode* s) {//获取数据结点的个数
	assert(s != NULL);
	if (Is_Empty(s)) return 0;
	int count = 0;
	int i = 0;
	while (s[i].next != 0) {
		count++;
		i = s[i].next;
	}
	return count;

}

//在s查找第一个key值,找到返回结点地址下标,没有找到返回-1;
int Search(SNode* s, int val) {
	assert(s != NULL);
	if (Is_Empty(s)) return -1;
	int i = 0;
	while (s[i].next != 0) {
		if (s[s[i].next].data == val)  return i;
		i = s[i].next;
	}
	return -1;
}

bool  DelVal(SNode* s, int val) {//删除第一个val的值
	assert(s != NULL);
	int i = 0;
	bool tag = false;
	while (s[i].next != 0) {
		if (s[s[i].next].data == val) {
			tag = true;
			break;
		}
		i = s[i].next;
	}
	int j = s[i].next ;
	while (s[s[j].next].next  != 0) {
		s[j].data = s[s[j].next].data;
		j = s[j].next;
	}
	s[j].next = 0;
	s[1].next =s[s[j].next].next;
	return tag;
}

void  Clear(SNode* s) {//清空数据
	assert(s != NULL);
	s[0].next = 0;
	for (int i = 1; i < MAXSIZE-1; i++) {
		s[i].next = i + 1;
	}
	s[MAXSIZE - 1].next  = 1;
}

void  Destroy(SNode* s) {//销毁整个内存nm
	assert(s != NULL);
	free(s);
	s = NULL;
}



main.cpp

#include<iostream>
#include<stdio.h>
#include<malloc.h>
#include"SList.h"
using namespace std;
int main() {
	SLInkLIst s;
	Init_SList(s);
	for (int i = 0; i < 10; i++) {
		Insert_tail(s, i + 1);
	}
	Print_SList(s);
	//int t = Search(s, 5);
	DelVal(s, 5);

	Print_SList(s);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
静态链表是一种基于数组的链表实现方式,它可以在不使用指针的情况下实现链表的基本操作。静态链表实现归并排序的基本思路是将数组分成若干个小的有序数组,然后将这些有序数组两两合并,直到最终得到一个完整的有序数组。 以下是静态链表实现归并排序的步骤: 1. 定义静态链表结构体,包含数据和指向下一个元素的指针。 2. 定义归并排序函数,该函数接收一个静态链表作为参数。 3. 在归并排序函数中,首先判断链表是否为空或只有一个元素,如果是,则直接返回。 4. 如果链表中有多个元素,则将链表分成两个部分,分别递归调用归并排序函数。 5. 在递归调用结束后,将两个有序链表合并成一个有序链表。 6. 合并两个有序链表的方法是,比较两个链表的头元素,将较小的元素插入到新链表中,并将指针指向下一个元素。 7. 最后返回新链表。 以下是静态链表实现归并排序的Python代码示例: ```python class Node: def __init__(self, val, next=None): self.val = val self.next = next def merge_sort(head): if not head or not head.next: return head mid = get_mid(head) right = mid.next mid.next = None left = head left = merge_sort(left) right = merge_sort(right) return merge(left, right) def get_mid(head): if not head: return head slow = head fast = head while fast.next and fast.next.next: slow = slow.next fast = fast.next.next return slow def merge(left, right): dummy = Node(0) cur = dummy while left and right: if left.val < right.val: cur.next = left left = left.next else: cur.next = right right = right.next cur = cur.next if left: cur.next = left if right: cur.next = right return dummy.next ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值