Lab1 线性表的基本操作及其应用 sqlist linklist

本文介绍了一个简单的学生信息管理系统的设计与实现,系统采用顺序表和单链表两种数据结构进行学生信息的存储,并实现了基本的操作功能,如创建、插入、删除、查找等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验要求

1、 创建一个学生表(5个学生);

2、 显示该表中所有的元素;

3、 根据姓名查找到第3个学生的信息并显示;

4、 插入一个新的学生并显示全部学生信息;

5、 删除第3个学生的信息并显示全部学生信息;

6、 统计学生表中元素的个数(即学生人数);

7、 退出

实验结果:

能够顺利完成顺序表和单链表的创建、插入、删除等操作。

实验分析:

1、顺序表和单链表在各种操作实现过程中的差别

2、程序调试运行中出现的错误信息原因分析。

参考信息:

Definition of structure student

typedef struct {

    char no[8];   //8位学号

    char name[20]; //姓名

    int  score;     //成绩

}Student;

 

Definition of sequential list:

typedef  struct {

  Student  *elem;     //指向数据元素的基地址

  int  length;       //线性表的当前长度                                                           

 }SqList;

         

Definition of linked list

typedef struct LNode{

     Student   data;       //数据域

     struct LNode  *next;   //指针域

}LNode,*LinkList;  


顺序表如下


#include<stdio.h>
#include<iostream> 
#include<cstdio>
#include<stdlib.h>  
#include<string.h>
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -1 
#define Status int
using namespace std;

typedef struct 
{
	char num[10];
	char name[10];
	int age;
	int score;
}Stu;

typedef struct 
{
	Stu *elem;        //存储空间的基地址 
	int length;       //当前长度 
}SqList;              //顺序的结构类型为Sqlist 

//初始化 
Status InitList(SqList &L)           //构造一个空的顺序表L 
{
	L.elem=new Stu[MAXSIZE];         //为顺序表分配一个大小为MASIZE的数组空间 
	
	if(!L.elem)   
	   exit(OVERFLOW);               //存储空间失败退出 
	   
	L.length=0;                      // 空表长度为0 
	return OK;
}

//取值
Status GetElem(SqList L,Stu &e)
{
	int i;
    if(i<i || i>L.length)  
	    return ERROR;   //判断i值是否合理,若不合理,返回ERROR 
    
    e=L.elem[i-1];     //elem[i-1]存储第i个数据元素 
    
	return OK;
} 

//输入学生信息 
void input(Stu &e)
{   
	 cout<<"\t\t\t请输入姓名:"; 	    cin>>e.name;
	 cout<<"\t\t\t请输入学号:";         cin>>e.num;
	 cout<<"\t\t\t请输入年龄:"; 	    cin>>e.age; 
	 cout<<"\t\t\t请输入成绩:";         cin>>e.score;
	 cout<<endl; 
}

//显示学生信息 
void output(Stu &e)
{  
	printf("\t\t\t|姓名: %-10s  学号: %-10s  年龄: %-3d  成绩: %-3d|\n",e.name,e.num,e.age,e.score);
}

//查找
Status LocatedElem(SqList L,Stu e)
{
	int i;
	for(i=0;i<L.length;i++)      //在L中查找值为e的数据元素,返回其序号 
	  if(!strcmp(e.name, L.elem[i].name))
	      return i+1;            //查找成功,返回序号i+1 
	  return 0;                  //查找失败,返回0 
}

//插入
Status ListInsert(SqList &L,Stu e,int i)
{//在顺序表中第i个位置插入新的元素e,i值合法范围是1<= i <= L.length+1 
	
	if((i<1) || (i>L.length+1))  //i值不合法 
	   return ERROR;
	   
	if(L.length == MAXSIZE)       //存储空间已满 
	   return ERROR;
	 int j;  
	for(j=L.length-1;j>=i-1;j--)
	   L.elem[j+1]=L.elem[j];    //插入位置及之后的元素后移 
	   
	L.elem[i-1]=e;               //将新的元素e放入第i个位置 
	
	++L.length;                  //表长加一 
	
	return OK;    
}
 
//删除
Status ListDelete(SqList &L,int i) 
{//在顺序表中删除第i个元素,i值合法范围是1<= i <= L.length+1
   	if((i<1) || (i>L.length+1))  //i值不合法 
	   return ERROR;
	   
	int j; 
	Stu x;                        
	for(j=i; j<=L.length-1; j++)
	{
		  x=L.elem[i-1];                //临时存储删除的元素 
		  L.elem[j-1] = L.elem[j];      //之后的元素前移           	
	}
	   
	--L.length;                         //表长减一
	 
	return OK;    
	
} 

void Print()
{
	cout<<endl;
	cout<<"\t\t\t*————————————————————*"<<endl;
	cout<<"\t\t\t|---------请选择要执行的功能-------------|"<<endl;
	cout<<"\t\t\t|                                        |"<<endl;
	cout<<"\t\t\t|1.创建链表                              |"<<endl;
	cout<<"\t\t\t|                                        |"<<endl;
	cout<<"\t\t\t|2.输入学生基本信息                      |"<<endl;
	cout<<"\t\t\t|                                        |"<<endl;
    cout<<"\t\t\t|3.显示所有学生信息                      |"<<endl;
    cout<<"\t\t\t|                                        |"<<endl;
	cout<<"\t\t\t|4.查找信息                              |"<<endl;
	cout<<"\t\t\t|                                        |"<<endl;
	cout<<"\t\t\t|5.插入信息                              |"<<endl;
	cout<<"\t\t\t|                                        |"<<endl;
	cout<<"\t\t\t|6.删除信息                              |"<<endl;
	cout<<"\t\t\t|                                        |"<<endl;
	cout<<"\t\t\t|7.显示学生总数                          |"<<endl;
	cout<<"\t\t\t|                                        |"<<endl;
	cout<<"\t\t\t|8.退出系统                              |"<<endl;
	cout<<"\t\t\t|                                        |"<<endl;
	cout<<"\t\t\t*————————————————————*"<<endl<<endl; 
}


int main()
{ 
	Stu e,stu;
	SqList L;
	cout<<"\t\t\t *————————————————————*"<<endl; 
	cout<<"\t\t\t|                                         |"<<endl;
	cout<<"\t\t\t|        欢迎进入学生信息管理系统         |"<<endl;
    cout<<"\t\t\t|                                         |"<<endl;
	cout<<"\t\t\t *————————————————————*"<<endl<<endl; 
	int n = 1;   
	while(n != 0)
	{
	  Print(); 
	  
	  cout<<"\t\t\t";	     cin>>n;
		 	 
	  if(n<1||n>8)
	  {
	  	cout<<"\t\t\t —————————————————"<<endl; 
	    cout<<"\t\t\t|       输入错误,请重新选择!     |"<<endl;	   
	  	cout<<"\t\t\t —————————————————"<<endl<<endl; 
	  } 
	 switch(n)
	 {
		case 1:
			if(InitList(L)) 
			{
			   cout<<"\t\t\t * ——————————————————*"<<endl; 
			   cout<<"\t\t\t|          链表创建成功!               |"<<endl;
			   cout<<"\t\t\t *——————————————————*"<<endl<<endl;
			}
			else 
			{
			   cout<<"\t\t\t —————————————————"<<endl; 
			   cout<<"\t\t\t|          链表创建失败!          |"<<endl;
			   cout<<"\t\t\t —————————————————"<<endl<<endl;
			}
			break;
			
		case 2:
			cout<<"\t\t\t —————————————————"<<endl;	
			cout<<"\t\t\t|      请输入学生人数:             |"<<endl;
			cout<<"\t\t\t —————————————————"<<endl<<endl;
			int n1;
			cout<<"\t\t\t";	   cin>>n1;
		      
		    cout<<"\t\t\t —————————————————"<<endl; 
	        cout<<"\t\t\t|       请输入学生信息:           |"<<endl;	   
	  	    cout<<"\t\t\t —————————————————"<<endl<<endl;
			for(int i=0; i<n1; i++)
			{
				L.length++;
			    input(L.elem[i]);
			    if(i == n1-1)
			    {
			       cout<<"\t\t\t —————————————————"<<endl; 
			       cout<<"\t\t\t|          输入结束!               |"<<endl;
			       cout<<"\t\t\t —————————————————"<<endl<<endl;
				}		
			} 
			break;
			
		case 3:
			 cout<<"\t\t\t*——————————————————————————————*"<<endl;
			 for(int i=0;i<L.length;i++)
			 {
			 	 
			 	output(L.elem[i]);
			 
			 } 
			 cout<<"\t\t\t*——————————————————————————————*"<<endl;
			 break;
			 
		case 4:
		    cout<<"\t\t\t —————————————————"<<endl; 
			cout<<"\t\t\t|      请输入所查找学生姓名:       |"<<endl;
			cout<<"\t\t\t —————————————————"<<endl<<endl;
			cout<<"\t\t\t"; 	  cin>>e.name;
		    int ans;  
		    ans=LocatedElem(L,e);
			  if(ans!=0)
			  {
			        cout<<"\t\t\t*——————————————————————————————*"<<endl;
			  		output(L.elem[ans-1]);    
			        cout<<"\t\t\t*——————————————————————————————*"<<endl;
			  }            
		      else 
		      {
			       cout<<"\t\t\t * ———————————————————*"<<endl; 
			       cout<<"\t\t\t|     查询不到此学生信息,请重新输入!    |"<<endl;
			       cout<<"\t\t\t * ———————————————————*"<<endl<<endl;
			 }	
			break;
			
		case 5:
			cout<<"\t\t\t * —————————————————*"<<endl; 
			cout<<"\t\t\t|         请输入要插入的位置:         |"<<endl;
			cout<<"\t\t\t * —————————————————*"<<endl<<endl;
			
			int I;
			cout<<"\t\t\t";   cin>>I;
			
			      cout<<"\t\t\t * —————————————————*"<<endl; 
			      cout<<"\t\t\t|         请输入插入学生的信息:       |"<<endl;
			      cout<<"\t\t\t * —————————————————*"<<endl<<endl;
				  	
                   cout<<"\t\t\t姓名:";  
                       cin>>stu.name; 				 
                   cout<<"\t\t\t学号:";  
                       cin>>stu.num;               
                   cout<<"\t\t\t年龄:";  
                       cin>>stu.age;                 
        	       cout<<"\t\t\t成绩:";  
                     cin>>stu.score; 
                  
			 
			 if( ! ListInsert(L,stu,I))  
              {
                	cout<<"\t\t\t * —————————————————————————*"<<endl; 
			        cout<<"\t\t\t|           插入位置不合法,请重新输入!               |"<<endl;
			        cout<<"\t\t\t * —————————————————————————*"<<endl<<endl;
			  } 
            else  
             { 
                  	cout<<"\t\t\t * —————————————————————————*"<<endl; 
			        cout<<"\t\t\t|           插入成功!                                 |"<<endl;
			        cout<<"\t\t\t * —————————————————————————*"<<endl<<endl;
                
              } 
                break; 
                    
		case 6:
			cout<<"\t\t\t * —————————————————*"<<endl; 
			cout<<"\t\t\t|      请输入要删除信息的学生位置:    |"<<endl;
		    cout<<"\t\t\t * —————————————————*"<<endl<<endl;
			    
			int d;
			cout<<"\t\t\t";   cin>>d; 
			    if(ListDelete(L,d) == OK)
			    {
			    	 cout<<"\t\t\t —————————————————"<<endl; 
			         cout<<"\t\t\t|          操作成功!               |"<<endl;
			         cout<<"\t\t\t —————————————————"<<endl<<endl;
				} 
				else 
				{
			    	 cout<<"\t\t\t —————————————————"<<endl; 
			         cout<<"\t\t\t|          操作失败!              |"<<endl;
			         cout<<"\t\t\t —————————————————"<<endl<<endl;
				}  
			 	break;
			 	
		case 7:
			cout<<"\t\t\t —————————————————"<<endl; 
			cout<<"\t\t\t|          总学生人数为:"<<L.length <<"          |"<<endl;
			cout<<"\t\t\t —————————————————"<<endl<<endl;
			    break;
			
		case 8:
		   
		    cout<<"\t\t\t —————————————————"<<endl; 
			cout<<"\t\t\t|          谢谢您的使用!           |"<<endl;
			cout<<"\t\t\t —————————————————"<<endl<<endl;
		     exit(0);
	} 
}
	return 0;
}



链表如下
#include<stdio.h>
#include<iostream> 
#include<cstdio>
#include<stdlib.h>  
#include<string.h>
#include<string>
#define OK 1
#define ERROR 0
#define MAXSIZE 100
#define OVERFLOW -1 
#define Status int
int sum=0;
using namespace std;
typedef struct 
{
	char num[10];
	char name[10];
	int age;
	int score;
}Stu;

//****单链表的存储结构 
typedef struct  LNode 
{
	Stu data;                //结点的数据域 
	struct LNode *next;      //结点的指针域 
}LNode,*LinkList;             //LinkList为指向结构体LNode的指针类型 

//初始化 
Status InitList(LinkList &L)           //构造一个空的单链表 
{
	L=new LNode;    //生成新结点作为头结点 ,用头指针L指向头结点 
	
    L->next=NULL;   //头节点的指针域置空 
	return OK;
}

//取值
Status GetElem(LinkList L,Stu &e)
{//在带头结点的单链表中根据序号i获取元素的值,用e返回L中第i个数据元素的值
     int i,j;
     LinkList p;
	 p->next;          //初始化,p指向首元结点,计数器j赋初值为1 
	 j=1;
	 while(p && j<1)     //顺链域向后扫描,直到p为空或p指向第i个元素 
	 {
	 	p=p->next;      //p指向下一个结点 
	 	++j;            //计数器加1 
	 }
    if(!p || j>i)
      return ERROR;     //i值不合法	
	e=p->data;          //取第i个结点数据域 
	return OK;
} 

//输入学生信息 
void input(LinkList &L)
{   
		LinkList p=new LNode;             //疑惑点1   改为LinkList p  不能输入 
		cout<<"\t\t\t请输入姓名:"; 
		    cin>>p->data.name; 
		cout<<"\t\t\t请输入学号:"; 
		    cin>>p->data.num; 
		cout<<"\t\t\t请输入年龄:"; 
		    cin>>p->data.age; 
		cout<<"\t\t\t请输入成绩:"; 
		    cin>>p->data.score;
		cout<<endl; 
		
		p->next = L->next;               //疑惑点2   去除这两句不能输出 
		L->next = p;
		sum++;
}

//显示学生信息 
void output(LinkList &L)
{
	LinkList p;
	p = L->next;
	while(p)
	{
		printf("\t\t\t|姓名: %-10s  学号: %-10s  年龄: %-3d  成绩: %-3d|\n",p->data.name,p->data.num,p->data.age,p->data.score);
		p=p->next;
	}
}

//查找
Status LocateElem(LinkList L,Stu &e)
{//在L中查找值为e的数据元素
    cout<<"\t\t\t";  cin>>e.name;
    LinkList p;
	p=L->next;                   //初始化,指向首元结点 
	int flag = -1;
	while( p )     //顺链域向后扫描,直到p为空或者p所指结点的数据域等于e 
	{
		if(strcmp(p->data.name , e.name) == 0)
		{
		  printf("\t\t\t|姓名: %-10s  学号: %-10s  年龄: %-3d  成绩: %-3d|\n",p->data.name,p->data.num,p->data.age,p->data.score);
            flag = 1;
            break;
		}
		else
	       p = p->next;   
	}
    
	if( !p )
	{
		cout<<"\t\t\t * ———————————————————*"<<endl; 
		cout<<"\t\t\t|     查询不到此学生信息,请重新输入!    |"<<endl;
		cout<<"\t\t\t * ———————————————————*"<<endl<<endl;
	}  
}

//插入
Status ListInsert(LinkList L,Stu e,int i)
{//在链表中第i个位置插入新的元素e
  LinkList p;
  LNode *s;
  int j;
  p=L; j=0;
  while(p && j<(i-1))         
  {
  	  p=p->next;            //查找第i-1个结点,p指向该结点 
   	  ++j;
   } 
  if(!p || j>i-1)         //i>n+1或者i<1 
  {
  	 cout<<"\t\t\t * —————————————————*"<<endl; 
	 cout<<"\t\t\t|           插入位置不合法!           |"<<endl;
	 cout<<"\t\t\t * —————————————————*"<<endl<<endl;
  }
  else
  {
  	 s=new LNode;           //生成新结点*s 
     s->data = e;             //将结点*s的数据域置为e 
     s->next = p->next;       //将结点*s的指针域指向结点ai
     p->next = s;             //将结点*p的指针域指向结点*s 
   
    sum++;             //插入成功总数加一	
    cout<<"\t\t\t * —————————————————*"<<endl; 
	cout<<"\t\t\t|         请输入插入学生的信息:       |"<<endl;
	cout<<"\t\t\t * —————————————————*"<<endl<<endl;
				  				  	
    cout<<"\t\t\t姓名:";   cin>>s->data.name; 				 
    cout<<"\t\t\t学号:";   cin>>s->data.num;               
    cout<<"\t\t\t年龄:";   cin>>s->data.age;                 
    cout<<"\t\t\t成绩:";   cin>>s->data.score; 
  }
   //  return ERROR;
     
  
					   			
  // return OK; 
} 
 
//删除
Status ListDelete(LinkList &L,int i) 
{//在带头结点的单链表L中,删除第i个元素 
    LinkList p,q;
    int j=0;
    p=L; 
	while((p->next) && (j<i-1))
	{
	  	p = p->next;            //查找第i-1个结点,p指向该结点 
  	    ++j;
	} 
	 if(!(p->next) || (j>i-1))         //i>n或者i<1时,删除位置不合理
	   return ERROR;
	 q=p->next;                        //临时保存被删结点以备释放 
	 p->next=q->next;                  //改变删除结点前驱结点的指针域 
	 delete q;                         //释放删除结点的空间 
	 return OK; 
} 

void Print()
{
	cout<<endl;
	cout<<"\t\t\t*————————————————————*"<<endl;
	cout<<"\t\t\t|---------请选择要执行的功能-------------|"<<endl;
	cout<<"\t\t\t|                                        |"<<endl;
	cout<<"\t\t\t|1.创建链表                              |"<<endl;
	cout<<"\t\t\t|                                        |"<<endl;
	cout<<"\t\t\t|2.输入学生基本信息                      |"<<endl;
	cout<<"\t\t\t|                                        |"<<endl;
    cout<<"\t\t\t|3.显示所有学生信息                      |"<<endl;
    cout<<"\t\t\t|                                        |"<<endl;
	cout<<"\t\t\t|4.查找信息                              |"<<endl;
	cout<<"\t\t\t|                                        |"<<endl;
	cout<<"\t\t\t|5.插入信息                              |"<<endl;
	cout<<"\t\t\t|                                        |"<<endl;
	cout<<"\t\t\t|6.删除信息                              |"<<endl;
	cout<<"\t\t\t|                                        |"<<endl;
	cout<<"\t\t\t|7.显示学生总数                          |"<<endl;
	cout<<"\t\t\t|                                        |"<<endl;
	cout<<"\t\t\t|8.退出系统                              |"<<endl;
	cout<<"\t\t\t|                                        |"<<endl;
	cout<<"\t\t\t*————————————————————*"<<endl<<endl; 
}

int main()
{ 
	Stu e,stu;
	LinkList p;
	LinkList L;
	cout<<"\t\t\t *————————————————————*"<<endl; 
	cout<<"\t\t\t|                                         |"<<endl;
	cout<<"\t\t\t|        欢迎进入学生信息管理系统         |"<<endl;
    cout<<"\t\t\t|                                         |"<<endl;
	cout<<"\t\t\t *————————————————————*"<<endl<<endl; 
	int n = 1;   
	while(n != 0)
	{
	  Print(); 
	  
	  cout<<"\t\t\t";
	     cin>>n;
		 	 
	  if(n<1||n>8)
	  {
	  	cout<<"\t\t\t —————————————————"<<endl; 
	    cout<<"\t\t\t|       输入错误,请重新选择!     |"<<endl;	   
	  	cout<<"\t\t\t —————————————————"<<endl<<endl; 
	  } 
	 switch(n)
	 {
		case 1:
			if(InitList(L)) 
			{
			   cout<<"\t\t\t * ——————————————————*"<<endl; 
			   cout<<"\t\t\t|          链表创建成功!               |"<<endl;
			   cout<<"\t\t\t *——————————————————*"<<endl<<endl;
			}
			else 
			{
			   cout<<"\t\t\t —————————————————"<<endl; 
			   cout<<"\t\t\t|          链表创建失败!          |"<<endl;
			   cout<<"\t\t\t —————————————————"<<endl<<endl;
			}
			break;
			
		case 2:
			cout<<"\t\t\t —————————————————"<<endl;	
			cout<<"\t\t\t|      请输入学生人数:             |"<<endl;
			cout<<"\t\t\t —————————————————"<<endl<<endl;
			int n1;
			cout<<"\t\t\t";  cin>>n1;
		      
		    cout<<"\t\t\t —————————————————"<<endl; 
	        cout<<"\t\t\t|       请输入学生信息:           |"<<endl;	   
	  	    cout<<"\t\t\t —————————————————"<<endl<<endl;
			for(int i=0; i<n1; i++)
			{
			    input(L);
			    if(i == n1-1)
			    {
			       cout<<"\t\t\t —————————————————"<<endl; 
			       cout<<"\t\t\t|          输入结束!               |"<<endl;
			       cout<<"\t\t\t —————————————————"<<endl<<endl;
				}		
			} 
			break;
			
		case 3:
			
			 cout<<"\t\t\t*——————————————————————————————*"<<endl;
			 output(L);
			 cout<<"\t\t\t*——————————————————————————————*"<<endl;
			 break;
			 
		case 4:
			
		    cout<<"\t\t\t —————————————————"<<endl; 
			cout<<"\t\t\t|      请输入所查找学生姓名:       |"<<endl;
			cout<<"\t\t\t —————————————————"<<endl<<endl;
			
			LocateElem( L,e);
			break;
			
		case 5:
			cout<<"\t\t\t * —————————————————*"<<endl; 
			cout<<"\t\t\t|         请输入要插入的位置:         |"<<endl;
			cout<<"\t\t\t * —————————————————*"<<endl<<endl;
			
			int I;
			cout<<"\t\t\t";  cin>>I;			 
			 ListInsert(L,e,I); 
                break; 
                    
		case 6:
			cout<<"\t\t\t * —————————————————*"<<endl; 
			cout<<"\t\t\t|      请输入要删除信息的学生位置:    |"<<endl;
		    cout<<"\t\t\t * —————————————————*"<<endl<<endl;
			    
			int d;
			cout<<"\t\t\t";   cin>>d;
			   
			if(ListDelete(L,d) == OK)
			{
				sum--;              //总删除成功数减一 
			    cout<<"\t\t\t —————————————————"<<endl; 
			    cout<<"\t\t\t|          操作成功!               |"<<endl;
			    cout<<"\t\t\t —————————————————"<<endl<<endl;
			} 
			else 
			{
			    cout<<"\t\t\t —————————————————"<<endl; 
			    cout<<"\t\t\t|          操作失败!              |"<<endl;
			    cout<<"\t\t\t —————————————————"<<endl<<endl;
			}  
		     break;
			 	
		case 7:
			cout<<"\t\t\t —————————————————"<<endl; 
			cout<<"\t\t\t|          总学生人数为:"<<sum<<"          |"<<endl;
			cout<<"\t\t\t —————————————————"<<endl<<endl;
			    break;
			
		case 8:
		    
		    cout<<"\t\t\t —————————————————"<<endl; 
			cout<<"\t\t\t|          谢谢您的使用!           |"<<endl;
			cout<<"\t\t\t —————————————————"<<endl<<endl;
		    exit(0);
	} 
}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值