建立一有序的顺序表,并实现下列操作: 1.把元素x插入表中并保持有序; 2.查找值为x的元素,若找到将其删除; 3.输出表中各元素的值。

一.内容:

建立一有序的顺序表,并实现下列操作:
1.把元素x插入表中并保持有序;
2.查找值为x的元素,若找到将其删除;
3.输出表中各元素的值。

二.算法思想:

在主函数中定义顺序表L,并定义插入元素和被删除元素的值;再给顺序表L分配内存空间,通过调用InitList函数,初始化顺序表L,利用冒泡法将键盘输入的值进行排序,再调用printList函数进行输出。从键盘输入要插入的数的值,通过调用InsList函数,将该值插入,再通过调用printList函数进行输出。从键盘输入要查找的数,调用Locate函数进行查找,若元素存在,则输出“元素查询成功”,并输出该元素在顺序表中的位置;若元素不存在,则输出“该元素不存在!”。从键盘上输入要删除的数,调用DelList函数,循环遍历查找所删除元素的位置,若位置不合法则输出“删除位置不合法!”;若所要删除元素的位置合法,则将删除元素位置后移,顺序表长度减一,输出“删除元素成功”,再调用printList函数进行输出。

三.运行结果
在这里插入图片描述
四.代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>   // 包含了动态内存相关的malloc,calloc,free等
#define MAXSIZE 100			/* 线性表最大长度,MAXSIZE为全局变量 */
#define OK 1				/* 宏定义OK表示返回值1,OK为全局变量 */
#define ERROR -1			/* 宏定义ERROR表示返回值1,ERROR为全局变量 */

typedef struct
{
	/* 定义数组名为elem线性表占用空间为gMAXSIZE=100;*/
	int elem[MAXSIZE];
	/* last表示该顺序表最后一个元素位置*/
	int last;
}SeqList;SeqList L;

/* 初始化顺序表 */
int InitList(SeqList* L)
{
	int I;		/* 初始化顺序表循环常量 */
	int n;      /* 初始化顺序表输入的个数 */
	int i,j;	/* 定义冒泡法所需的常量 */
    printf("请输入线性表元素个数:");
	scanf("%d", &n);
	
	printf("请输入有序线性表元素:");
	for (I = 0; I < n; I++)
	/*依次输入整形元素,直到I=n */
	scanf("%d", &(L->elem[I]));
	
	//冒泡法排序
	for(i=0;i<n-1;i++)
	{
	   for(j=0;j<n-1-i;j++)
		if(L->elem[j]>L->elem[j+1])
			{
				int temp=L->elem[j];
				L->elem[j]=L->elem[j+1];
				L->elem[j+1]=temp;
			}
		}
		
		printf("\n");
		 
	L->last += n;    //顺序表的长度 
	printf("初始化成功  \n");
	return (OK);
}



/* 插入顺序表,e为插入的元素 */
int InsList(SeqList* L, int e)
{
	int i;
	/* 判断顺序表是否满 */
	if (L->last == MAXSIZE - 1)
	{
		printf("表已满,无法插入!");
		return(ERROR);
	}
	
    /* 为插入元素移动位置 */
	for (i = L->last; i >= 0 && e < L->elem[i]; i--)
		L->elem[i + 1] = L->elem[i];

	L->elem[i + 1] = e;
	L->last++;
	printf("插入元素成功!  \n");
	return(OK);
}



/* 删除顺序表中元素 ,参数e代表即将删除的元素  */
int  DelList(SeqList* L, int e)
{
	int k;      /* 为查找元素位置做准备的变量 */
	int flag;  /* 该删除元素在顺序表中所在位置 */

	/* 循环遍历查找所删除元素形参e的位置  */
	for (k = 0; k <= L->last; k++)
	
	{
					if (L->elem[k] == e)
		
			{
					flag = k;
	/* 删除元素后移动位置 */ 
	for (k = flag; k <= L->last; k++)
		L->elem[k] = L->elem[k + 1];
	L->last--;
	printf("删除元素成功!\n");}
	else printf("该元素不在顺序表内! \n"); break; 
}
	
	return(OK);
}

	/* 查找元素位置 */
int Locate(SeqList *L, int e)
{
	int i = 0;
	while ((i <= L->last) && (L->elem[i] != e))
		i++;
	if (i <= L->last) 
	{

		printf("元素查询成功,该元素所在表中位置是:\n");
		printf("%d",i+1);     
	}

	else
		printf("该元素不存在! ");
		return(ERROR);
	
}


/* 打印顺序表函数 */
void printList(SeqList* L)
{
    int I;
	printf("目前顺序表中元素为:\n          ");

	for (I = 0; I <= L->last; I++)
	{
		printf("%d ", L->elem[I]);
	}
	printf("\n");
}


int main(void)
{
	SeqList* L;		
	int Ins_num;    /* 插入元素*/
	int Del_num;	/*即将被删除元素的数值*/
	int e;
	L = (SeqList*)malloc(sizeof(SeqList));  //给顺序表L分配内存空间 
	L->last = -1;                           //建立空的顺序表 
	if (!L->elem)
	{
		return ERROR;
	}
	     //建立顺序表 
		InitList(L);
		printList(L);
	     
	    //插入顺序表 
	    printf("输入想要插入该数的数值:\n");
		scanf("%d", &Ins_num);
		InsList(L,Ins_num);
		printList(L);
			
		//查找顺序表中的值 
		printf("输入想要查找该数的数值: \n");
		scanf("%d", &e);
		Locate(L, e);
			
		//删除顺序表中的值 
		printf("\n");
		printf("输入想要删除该数的数值: \n");
		scanf("%d", &Del_num);
		DelList(L, Del_num);
		printList(L);                                                    
}

  • 10
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
好的,我可以帮你完成这个任务。以下是 C 语言建立一个长度为 10 的链表,并对链表进行操作的代码示例: ```c #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *next; }; int main() { struct Node *head = NULL; struct Node *tail = NULL; struct Node *newNode = NULL; // Create the linked list for (int i = 1; i <= 10; i++) { newNode = (struct Node *)malloc(sizeof(struct Node)); if (newNode == NULL) { printf("Memory allocation failed!"); exit(1); } newNode->data = i; newNode->next = NULL; if (head == NULL) { head = newNode; } else { tail->next = newNode; } tail = newNode; } // Print the original linked list printf("Original linked list: "); struct Node *current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); // Insert a new node at the beginning newNode = (struct Node *)malloc(sizeof(struct Node)); if (newNode == NULL) { printf("Memory allocation failed!"); exit(1); } newNode->data = 0; newNode->next = head; head = newNode; printf("Linked list after inserting a node at the beginning: "); current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); // Insert a new node at the end newNode = (struct Node *)malloc(sizeof(struct Node)); if (newNode == NULL) { printf("Memory allocation failed!"); exit(1); } newNode->data = 11; newNode->next = NULL; tail->next = newNode; tail = newNode; printf("Linked list after inserting a node at the end: "); current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); // Delete a node at the beginning struct Node *temp = head; head = head->next; free(temp); printf("Linked list after deleting a node at the beginning: "); current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); // Delete a node at the end struct Node *prev = NULL; current = head; while (current->next != NULL) { prev = current; current = current->next; } tail = prev; prev->next = NULL; free(current); printf("Linked list after deleting a node at the end: "); current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); // Free memory current = head; while (current != NULL) { head = head->next; free(current); current = head; } return 0; } ``` 这个程序首先创建一个长度为 10 的链表,每个节点的分别为 1~10。然后,我们依次对链表进行以下操作: 1. 在链表的开头插入一个值为 0 的节点; 2. 在链表的末尾插入一个值为 11 的节点; 3. 删除链表开头的节点; 4. 删除链表末尾的节点。 每次操作后,程序都会输出链表当前的状态。最后,我们再次遍历链表,逐个释放节点所占用的内存,防止内存泄漏。 希望这个示例代码能够对你有所帮助。如果你还有其他问题,可以继续问我。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

石先森很疯狂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值