c语言顺序表

  •  1.创建顺序表(产生随机数) 。
  •  2.查找元素的位置。
  •  3.插入元素。
  •  4.删除元素。
  •  5. 输出元素 。
  •  6.1 颜色和坐标函数。
  •  6.2 菜单
  •  6.3蛇
  •  7.完整代码
  •  8.效果图片

开始:库函数,创建结构体。

#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<windows.h>
#include<string.h>
#include<conio.h>

#define MAXSIZE 1000                                   
#define OK 1
#define ERROR 0

typedef int ElemType;
typedef int Status;


typedef struct{
	ElemType data[MAXSIZE];                                     //数据 
	int Length;                                                 //长度 
}SqList;                                                        //结构体变量名 

1.创建顺序表(产生随机数) 。

Status CreateList(SqList *L,int n)                         //创建顺序表 
{
	int i;
	srand(time(0));
	L->Length=0; 
	for(i=0;i<n;i++)
	{
		L->data[i]=rand()%100+1;
		L->Length++;
	}
}

 2.查找元素的位置。

Status LocateList(SqList L,ElemType e)                      //输入元素,查找位置。 
{
	int i;
	for(i=0;i<L.Length;i++)
	        if(L.data[i]==e)
	        return i+1;
	return ERROR;
} 

 3.插入元素。

Status ListInsert(SqList *L,int i,ElemType e)                //插入元素 i是插入的位置   e是新元素。 
{
	int k;
	if(L->Length==MAXSIZE)                                  //数组满,则不能插入 
	         return ERROR;
	
    if(i==L->Length+1)                                      //插入到最后一位。 
	{
		L->data[i-1]=e;
		L->Length++;
		return OK;
	 } 
	
	 if(i<1||i>L->Length+1)                                 //插入到比最后一位还要多一位,例:只有10位元素,你插到12位去,则不行了。 
	        return ERROR;
	 
	 if(i<L->Length)                                       //插入到中间任意一位。 
	 {
	 	for(k=L->Length-1;k>=i-1;k--)
	 	L->data[k+1]=L->data[k];
	 }
	 
	 L->data[i-1]=e;
	 L->Length++;
	 return OK;
}

 4.删除元素。

Status ListDelete(SqList *L,int i,ElemType *e)                       //输入元素位置,删除元素。 
{
         	int k;
	if(L->Length==0)
	     return ERROR;
	
	if(i<1||i>L->Length)
          return ERROR;	
          
     *e=L->data[i-1]; 
     
     if(i<L->Length)
     {
     	for(k=i;k<L->Length;k++)
     	L->data[k-1]=L->data[k];
	 }
	 L->Length--;
	 return OK;
 	  
}

5.输出元素 。

 
Status visit(ElemType c,int i)                              //输出元素 
{
	printf("第%2d元素:L->data[%d]=%d\n",i,i,c);
	return OK;
}

Status ListTraverse(SqList L)                  
{
	int i;
	for(i=0;i<L.Length;i++)
	visit(L.data[i],i+1);
	printf("\n");
	return OK;
}

6.接下来是一些无关操作,与主内容没有多大关系。

6.1 (颜色和坐标)函数。

int color(int c)                                                           //更改文字颜色。
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);            //SetConsoleTextAttribute设置控制台窗口字体 
	return 0;                                                             //其中GetStdHandle是获得输入、输出或错误得屏幕缓冲的句柄 
}    
                                                                 //STD_OUTPUT_HANDLE 标准输出的句柄 
void gotoxy(int x,int y)
{
	COORD pos;             //COORD是Windows API中定义的一种结构,表示一个字符在控制台屏幕上的坐标.
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);      //SetConsoleCursorPosition放置控制台光标位置 
}

6.2 菜单

void  menu()              //菜单 
{
	/*
	printf("\n\n");color(15); 
	printf("\t\t|------------------顺序表---------------|\n");
	printf("\t\t|\t\t                        |\n");color(11);
	printf("\t\t|\t\t 1.创建顺序表 \t        |\n");color(15);
	printf("\t\t|\t\t 2.查找元素  \t        |\n");color(10);
	printf("\t\t|\t\t 3.插入元素  \t        |\n");color(8);
	printf("\t\t|\t\t 4.删除元素  \t        |\n");color(13);
	printf("\t\t|\t\t 0.退出系统  \t        |\n");color(12);
	printf("\t\t请选择(0-4):");
	*/
	int i,j; 
	gotoxy(43,18); 
    color(11);
    printf(" 顺  序   表   操   作");
    color(14);
    for(i=20;i<=26;i++)
    {
    	for(j=27;j<=74;j++)
    	{
    		gotoxy(j,i);
    		if(i==20||i==26)
    		{
    			printf("_");
			}
			else if(j==27||j==74)
			{
				printf("|");
			}
		}
	}
	color(12);
	gotoxy(35,22);
	printf("1.创建顺序表");
	gotoxy(55,22);
	printf("2.查找元素");
	gotoxy(35,24);
	printf("3.插入元素");
	gotoxy(55,24);
    printf("4.删除元素"); 
	gotoxy(35,26);
	printf("0.退出程序"); 
	gotoxy(29,27);
	color(3);
	printf("请选择【1 2 3 4 0】:[ ]\b\b");
}

 6.2菜单。


void  menu()              //菜单 
{
	/*
	printf("\n\n");color(15); 
	printf("\t\t|------------------顺序表---------------|\n");
	printf("\t\t|\t\t                        |\n");color(11);
	printf("\t\t|\t\t 1.创建顺序表 \t        |\n");color(15);
	printf("\t\t|\t\t 2.查找元素  \t        |\n");color(10);
	printf("\t\t|\t\t 3.插入元素  \t        |\n");color(8);
	printf("\t\t|\t\t 4.删除元素  \t        |\n");color(13);
	printf("\t\t|\t\t 0.退出系统  \t        |\n");color(12);
	printf("\t\t请选择(0-4):");
	*/
	int i,j; 
	gotoxy(43,18); 
    color(11);
    printf(" 顺  序   表   操   作");
    color(14);
    for(i=20;i<=26;i++)
    {
    	for(j=27;j<=74;j++)
    	{
    		gotoxy(j,i);
    		if(i==20||i==26)
    		{
    			printf("_");
			}
			else if(j==27||j==74)
			{
				printf("|");
			}
		}
	}
	color(12);
	gotoxy(35,22);
	printf("1.创建顺序表");
	gotoxy(55,22);
	printf("2.查找元素");
	gotoxy(35,24);
	printf("3.插入元素");
	gotoxy(55,24);
    printf("4.删除元素"); 
	gotoxy(35,26);
	printf("0.退出程序"); 
	gotoxy(29,27);
	color(3);
	printf("请选择【1 2 3 4 0】:[ ]\b\b");
}

 6.3蛇。

void flower()           //绘制字符花 
{
       gotoxy(35,1);
       color(6);
       printf("/^\\/^\\");

       gotoxy(34,2);
       printf("|_| o|");

       gotoxy(33,2);
       color(2);
       printf("_");

       gotoxy(25,3);
       color(12);
       printf("\\/");
       
       gotoxy(31,3);
       color(2);
       printf("/");

       gotoxy(37,3);
       color(6);
       printf("\\_/");

       gotoxy(41,3);
       color(10);
       printf("\\");

       gotoxy(26,4);
       color(12);
       printf("\\_____");

       gotoxy(32,4);
       printf("_______/");

       gotoxy(31,4);
       color(2);
       printf("|");

       gotoxy(43,4);
       color(10);  
       printf("\\");

       gotoxy(32,5);
       color(2);
       printf("\\______");

       gotoxy(44,5);
       color(10);
       printf("\\");

       gotoxy(39,6); 
	   printf("|     |                  \\");

       gotoxy(38,7); 
       printf("/     /                  \\");

       gotoxy(37,8);
       printf("/     /                    \\  \\");

       gotoxy(35,9); 
	   printf("/     /                      \\   \\");

       gotoxy(34,10); 
       printf("/     /                        \\   \\");

       gotoxy(33,11); 
       printf("/     /          _----_          \\    \\");

       gotoxy(32,12); 
       printf("/     /       _-~       ~-_         |    |");               

       gotoxy(31,13); 
       printf("(     (      _-~    _--_   ~-_      _/    |");

       gotoxy(32,14);
	   printf("\\     ~-____-~ _-~    ~-_    ~-_-~      /");

       gotoxy(33,15);
	   printf("~-_         _-~       ~-_          _-~");
	   
       gotoxy(35,16);
       printf("~--______-~            ~-______-~");
 } 

                                                                       主函数

int main()
{
    flower();
    SqList L;
	ElemType e;
	Status i,n;
	
    int s;
    int z;

	menu();	
    scanf("%d",&s);
     color(15);
    while(s)
    {
    	switch(s)
    	{
    			case 1:{
    				
				  printf("请输入你想创建多少位数的顺序表:");
                    scanf("%d",&n);
		           CreateList(&L,n);
			          printf("显示出结果:\n");
			          //  ListTraverse(L);
			          break; 
				}
				
				
			case 2:	{
				    color(11);
			        char ans='Y'; 
			        while(ans=='Y'||ans=='y')
			        {
			        	printf("请输入你要查找元素的位置的元素(输出0代表该元素不存在):");
			        	scanf("%d",&e);
			        	i=LocateList(L,e);
			        	if(i)
			        	     printf("\n元素%d位于:%d\n是否继续查找(是:Y/y,任意值退出):",e,i); 
			        	else
			        		printf("查找失败!是否重新输入(是:Y/y,任意值退出):");
						scanf("%s",&ans); 
						printf("\n");
					}		
							
			        break;
			}

			        
			case 3: {
				color(10);
				char ans='Y';
				while(ans=='y'||ans=='Y')
				{
			      	printf("请输入插入元素位置:");
			              scanf("%d",&i); 
					printf("请输入新元素:");
					      scanf("%d",&e);
					z=ListInsert(&L,i,e);
					
					if(z){
       	              printf("显示插入结果:\n"); 
       	                             ListTraverse(L);
       	               printf("是否继续插入(是:Y/y,任意值退出):"); 
			        }	 
			        else	
		              printf("插入失败!是否继续插入(是:Y/y,任意值退出):");
		              
		            scanf("%s",&ans); 
					printf("\n");
			       }
				      break;
				      
			    }
		            
		    case 4: {
		    	     color(8);
		    	     char ans='Y';
		    	    while(ans=='y'||ans=='Y')
					{
						printf("请输入你要删除元素的位置的元素:");
	                             scanf("%d",&i);
	                        z=ListDelete(&L,i,&e);
	                if(z){
	                	printf("删除第%d位置的元素值为:%d\n",i,e); 
	                      	printf("依次输出L的元素:\n");
                              ListTraverse(L);
                        printf("是否继续插入(是:Y/y,任意值退出):"); 
					} 
	                else
			          printf("删除失败!是否继续(是:Y/y,任意值退出):");
			          
			          
			           scanf("%s",&ans); 
					    printf("\n");
			          
			      }
			         
					break;
				   }
				  
			default :
			            break;
			
			 			 
		}
		system("cls");
		 ListTraverse(L);
		 
		menu();
	
		scanf("%d",&s);
		
	}
	
	
	
	
}

                                                                  完整代码

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<windows.h>
#include<string.h>
#include<conio.h>

#define MAXSIZE 1000                                   
#define OK 1
#define ERROR 0

typedef int ElemType;
typedef int Status;


typedef struct{
	ElemType data[MAXSIZE];                                     //数据 
	int Length;                                                 //长度 
}SqList;                                                        //结构体变量名 


Status CreateList(SqList *L,int n)                         //创建顺序表 
{
	int i;
	srand(time(0));
	L->Length=0; 
	for(i=0;i<n;i++)
	{
		L->data[i]=rand()%100+1;
		L->Length++;
	}
}


 
Status visit(ElemType c,int i)                              //输出元素 
{
	printf("第%2d元素:L->data[%d]=%d\n",i,i,c);
	return OK;
}

Status ListTraverse(SqList L)                  
{
	int i;
	for(i=0;i<L.Length;i++)
	visit(L.data[i],i+1);
	printf("\n");
	return OK;
}



Status LocateList(SqList L,ElemType e)                      //输入元素,查找位置。 
{
	int i;
	for(i=0;i<L.Length;i++)
	        if(L.data[i]==e)
	        return i+1;
	return ERROR;
} 



Status ListInsert(SqList *L,int i,ElemType e)                //插入元素 i是插入的位置   e是新元素。 
{
	int k;
	if(L->Length==MAXSIZE)                                  //数组满,则不能插入 
	         return ERROR;
	
    if(i==L->Length+1)                                      //插入到最后一位。 
	{
		L->data[i-1]=e;
		L->Length++;
		return OK;
	 } 
	
	 if(i<1||i>L->Length+1)                                 //插入到比最后一位还要多一位,例:只有10位元素,你插到12位去,则不行了。 
	        return ERROR;
	 
	 if(i<L->Length)                                       //插入到中间任意一位。 
	 {
	 	for(k=L->Length-1;k>=i-1;k--)
	 	L->data[k+1]=L->data[k];
	 }
	 
	 L->data[i-1]=e;
	 L->Length++;
	 return OK;
}


Status ListDelete(SqList *L,int i,ElemType *e)                       //输入元素位置,删除元素。 
{
         	int k;
	if(L->Length==0)
	     return ERROR;
	
	if(i<1||i>L->Length)
          return ERROR;	
          
     *e=L->data[i-1]; 
     
     if(i<L->Length)
     {
     	for(k=i;k<L->Length;k++)
     	L->data[k-1]=L->data[k];
	 }
	 L->Length--;
	 return OK;
 	  
}

                /*添加一些好玩的程序,以本程序主要内容无关 */
                
int color(int c)                                                           //更改文字颜色。
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);            //SetConsoleTextAttribute设置控制台窗口字体 
	return 0;                                                             //其中GetStdHandle是获得输入、输出或错误得屏幕缓冲的句柄 
}    
                                                                 //STD_OUTPUT_HANDLE 标准输出的句柄 
void gotoxy(int x,int y)
{
	COORD pos;             //COORD是Windows API中定义的一种结构,表示一个字符在控制台屏幕上的坐标.
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);      //SetConsoleCursorPosition放置控制台光标位置 
}

void  menu()              //菜单 
{
	/*
	printf("\n\n");color(15); 
	printf("\t\t|------------------顺序表---------------|\n");
	printf("\t\t|\t\t                        |\n");color(11);
	printf("\t\t|\t\t 1.创建顺序表 \t        |\n");color(15);
	printf("\t\t|\t\t 2.查找元素  \t        |\n");color(10);
	printf("\t\t|\t\t 3.插入元素  \t        |\n");color(8);
	printf("\t\t|\t\t 4.删除元素  \t        |\n");color(13);
	printf("\t\t|\t\t 0.退出系统  \t        |\n");color(12);
	printf("\t\t请选择(0-4):");
	*/
	int i,j; 
	gotoxy(43,18); 
    color(11);
    printf(" 顺  序   表   操   作");
    color(14);
    for(i=20;i<=26;i++)
    {
    	for(j=27;j<=74;j++)
    	{
    		gotoxy(j,i);
    		if(i==20||i==26)
    		{
    			printf("_");
			}
			else if(j==27||j==74)
			{
				printf("|");
			}
		}
	}
	color(12);
	gotoxy(35,22);
	printf("1.创建顺序表");
	gotoxy(55,22);
	printf("2.查找元素");
	gotoxy(35,24);
	printf("3.插入元素");
	gotoxy(55,24);
    printf("4.删除元素"); 
	gotoxy(35,26);
	printf("0.退出程序"); 
	gotoxy(29,27);
	color(3);
	printf("请选择【1 2 3 4 0】:[ ]\b\b");
}

void flower()           //绘制字符花 
{
       gotoxy(35,1);
       color(6);
       printf("/^\\/^\\");

       gotoxy(34,2);
       printf("|_| o|");

       gotoxy(33,2);
       color(2);
       printf("_");

       gotoxy(25,3);
       color(12);
       printf("\\/");
       
       gotoxy(31,3);
       color(2);
       printf("/");

       gotoxy(37,3);
       color(6);
       printf("\\_/");

       gotoxy(41,3);
       color(10);
       printf("\\");

       gotoxy(26,4);
       color(12);
       printf("\\_____");

       gotoxy(32,4);
       printf("_______/");

       gotoxy(31,4);
       color(2);
       printf("|");

       gotoxy(43,4);
       color(10);  
       printf("\\");

       gotoxy(32,5);
       color(2);
       printf("\\______");

       gotoxy(44,5);
       color(10);
       printf("\\");

       gotoxy(39,6); 
	   printf("|     |                  \\");

       gotoxy(38,7); 
       printf("/     /                  \\");

       gotoxy(37,8);
       printf("/     /                    \\  \\");

       gotoxy(35,9); 
	   printf("/     /                      \\   \\");

       gotoxy(34,10); 
       printf("/     /                        \\   \\");

       gotoxy(33,11); 
       printf("/     /          _----_          \\    \\");

       gotoxy(32,12); 
       printf("/     /       _-~       ~-_         |    |");               

       gotoxy(31,13); 
       printf("(     (      _-~    _--_   ~-_      _/    |");

       gotoxy(32,14);
	   printf("\\     ~-____-~ _-~    ~-_    ~-_-~      /");

       gotoxy(33,15);
	   printf("~-_         _-~       ~-_          _-~");
	   
       gotoxy(35,16);
       printf("~--______-~            ~-______-~");
 } 


int main()
{
    flower();
    SqList L;
	ElemType e;
	Status i,n;
	
    int s;
    int z;

	menu();	
    scanf("%d",&s);
     color(15);
    while(s)
    {
    	switch(s)
    	{
    			case 1:{
    				
				  printf("请输入你想创建多少位数的顺序表:");
                    scanf("%d",&n);
		           CreateList(&L,n);
			          printf("显示出结果:\n");
			          //  ListTraverse(L);
			          break; 
				}
				
				
			case 2:	{
				    color(11);
			        char ans='Y'; 
			        while(ans=='Y'||ans=='y')
			        {
			        	printf("请输入你要查找元素的位置的元素(输出0代表该元素不存在):");
			        	scanf("%d",&e);
			        	i=LocateList(L,e);
			        	if(i)
			        	     printf("\n元素%d位于:%d\n是否继续查找(是:Y/y,任意值退出):",e,i); 
			        	else
			        		printf("查找失败!是否重新输入(是:Y/y,任意值退出):");
						scanf("%s",&ans); 
						printf("\n");
					}		
							
			        break;
			}

			        
			case 3: {
				color(10);
				char ans='Y';
				while(ans=='y'||ans=='Y')
				{
			      	printf("请输入插入元素位置:");
			              scanf("%d",&i); 
					printf("请输入新元素:");
					      scanf("%d",&e);
					z=ListInsert(&L,i,e);
					
					if(z){
       	              printf("显示插入结果:\n"); 
       	                             ListTraverse(L);
       	               printf("是否继续插入(是:Y/y,任意值退出):"); 
			        }	 
			        else	
		              printf("插入失败!是否继续插入(是:Y/y,任意值退出):");
		              
		            scanf("%s",&ans); 
					printf("\n");
			       }
				      break;
				      
			    }
		            
		    case 4: {
		    	     color(8);
		    	     char ans='Y';
		    	    while(ans=='y'||ans=='Y')
					{
						printf("请输入你要删除元素的位置的元素:");
	                             scanf("%d",&i);
	                        z=ListDelete(&L,i,&e);
	                if(z){
	                	printf("删除第%d位置的元素值为:%d\n",i,e); 
	                      	printf("依次输出L的元素:\n");
                              ListTraverse(L);
                        printf("是否继续插入(是:Y/y,任意值退出):"); 
					} 
	                else
			          printf("删除失败!是否继续(是:Y/y,任意值退出):");
			          
			          
			           scanf("%s",&ans); 
					    printf("\n");
			          
			      }
			         
					break;
				   }
				  
			default :
			            break;
			
			 			 
		}
		system("cls");
		 ListTraverse(L);
		 
		menu();
	
		scanf("%d",&s);
		
	}
}

效果图片:

主要页面
创建10位数的顺序表

2.查找元素

3.插入元素

4.删除元素

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值