急求!!!!关于数据结构在【C语言】环境中实现:是关于查找和排序的算法 实现如下功能

发布网友 发布时间:2022-04-24 11:18

我来回答

2个回答

热心网友 时间:2023-10-11 00:17

下面的代码能实现你的功能咯。。。都很基础的,这是我以前学C的时候写的,希望对你有帮助
#include <stdio.h>
#include <malloc.h>
#include<stdlib.h>
struct stu
{ struct stu *last;
int a;
struct stu* next;
}*q,*h,*p,*l;

/*void jllb()
{
int i,j;
h=(struct stu*)malloc(sizeof(struct stu));
q=h;
q->last=NULL;
h->a=0;
for(i=1;i<=9;i++)
{
p=(struct stu*)malloc(sizeof(struct stu));
p->a=2*i+3;
p->next=NULL;
q->next=p;
p->last=q;
q=p;
}

}
void cylb()
{
int b;
printf("请输入要插入的数");
scanf("%d",&b);
p=(struct stu*)malloc(sizeof(struct stu));
p->a=b;
for(q=h;(q->next)->a<b;q=q->next);
p->next=q->next;
p->last=q;
q->next=p;
p->next->last=p;
}
void dylb()
{
for(q=h;;q=q->next)
{
printf("%d\n",q->a);
if(q->next==NULL)break;
}
}
void sclb()
{
int m;
printf("请输入需删除的数:");
scanf("%d",&m);
for(p=h;p->a!=m;p=p->next) ;
q=p->last;
p->next->last=q;
q->next=p->next;
free(p);
}
void xglb()
{
int n,m;
printf("选择错误的数:");
scanf("%d",&n);
printf("\n需要改成的数:");
scanf("%d",&m);
for(p=h;p->a!=n;p=p->next);
p->a=m;
}
void _save()
{
FILE *fp;
if((fp=fopen("D:\\data.txt","w"))==NULL)
{
printf("error");
exit(0);
}
p=h;
while(1)
{
fread(p,sizeof(struct stu),1,fp);
p=p->next;
if(p->next==NULL)
break;
}
fclose(fp);
}*/
void chu()
{
FILE *fp;
int i=0;
if((fp=fopen("D:\\data.txt","r"))==NULL)
{
printf("esaa");
exit(0);
}
q=(struct stu *)malloc(sizeof(struct stu ));
while(1)
{

fread(q,sizeof(struct stu),1,fp);
if(i==0)
q=h;
q=(struct stu *)malloc(sizeof(struct stu ));
q=q->next;
i++;
if(q->next==NULL)
break;
}
fclose(fp);
q=h;
printf("%d %d\n\n\n",q->a,q->next->a);
}
main()
{
int m;
// jllb();
// dylb();

while(1)
{
printf("需要选择的功能1sc 2cy 0 tc 3;");
scanf("%d",&m);
switch(m)
{
// case 1:sclb();break;
// case 2:cylb();break;
// case 0:exit(0);break;
// case 3:xglb();break;
// case 4:_save(); break;
case 5:chu();break;
}
// dylb();

}

}

热心网友 时间:2023-10-11 00:17

// ss.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#define MaxLen 20
#include<iostream>
using namespace std;

typedef struct Sqlist
{
int *elem;
int length;
int listsize;
int data[MaxLen];
}Sqlist;

void Initlist_Sq(Sqlist &L)
{
L.elem=new int[MaxLen];
L.length=0;
}

int ListInsert_Sq(Sqlist &L,int i,int x)
{
int j ;
if(i<0||i>L.length)
return 0;
else
{
L.length++;
for(j=L.length-1;j>i;j--)
L.data[j]=L.data[j-1];
L.data[j]=x;
return 1;
}
}

int DelNode(Sqlist &L,int i)
{
int j;
if(i<0 || i>L.length-1)
return 0;
else
{
for(j=i;j<L.length-1;j++)
L.data[j]=L.data[j+1];
L.length--;
return 1;
}
}

int Locate(Sqlist &L,int x)
{
int i=0;
while(i<L.length && L.data[i]!=x)
i++;
if(i==L.length)
return (-1);
else
return (i);

}

void display(Sqlist L)
{
int j;
cout<<"The Sqlist is:";
if(L.length<0)
cout<<"The Sqlist is NUll";
else
{
if(L.length==1)
cout<<L.data[0]<<endl;
else
{
for(j=0;j<L.length-1;j++)
cout <<L.data[j]<<"->";
cout<<L.data[j];
}
cout<<endl;
}
}
int main(int argc, char* argv[])
{
Sqlist L;
int x,y,n,b,i;
Initlist_Sq(L);
cout<<("Please input the number of your Sqlist:\n") ;
cin>>n;
cout<<"Please input the total elements ot your Sqlist:"<<endl;
for(i=0 ; i<n ; i++)
{
cin>> x;
ListInsert_Sq(L,i,x);

}
display(L);
cout<<endl<<"The location of the element you will delete:";
cin>>y;
DelNode(L,y);
cout<<endl<<"Afer changing,your Sqlist is like this:";
display(L);
cout<<"Input the location you will find:"<<endl;
cin>>b;
cout<<endl<<Locate(L,b)<<endl;
return 0;
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com