数据结构--静态链表

先上代码:静态链表的相关操作

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define overflow 1 
#define MAXSIZE 11 
typedef struct StaticLinkedNode_{
	int data;
	int next;
}StaticLinkedNode;
typedef struct node_{
	StaticLinkedNode *datas;
	int *isused;
}Node,*node;
void initList(node &l);
void insertElement(node &l,char n,int g);
void insertNode(node &l,int n);
void clearList(node &l);
void desList(node &l);
bool listEmpty(node &l);
int getElement(node &l,int i);
int locateElement(node &l,char e);
int priorElement(node &l,int e);
int nextElement(node &l,int e);
void deleteNode(node &l,int e);
void Testsample();
//建立链表
void initList(node &l){
	l=(node)malloc(sizeof(Node));
    l->datas=(StaticLinkedNode*)malloc(sizeof(StaticLinkedNode)*MAXSIZE);
    l->isused=(int*)malloc(sizeof(int)*MAXSIZE);
    for (int i=0;i<MAXSIZE;i++)
    {
        l->datas[i].data=0;
        l->datas[i].next=-1;
        l->isused[i]=0;
    }
    l->datas[0].data='1';
    printf("链表建立成功\n");
}
//插入元素 
void insertElement(node &l,char n,int g){
     if (g>MAXSIZE || g<=0)
     {
         printf("长度不符合规范,输入的位置无效!!");
        exit(overflow);
     }
     int Currentlocation,i;
    for (i=1;i<MAXSIZE;i++)
    {
        if (l->isused[i]==0)
        {
           l->datas[i].data=n;
           l->isused[i]=1;
           Currentlocation=i;
           break;
        }
    }
    if (i==MAXSIZE)
    {
        printf("空间已满!!!");
        exit(overflow);
    }
     StaticLinkedNode p=l->datas[0];
     for (int j=1;j<MAXSIZE;j++)
     {
        if (g==j)
        {
            int oldlocation=p.next;
            p.next=Currentlocation;
            l->datas[Currentlocation].next=oldlocation;
            printf("插入成功\n");
            return;
        }
         p=l->datas[p.next];
     }
     printf("你输入的位置大于链表的长度,不合法!!");
     exit(overflow);
	}
//尾插法
void insertNode(node &l,int n){
    if(n<0 || n>MAXSIZE){
		printf("输入的结点数不合法\n");
		exit(overflow);
	}
	int Currentlocation,last=0;
    StaticLinkedNode p=l->datas[0];
    while(p.next!=-1)
    {
        last=p.next;
        p=l->datas[p.next];
    }
    for (int i=1;i<=n;i++)
    {
        for (i=1;i<MAXSIZE;i++)
    {
        if (l->isused[i]==0)
        {
           l->isused[i]=1;
           Currentlocation=i;
           break;
        }
    }
    if (i==MAXSIZE)
    {
        printf("空间已满!!!");
        exit(overflow);
    }
        scanf("%d",&l->datas[Currentlocation].data);
        l->datas[Currentlocation].next=-1;
        l->datas[last].next=Currentlocation;
        last=Currentlocation;
    }
    printf("尾插成功(*^_^*)\n");
}
//打印链表
void printList(node &l)
{
	if (l->datas[0].next==-1)
	{
		printf("链表不存在");
		exit(overflow);
	}
	printf("打印链表:");
	StaticLinkedNode p=l->datas[l->datas[0].next];
    while(p.data!=0)
    {
        printf("%d ",p.data);
        if (p.next==-1){
        	break;
		}
        p=l->datas[p.next];
    }
	printf("\n"); 
}
//清空链表 
void clearList(node &l)
{
	StaticLinkedNode p=l->datas[l->datas[0].next];
    StaticLinkedNode q;
    while(p.data!=0)
    {
    	if(p.next==-1){
        p.data=0;
        p.next=-1;
        break; 
        }q=l->datas[p.next];
        p.data=0;
        p.next=-1;
        p=q;
    }
    l->datas[0].next=-1; 
	printf("链表已清空\n"); 
}
//销毁链表
void desList(node &l){
StaticLinkedNode p=l->datas[0];
StaticLinkedNode q;
    while(p.data!=0)
    {
    	if(p.next==-1){
        p.data=0;
        p.next=-1;
        break; 
        }
        q=l->datas[p.next];
        p.data=0;
        p.next=-1;
        p=q;
    }
    l=NULL;
	printf("链表已销毁\n"); 
}
 
//检查链表是否为空
bool listEmpty(node &l)
{
	if (l->datas[0].next!=-1)
	{
		return true;
	}
	else
	{
		return false;
	}
}
//返回链表第i个的值
int getElement(node &l,int i){
    if (i<=0 || i>MAXSIZE){
		printf("i的值不合法");
		exit(overflow);
	}
	StaticLinkedNode p=l->datas[0];
     for (int j=1;j<MAXSIZE;j++)
     {
        if (i==j)
        {
            return l->datas[p.next].data;
        }
         p=l->datas[p.next];
     }
}
//返回与e值相同的元素位置
int locateElement(node &l,int e){
	StaticLinkedNode p=l->datas[0];
     for (int j=1;j<MAXSIZE;j++)
     {
         if (l->datas[p.next].data==e)
         {
             return j;
         }
         p=l->datas[p.next];
     }
    return 0;
}
//返回指定元素的前驱
int priorElement(node &l,int e){
	if (e==l->datas[l->datas[0].next].data){
        printf("该元素为第一个,无前驱");
        exit(overflow);
    }
    StaticLinkedNode p=l->datas[0];
     for (int j=1;j<MAXSIZE;j++)
     {
         if (l->datas[p.next].data==e)
         {
             return p.data;
         }
         p=l->datas[p.next];
     }
     printf("输入的值在链表中不存在!!");
     exit(overflow);
}
//返回指定元素的后继
int nextElement(node &l,int e)
{
    int last=0;
    StaticLinkedNode p=l->datas[0];
    while(p.next!=-1)
    {
        last=p.next;
        p=l->datas[p.next];
    }
    if (e==l->datas[last].data){
        printf("该元素为最后一个,无后继");
        exit(overflow);
    }
	p=l->datas[0];
     for (int j=1;j<MAXSIZE;j++)
     {
         if (l->datas[p.next].data==e)
         {
             return l->datas[l->datas[p.next].next].data;
         }
         p=l->datas[p.next];
     }
     printf("输入的值在链表中不存在!!");
     exit(overflow);
}
//删除指定的结点
void deleteNode(node &l,int e){
	StaticLinkedNode p=l->datas[0];
     for (int j=1;j<MAXSIZE;j++)
     {
         if (l->datas[p.next].data==e)
         {
             StaticLinkedNode q=l->datas[p.next];
             l->datas[p.next]=l->datas[l->datas[p.next].next];
             //free(&q);
             printf("已经成功删除\n");
             return;
         }
         p=l->datas[p.next];
     }
     printf("输入的值在链表中不存在!!");
     exit(overflow);
}
void Testsample()
{
    node l;
	initList(l);
	insertNode(l,5);
	printList(l);
	bool r=listEmpty(l);
	printf("链表是否为空:%d\n",r);
    int e;
	e=locateElement(l, 2);
	printf("该元素的位置:%d\n",e);
    int s;
    s=getElement(l,3);
	printf("3位置的元素:%d\n",s);
	s=priorElement(l,3);
	printf("3元素的前驱:%d\n",s);
	s=nextElement(l,3);
	printf("3元素后继:%d\n",s);
	deleteNode(l,3);
	printf("删除指定元素3后:");
	printList(l);
	clearList(l);
	r=listEmpty(l);
	printf("清空链表后:");
	printf("链表是否为空:%d\n",r);
	desList(l);
}
int main(void)
{
	Testsample();
    return 0;
}

运行结果:

 

 

总结:

这次倒是没犯内存上的问题,但是又把传参的方式搞混啦,以后一定要注意区分&和*传参的区别

另外静态链表写的过程确实有点繁杂 ,写完已经不想干任何事情了。。。包括总结,所以。。。over啦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值