线性表

栈&队列

二叉树

持续更新中,尽请期待!!!

#include<bits/stdc++.h>
using namespace std;
#define ElemType int 
#define Status int

typedef struct LNode{
   
    ElemType data;
    struct LNode *next;
}LNode,*LinkList;

//算法2.8
//获得第i个元素把他赋给e
Status GetElem_L(LinkList L,int i,ElemType &e){
   
    LinkList p = L->next;
    int j = 1;
    while(p&&j<i){
   
        p = p->next;
        j++;
    }
    if(!p||j<i)
        return 0;
    e = p->data;
    return 1;
}

//算法2.9
//在i位置之前插入元素e
Status ListInsert_L(LinkList &L,int i,ElemType e){
   
    LinkList p = L;
	int j = 0;
	while (p && j < i - 1) 
	{
   
		p = p->next;
		j++;
	}
	if (!p || j > i - 1)
        return 0;
    LinkList s = (LinkList)malloc(sizeof(LNode));
	s->data = e;
	s->next = p->next;
	p->next = s;
	return 1;
}

//算法2.10
//删除第i个元素,由e返回其值
Status ListDelete_L(LinkList &L,int i,ElemType &e){
   
    LinkList p = L;
	int j = 0;
	while (p->next && j < i - 1)
	{
   
		p = p->next;
		j++;
	}
	if (!(p->next) || j > i - 1)
        return 0;
    LinkList q = p->next;
	p->next = q->next;
	e = q->data;
	free(q);
	return 1;
}

//算法2.11
//逆位序输入n个元素的值,建立带表头结点的单链线性表L
void CreateList_L(LinkList &L,int n){
   
    L = (LinkList)malloc(sizeof(LNode));
    L->next = NULL;
    ElemType e;
    for (int i = n; i > 0;i--){
   
        LinkList p = (LinkList)malloc(sizeof(LNode));
        scanf("%d", &e);
        p->data = e;
        p->next = L->next;
        L->next = p;
    }
}

//算法2.12
//单链表La,Lb的元素归并到Lc,Lc元素按值非递减排列
void MergeList_L(LinkList &La, LinkList &Lb, LinkList &Lc){
   
    LinkList pa = La->next;
    LinkList pb = Lb->next;
    LinkList pc;
    La = pc = La;
	while (pa && pb)
	{
   
		if (pa->data <= pb->data)
		{
   
			pc->next = pa;
			pc = pa;
            pa = pa->next;
        }
		else
		{
   
			pc->next = pb;
			pc = pb;
			pb = pb->next;
		}
	}
	pc->next = pa ? pa : pb;
	free(Lb);
	La = NULL;
    Lb = NULL;
}