数据结构 - 线性表的存储及其他操作

1、采用线性表动态分配顺序存储结构,编程实现顺序表中数据元素的逆置操作。
2、有一个带头结点的线性链表L,编程实现链表中数据元素的逆置操作。要求不另设新结点。
3、采用线性表动态分配顺序存储结构,编程实现顺序表中数据元素按值非递减排列。
4、有一个带头结点的线性链表L,编程实现链表中数据元素按值非递减排列。要求不另设新结点。

3.1

#include <cstring>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#define ll long long
#define PII pair<int, int>
#define PSI pair<string, int>
#define MSI map<string,int>
#define TLE ios::sync_with_stdio(0),cin.tie(0)
const ll INF = 99999999999999;
const int mod = 1e9 + 7;
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

 
typedef int Status;
typedef int ElemType;
 
typedef struct 
{
	ElemType *elem;
	int length;
	int listsize;
}SqList;
 
Status InitList_Sq(SqList &L)
{
	L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
	if (!L.elem) return OVERFLOW;
	L.length = 0;
	L.listsize = LIST_INIT_SIZE;
	return OK;
}
 
Status ListCreate_Sq(SqList &L, int n)
{
	int i;
	srand(time(0));
	for (i = 0; i < n; i++)
	{
		L.elem[i] = rand() % 90 + 10;
		++L.length;
	}
	if (L.length == 0)
		return ERROR;
	return OK;
}
 
Status ListOutput_Sq(SqList L)
{
	int i;
	if (L.length == 0)
		return ERROR;
	for (i = 0; i < L.length; i++)
		printf("%d ", L.elem[i]);
	printf("\n");
	return OK;
}
 
Status ListConverse_Sq(SqList &L)
{
	ElemType temp;
	int i;
	if (L.length == 0)
		return ERROR;
	for (i = 0; i < L.length/2; i++)
	{
		temp = L.elem[i];
		L.elem[i] = L.elem[L.length - 1 - i];
		L.elem[L.length - 1 - i] = temp;
	}
	return OK;
}
 
int main()
{
	SqList L;
	printf("Initialize the sequential list!");
	InitList_Sq(L);
	if (L.length == 0)
		printf("The sequential list is empty!\n");
	printf("Create the sequential list,");
	ListCreate_Sq(L, 5);
	printf("Output all elements of the sequential list!\n");
	ListOutput_Sq(L);
	ListConverse_Sq(L);
	printf("Output all converse elements of the sequential list\n");
	ListOutput_Sq(L);
}

3.2

#include <cstring>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#define ll long long
#define PII pair<int, int>
#define PSI pair<string, int>
#define MSI map<string,int>
#define TLE ios::sync_with_stdio(0),cin.tie(0)
const ll INF = 99999999999999;
const int mod = 1e9 + 7;
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
 
typedef int Status;
typedef int ElemType;
typedef struct LNode {
	ElemType data;
	struct LNode *next;
}LNode,*LinkList;
 
 
void CreateList_L(LinkList &L, int n)
{
	LinkList p, q;
	int i;
	L = (LinkList)malloc(sizeof(LNode));
	q = L;
	srand(time(0));
	for (i = 1; i <= n; i++)
	{
		p = (LinkList)malloc(sizeof(LNode));
		p->data = rand() % 90 + 10;
		q->next = p;
		q = q->next;
	}
	q->next = NULL;
}
 
Status OutputList_L(LinkList L)
{
	LinkList p = L->next;
	if (p == NULL)
		return ERROR;
	while (p != NULL)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
	return OK;
}
 
Status ListConverse_L(LinkList &L)
{
	LinkList p, q;
	p = L->next;
	L->next=NULL;
	while (p!=NULL)
	{
		q = p;
		p = p->next;
		q->next = L->next;
		L->next=q;
	}
	return OK;
}
 
int main()
{
	LinkList L;
	printf("Create the linked list,");
	CreateList_L(L, 5);
	printf("Output all elements of the linked list!\n");
	OutputList_L(L);
	ListConverse_L(L);
	printf("Output all converse elements of the linked list!\n");
	OutputList_L(L);
}

3.3

#include<stdio.h>
#include<stdlib.h>
#define ADD 10
typedef struct{
	int *elem;
	int length;
	int listsize;
}List; 
int CreatList(List &L,int  n){
	L.elem=(int*)malloc(n*sizeof(int));
	L.length=n;
	L.listsize=n;
	int i;
	for(i=0;i<n;i++)
		scanf("%d",&L.elem[i]);		
}
void sortList(List &L){
	int *p;
	int *q;
	int *end;
	int i=0;
	int temp;
	end=L.elem+L.length-1;
	for(i=0;i<L.length;i++)
		{p=&(L.elem[i]);
		for(q=&(L.elem[i]);q<=end;q++)
			if(*q<*p)
			{temp=*q;
			*q=*p;
			*p=temp;}	
		}
}
void printfList(List &L){
	int i=0;
	for(i=0;i<L.length;i++)
		printf("%d ",L.elem[i]);
	return ;
}
int main(){
	List L;
	printf("请输入10个线性表中的元素:");
	CreatList(L,10);
	sortList(L);
	printf("\n输出排好序的顺序表:");
	printfList(L);
	return 0;
}

3.4

#include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
	int data;
	struct LNode *next;
}LNode,*List;
void CreatList(List &L,int n){
	List p;
	List q;
	int i;
	L=(List)malloc(sizeof(LNode));
	L->next=NULL;
	p=L;
	for(i=0;i<n;i++)
		{q=(List)malloc(sizeof(LNode));
		scanf("%d",&q->data);
		p->next=q;
		p=p->next;
		}
	p->next=NULL;
}
void sortList(List &L,int n){
	List p;
	List q;
	int i,j,k;
	int temp;
	q=p=L->next;
	for(i=0;i<n;i++)
		{
			q=p;
			for(j=i;j<n;j++)
			{if(q->data<p->data)
				{temp=q->data;
				q->data=p->data;
				p->data=temp;
				}
			q=q->next;
			}
		p=p->next;
		}
}
void printfList(List &L){
	List p;
	p=L;
	while(p->next){
		p=p->next;
		printf("%d ",p->data);
	}
}
int main(){
	List L;
	printf("请输入10个链表元素:");
	CreatList(L,10);
	printf("\n输出排序后的链表:");
	sortList(L,10);
	printfList(L);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值