⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 liner.cpp

📁 关于数据结构的各章节的c原代码实现
💻 CPP
字号:
// liner.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#define  OK 1
#define  ERROR 0
#define  OVERFLOW -1
#define  list_init_space 100
#define  list_inc_space 10
typedef int Elemtype;
typedef struct {
	  Elemtype *elem;
	  int length;
      int listsize;
}sq_list;
int sq_list_init(sq_list &l)
{
 l.elem=(Elemtype *)malloc(list_init_space*sizeof(Elemtype));
 if(!l.elem)exit(OVERFLOW);
 l.length=0;
 l.listsize=list_inc_space;
 return OK;
}
int locateelem(sq_list l,int e)
{
int i=1;
Elemtype *p=l.elem;
while (i<=l.length&&*p++!=e)
i++;
if (i<=l.length)
return i;
else
return ERROR;
}
int getlength(sq_list l)
{
	return l.length;
}
int sq_listinsert(sq_list &l,int i,Elemtype e)
{
	if (i<1||i>l.length+1)
	{
		printf("i的值不合法\n");
		return 0;
	}
	if (l.length>=l.listsize)
	{
		Elemtype *newspace;
		newspace=(Elemtype *)realloc(l.elem,(l.listsize+list_inc_space)*sizeof(Elemtype));
        if(!newspace)exit(OVERFLOW);
		l.elem=newspace;
		l.listsize+=list_inc_space;
	}
	int *p,*q;
	q=&(l.elem[i-1]);
	for(p=&(l.elem[l.length-1]);p>=q;p--)
	        *(p+1)=*p;
      *q=e;
	  l.length++;
	  return OK;
}
int sq_listdelete(sq_list &l,int i,Elemtype &e)
{
	if (i<1||i>l.length)
	{
		printf("i的值不合法\n");
		return 0;
	}
	int *p,*q;
	p=&(l.elem[i-1]);
	e=*p;
	q=l.elem+l.length-1;
	for(p=&(l.elem[i]);p<q;p++)
		*(p-1)=*p;
	l.length--;
	return OK;
}
int main(int argc, char* argv[])
{   sq_list r;
    int flag;
	flag=sq_list_init(r);
	if(flag==1)printf("成功\n");
	else printf("失败");
	int i=1;
	int elem;
	printf("当前线性表的长度是:%d\n",getlength(r));
	printf("输入要插入的元素:\n");
	scanf("%d",&elem);
	if(sq_listinsert(r,i,elem))printf("success!\n");
/*	for (int m=1;m<4;m++)
	{
		printf("输入要插入的元素:\n");
		scanf("%d",&elem);
		if(sq_listinsert(r,m,elem))printf("success!\n");

	}*/
	printf("当前线性表的长度是:%d\n",getlength(r));
	return 0;
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -