数据结构:链表(Linklist)的定义和它的函数们

数据结构:链表(Linklist)的定义和它的函数们

链表的定义:

  • 对整体的定义(相当于一个大括号,保存一头一尾的指针)
#define MAXSIZE 100
typedef char ElemType;
typedef struct _list
{
    ElemType* head;
    ElemType* tail;
}List;
  • 对结点的定义
typedef struct _node
{
    int index;					//链表索引
    ELemType data;				//节点中的数据
    struct _node* next;			//链接下一个结点
}Node;

链表初始化:(先弄一个结点)

void InitLinklist(List* plist)
{
    plist->head = (Node*)malloc(sizeof(Node));		//链表头指向首结点
    if(!plist->head)	exit(0);					//判断是否成功开辟
    plist->tail = plist->head;						//链表尾部指向头部
    plist->head->next = NULL;						//第一个结点的next指向NULL
}

链表结点的添加:

void add(List* plist)
{
    static int index = 1;						//索引作为静态变量
    Node* p = (Node*)malloc(sizeof(Node));		//开辟一个结点
    p->index = index++;							//记作第index个结点
    p->next = NULL;
    if(!plist->head)  plist->head = p;			//如果没有结点,p就作为头结点
    else{
        Node* pp = plist->head;					//用一个指针去找最后一个结点
        while(pp->next) pp = pp->next;			//直到pp为最后一个结点
        pp->next = p;							//把最后一个结点的next指向p
        plist->tail = p;						//链表尾结点更新为p;
    }
}

链表的删、查、改等功能:

比较懒,哪天兴致来了再写吧orz

BBBBBut!

  • 核心思想不会变:

就是用一个结构体指针去找index或者data(有时候要用另外一个指针记录prev上一个结构体)

其实感觉就是和数组一样:

  • 数组中的删比较麻烦,因为找到之后需要移动一部分的数据,把空位填充;然而链表就不一样了,就和链子一样,中间的断开取下一截,然后把两边连到一起就是了(这可能就是它叫做链表的原因吧,就和链子一样)

  • 查和改都是差不多,用for循环遍历加检测。只是在数组中,我们是用

    for(int i = 0 ; i < n ; i++)

  • 然而在链表中,我们就是用

    Node* ptr;
    for (ptr = list.head ; ptr && ptr->data != key ; ptr = ptr->next );
    
  • 这时候出来若是NULL则是未找到,否则就找到且ptr指向了那个结点

    两者味道其实差不多~

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于Node类实现链表LinkList类的代码,同时实现了栈这一数据结构: ```python class Node: def __init__(self, data): self.data = data self.next = None class LinkList: def __init__(self): self.head = None def isEmpty(self): return self.head == None def length(self): count = 0 cur = self.head while cur: count += 1 cur = cur.next return count def add(self, data): new_node = Node(data) if self.isEmpty(): self.head = new_node else: new_node.next = self.head self.head = new_node def append(self, data): new_node = Node(data) if self.isEmpty(): self.head = new_node else: cur = self.head while cur.next: cur = cur.next cur.next = new_node def insert(self, pos, data): if pos <= 0: self.add(data) elif pos >= self.length(): self.append(data) else: new_node = Node(data) cur = self.head count = 0 while count < (pos - 1): count += 1 cur = cur.next new_node.next = cur.next cur.next = new_node def search(self, data): cur = self.head while cur: if cur.data == data: return True cur = cur.next return False def remove(self, data): if self.isEmpty(): return if self.head.data == data: self.head = self.head.next return cur = self.head while cur.next: if cur.next.data == data: cur.next = cur.next.next return cur = cur.next def travel(self): cur = self.head while cur: print(cur.data, end=' ') cur = cur.next print() class Stack: def __init__(self): self.linklist = LinkList() def push(self, data): self.linklist.add(data) def pop(self): if self.linklist.isEmpty(): return None data = self.linklist.head.data self.linklist.head = self.linklist.head.next return data def top(self): if self.linklist.isEmpty(): return None return self.linklist.head.data def isEmpty(self): return self.linklist.isEmpty() def length(self): return self.linklist.length() ``` 我们可以通过以下方式测试栈的实现: ```python s = Stack() s.push(1) s.push(2) s.push(3) print(s.pop()) # 3 print(s.top()) # 2 s.push(4) print(s.pop()) # 4 print(s.length()) # 2 print(s.isEmpty()) # False ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值