静态链表

  1. 在分散的内存区域只有地址能唯一标识一个位置,但是在连续的内存中地址和下标都可以唯一的标识一个位置。
  2. 将连续的内存区划分为多个节点,节点包括存储数据的数据域和指针域
  3. 用第一个节点记录未使用节点,用最后一个节点记录已使用节点。
    #include<iostream>
    
    #define LINE 22
    typedef int elemtype;
    
    class Node
    {
    public:
    	elemtype mdata;
    	int cursor;
    };
    class SList
    {
    public:
    	SList()
    	{
    		int i = 0;
    		for (i; i < LINE - 2; i++)
    		{
    			node[i].cursor = i + 1;
    		}
    		node[i++].cursor = -1;
    		node[i].cursor = -1;
    	}
    
    	bool IsEmpty()
    	{
    		if (node[LINE - 1].cursor == -1)
    		{
    			return true;
    		}
    		return false;
    	}
    	void InsterHead(elemtype val)//头插
    	{
    		if (IsFull())
    		{
    			return;
    		}
    		int index = node[0].cursor;
    		node[0].cursor = node[index].cursor;
    
    		node[index].mdata = val;
    
    		node[index].cursor = node[LINE - 1].cursor;
    		node[LINE - 1].cursor = index;
    	}
    	void InsertTail(elemtype val)//尾插
    	{
    		if (IsFull())
    		{
    			return;
    		}
    		int index = node[0].cursor;
    		node[0].cursor = node[index].cursor;
    
    		node[index].mdata = val;
    		node[index].cursor = -1;
    
    		int last = LINE - 1;
    		while (node[last].cursor != -1)
    		{
    			last = node[last].cursor;
    		}
    		node[last].cursor = index;
    	}
    	void DeleteKey(elemtype val)//按元素删除某个节点
    	{
    		if (IsEmpty())
    		{
    			return;
    		}
    
    		int index = LINE - 1;
    		int nextindex = node[index].cursor;
    
    		while (nextindex != -1)
    		{
    			if (node[nextindex].mdata == val)
    			{
    				break;
    			}
    			index = nextindex;
    			nextindex = node[nextindex].cursor;
    		}
    
    		if (nextindex == -1)
    		{
    			printf("not found!");
    			return;
    		}
    
    		node[index].cursor = node[nextindex].cursor;
    
    		node[nextindex].cursor = node[0].cursor;
    		node[0].cursor = nextindex;
    	}
    	void DeletePos(int pos)//按位置删除某个节点
    	{
    		if (IsEmpty())
    		{
    			return;
    		}
    
    		if (pos == 1 || pos == LINE - 1)
    		{
    			return;
    		}
    		int index = pos;
    		int nextcursor = node[index].cursor;
    
    		int front = LINE - 1;
    		while (node[front].cursor != -1)
    		{
    			if (node[front].cursor == index)
    			{
    				break;
    			}
    			front = node[front].cursor;
    		}
    
    		node[front].cursor = node[index].cursor;
    
    		node[index].cursor = node[0].cursor;
    		node[0].cursor = index;
    
    	}
    	void Show()
    	{
    		int index = node[LINE - 1].cursor;
    		while (index != -1)
    		{
    			printf("%d ", node[index].mdata);
    			index = node[index].cursor;
    		}
    		printf("\n");
    	}
    
    private:
    	bool IsFull()
    	{
    		if (node[0].cursor == -1)
    		{
    			return true;
    		}
    		return false;
    	}
    	Node node[LINE];
    };
    
    int main()
    {
    	SList sl;
    	int i = 0;
    	for (i; i < 10; i++)
    	{
    		sl.InsterHead(i + 1);
    	}
    	sl.Show();
    	sl.DeletePos(4);
    	sl.Show();
    	sl.InsterHead(30);
    	sl.Show();
    	sl.DeleteKey(30);
    	sl.Show();
    	return 0;
    }

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
静态链表是一种使用数组实现的链表结构,它通过数组中的元素来模拟链表中的节点,并使用一个特殊的指针(游标)来表示节点之间的逻辑关系。静态链表的优点是实现简单,不需要频繁地申请和释放内存空间,但缺点是插入和删除操作比较麻烦,需要手动维护游标指针。 以下是一个简单的Python静态链表的实现示例[^1]: ```python class StaticLinkedList: def __init__(self, size): self.data = [None] * size # 存储数据的数组 self.next = [i + 1 for i in range(size)] # 游标数组,用于维护节点之间的逻辑关系 self.next[-1] = -1 # 最后一个元素的游标为-1,表示链表的末尾 self.head = -1 # 头指针,指向链表的第一个节点 def is_empty(self): return self.head == -1 def is_full(self): return self.next == -1 def insert(self, value): if self.is_full(): print("StaticLinkedList is full") return new_node = self.next # 获取一个空闲节点 self.next = self.next[new_node] # 更新空闲节点的游标 self.data[new_node] = value # 在空闲节点中存储数据 if self.is_empty(): self.head = new_node self.next[new_node] = -1 else: current = self.head while self.next[current] != -1: current = self.next[current] self.next[current] = new_node self.next[new_node] = -1 def delete(self, value): if self.is_empty(): print("StaticLinkedList is empty") return prev = -1 current = self.head while current != -1: if self.data[current] == value: break prev = current current = self.next[current] if current == -1: print("Value not found") return if prev == -1: self.head = self.next[current] else: self.next[prev] = self.next[current] self.next[current] = self.next # 将删除的节点加入空闲节点链表 self.next = current def display(self): if self.is_empty(): print("StaticLinkedList is empty") return current = self.head while current != -1: print(self.data[current], end=" ") current = self.next[current] print() # 创建一个容量为5的静态链表 static_list = StaticLinkedList(5) # 插入数据 static_list.insert(1) static_list.insert(2) static_list.insert(3) # 删除数据 static_list.delete(2) # 显示链表中的数据 static_list.display() ``` 输出结果为: ``` 1 3 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值