c语言顺序表实现
㈠ c语言中如何将顺序表排序并实现二分法查找
voidInsertSort(sqR)
这个函数是按值传递参数的。换句话说,你的顺序表在传递的时候被复制了一遍,然后这个函数收到的是一个副本,然后这个程序也许成功排序了这个副本,但是你原来的顺序表并没有改变。你可以考虑传递这个顺序表的指针。比如这样
voidInsertSort(sq*pR)
{
sqR=*pR;
//以下不变
...
}
调用的时候传递R的地址
InsertSort(&R);
㈡ C语言编程:顺序表实现集合的交与并 LA:3,6,9 LB:1,3,5,7,9,10
顺序表本质上就是底层的数组,要用顺序表来模拟集合,首先要对顺序表进行排序,如果不排序直接求交集,则时间复杂度是O(n^2),而排序的时间复杂度可以做到O(n*logn),排好序之后再求交集或者并集,时间复杂度就变为了O(n)。
顺序表的排序可以直接调用系统函数qsort。
㈢ c语言实现两个顺序表的合并
一个算法给你(假如是升序,并且不重复)
while(表1不结束&&表2不结束){
if(表1结束||表1.当前值>表2.当前值){表2.当前值插入新表;表2.当前值向后移动}
elseif(表2结束||表1.当前值<表2.当前值){表1.当前值插入新表;表1.当前值向后移动}
elseif(表1.当前值=表2.当前值){表1.当前值插入新表;表1.当前值和表2.当前值向后移动}
}
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
structstudent{
intnum;
structstudent*next;
};
voidprint(structstudent*head){
structstudent*p;
p=head;
chars='';
if(head==NULL){
printf("该链表为空");
}
if(head!=NULL){
do{
printf("%c%c%d",s,s,p->num);
p=p->next;
}while(p!=NULL);
printf(" ");
}
}
structstudent*creatb(){
structstudent*head;
structstudent*p1,*p2;
intn=0;
p1=p2=(structstudent*)malloc(sizeof(structstudent));
scanf("%d",&p1->num);
head=NULL;
while(p1->num!=0){
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(structstudent*)malloc(sizeof(structstudent));
scanf("%d",&p1->num);
}
p2->next=NULL;
returnhead;
}
intmain(){
structstudent*head1,*head2,*head3;
head1=NULL;
head2=NULL;
head3=NULL;
printf("请输入单链表La,输入0表示输入结束: ");
head1=creatb();
printf("输入的链表为:");
print(head1);
printf("请输入单链表Lb,输入0表示输入结束: ");
head2=creatb();
printf("输入的链表为:");
print(head2);
structstudent*a,*b,*c,*tmpNode;
a=head1->next;
b=head2;//
head3=c=head1;
while(a!=NULL||b!=NULL){
if(b==NULL||(a!=NULL&&a->num<b->num)){
c->next=a;
c=a;
a=a->next;
}elseif(a==NULL||(b!=NULL&&a->num>b->num)){
c->next=b;
c=b;
b=b->next;
}elseif(a!=NULL&&b!=NULL){
c->next=a;
c=a;
a=a->next;
tmpNode=b;
b=b->next;
free(tmpNode);
}
}
c->next=NULL;
printf("合并后的有序链表为:");
print(head3);
c=head3;
while(c){
tmpNode=c;
c=c->next;
free(tmpNode);
}
return0;
}
㈣ 使用C语言编写程序,实现顺序表的基本运算——插入和删除。
typedef struct
{
int *elem;
int length;
int listsize;
} sqlist;
status Create_sq(Sqlist *L,int n)
{
int i;
L->elem=(int*)malloc(100*sizeof(int));
if(!L->elem) return 0;
for(i=0;i<n;i++)
scanf("%d",&(L->elem[i]));
L->length=n;
L->listsize=100;
return 1;
}
status Listinsert_sq(Sqlist *L,int i,int e)
{
int *q,*p,*newbase;
if(i<1||i>L->length+1) return 0;
if(L->length>=L->listsize)
{
newbase=(int*)realloc(L->elem,(L->listsize+10)*sizeof(int));
if(!newbase) exit(-2);
L->elem=newbase;
L->listsize+=10;
}
q=&(L->elem[i-1]);
for(p=&(L->elem[L->length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L->length;
return 1;
}
int main()
{
Sqlist L1;
int n,a;
int i,e;
printf("\n please input the number of data:\n");
scanf("%d",&n);
if(Create_sq(&L1,n)==1)
{
scanf("%d%d",&i,&e);
a=Listinsert_sq(&L1,i,e);
if(a==1)
printf("insert success\n");
else printf("insert false\n");
printf("the list elements are:\n");
for(i=1;i<=L1.length;i++)
{
printf("%d\t",L1.elem[i-1]);
}
}
return 0;
}
㈤ 求数据结构顺序表c语言实现.....
无函数版本,只需你写算法就OK,算是比较基础的··*顺序表的基本操作*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 3
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct {
int *elem;
int length;
int listsize;
}SqList;Status Initlist_Sq(SqList &L) /*初始化顺序表*/
{
}int Destroylist(SqList &L) /*销毁顺序表*/
{
}int Clearlist_Sq(SqList &L) /*清空顺序表*/
{
}
Status Listempty_Sq(SqList L) /*测试顺序表是否为空*/
{
}Status ListInsert_Sq(SqList &L, int i,ElemType e) /*在第i个位置上插入一个元素*/
{ }int LocateElem_Sq(SqList L,ElemType e) /*返回元素e在顺序表中的位置*/
{
}Status ListDelete_Sq(SqList &L, int i, int &e) /*删除第i个位置上的元素*/
{ }void Print_Sq(SqList L) /*输出顺序表*/
{ int i;
if (Listempty_Sq(L))
printf("\nSequential List's length is %d. It's empty!",L.length);
else
printf("\nSequential List's length is %d. These element are : ",L.length);
for(i=0;i<L.length;i++)
printf(" %d",L.elem[i]);
}
void menu()
{ printf("\n");
printf("\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" 1 ------- Print the Sequential List(输出顺序表).\n");
printf(" 2 ------- Insert a data in the Sequential List(在第i个位置上插入一个元素).\n");
printf(" 3 ------- Delete a data in the Sequential List(删除第i个位置上的元素).\n");
printf(" 4 ------- Get a element's locationt in the SqList(返回元素e在顺序表中的位置).\n");
printf(" 5 ------- Clear the Sequential List(清空顺序表).\n");
printf(" 6 ------- Exit.\n");
printf("* * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("\n");
}void menuselect(SqList L)
{ int k,i,done=1;
ElemType e;
while (done)
{ menu();
printf("Please choose: ");
scanf("%d",&k);
getchar();
switch(k)
{ case 1: Print_Sq(L); break;
case 2:{
printf("\nInput the location and data you want to Insert: ");
scanf("%d,%d",&i,&e);
if (ListInsert_Sq(L,i,e)==ERROR) printf("Insert location is not correct!\n");
break;
} case 3:{ printf("\nInput the location you want to delete data: ");
scanf("%d",&i);
if (ListDelete_Sq(L,i,e)==ERROR)
printf("Delete location is not exit!\n");
else
printf("\nDelete data is %d.",e);
break; }
case 4:{ printf("\nInput the data you want to find: ");
scanf("%d",&e);
if (LocateElem_Sq(L,e)!=FALSE)
printf("\nData %d location in the Sequential List is %d.", e,LocateElem_Sq(L,e));
else printf("\nData %d is not in the Sequential List.\n", e);
break; }
case 5: Clearlist_Sq(L);; break;
case 6: done=0;
}
}
}
void main()
{ ElemType e;
SqList La;
int n,i;
Initlist_Sq(La);
printf("Input a number of the element in the Sequential List (n<=%d):",LIST_INIT_SIZE);
scanf("%d",&n);
printf("Enter these elements:");
for(i=1;i<=n;i++)
{scanf("%d",&e);<br> ListInsert_Sq(La,i,e);}
menuselect(La);
getch();
}
㈥ 用C语言创建一个顺序表并完成插入等操作
/*
GY52122008
请输入插入字符 : -
请输入插入位置 : 7
GY5212-2008
Press any key to continue
*/
#include<malloc.h>
#include<stdio.h>
#include<string.h>
#definemaxsize100
typedefstruct{
char*data;
intlength;
}sqlist;
voidinitlist(sqlist*&L){//初始化顺序表
L=(sqlist*)malloc(sizeof(sqlist));
L->data=(char*)malloc(maxsize);
L->length=0;
}
voidcreatelist(sqlist*&L,chara[],intn){//建立顺序表
inti;
for(i=0;i<n;i++)L->data[i]=a[i];
L->length=n;
}
boollistinsert(sqlist*&L,inti,chare){//插入数据元素
intj;
if(i<1||i>L->length+1)returnfalse;
i--;
for(j=L->length;j>i;j--)L->data[j]=L->data[j-1];
L->data[i]=e;
L->length++;
returntrue;
}
boollistdelete(sqlist*&L,inti,char&e){//删除数据元素
intj;
if(i<1||i>L->length)returnfalse;
i--;
e=L->data[i];
for(j=1;j<L->length-1;j++)L->data[j]=L->data[j+1];
L->length--;
returntrue;
}
intlistlength(sqlist*L){//求线性表长度
return(L->length);
}
voiddestroylist(sqlist*&L){
free(L);
}
intlocateelem(sqlist*L,chare){//按元素查找
inti=0;
while(i<L->length&&L->data[i]!=e)i++;
if(i>=L->length)return0;
elsereturni+1;
}
voiddisplist(sqlist*L){//输出线性表
inti;
for(i=0;i<L->length;i++)printf("%c",L->data[i]);
printf(" ");
}
intmain(){
intpos,len;
charch,data[50]="GY52122008";
sqlist*L;
initlist(L);//先初始化
len=strlen(data);
createlist(L,data,len);//创建表
displist(L);
printf("请输入插入字符:");
scanf("%c",&ch);
printf("请输入插入位置:");
scanf("%d",&pos);
if(listinsert(L,pos,ch))displist(L);
elseprintf("插入操作失败。 ");
free(L->data);
free(L);
return0;
}
㈦ 数据结构 c语言版 顺序表的实现
size 是首先定义为表的长度!
例如:
typedef struct Arr
{
datatype a[100];
int size;
}sequence_list;
sequence_list 代表的意思是 struct Arr
㈧ C语言编写一个程序实现两个有序(从小到大)顺序表合并成为一个顺序表,合并后的结果放在第一个顺序表中。
你提到的“奇怪错误”是由于你的exit宏和函数重名了,所以预处理器会把程序中的exit用-1代替,所以出错。
另外,你的合并函数也是错误的,无法达到要求,整个程序修改如下:
//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h> //注意这里,exit()函数要用到这个文件,这个文件已经包含了malloc()等函数的声明
#define OK 1
#define LIST_INIT_SIZE 100
#define OVERFLOW 0
#define EXIT -2 //************注意这里
typedef int ElemType;
typedef int Status;
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) exit(OVERFLOW);
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}
void Create_Sq(SqList *L)
{ int i,n;
printf("创建一个有序表:\n");
printf("输入有序表中元素的个数:");
scanf("%d",&n);
L->length=n;
for(i=0;i<n;i++){
printf("输入第%d个元素的值:",i+1);
scanf("%d",&L->elem[i]);
printf("\n");
}
}
void Disp_Sq(SqList *L) //************注意这里
{ int i,n;
n=L->length;
for(i=0;i<n;i++)
printf("%5d",L->elem[i]);
printf("\n");
}
void Combine(SqList *la,SqList *lb) //************注意这里,此函数被重新编写
{
ElemType *pa=la->elem,*pb=lb->elem ;
int i,j,k,len_a=la->length,len_b=lb->length;
if ((la->elem=malloc((len_a+len_b)*sizeof(ElemType)))==NULL) exit(OVERFLOW);
for (k=i=j=0; i<len_a&&j<len_b; ) {
if (pa[i]<pb[j]) la->elem[k++]=pa[i++];
else la->elem[k++]=pb[j++];
}
while (i<len_a)
la->elem[k++]=pa[i++];
while (j<len_b)
la->elem[k++]=pb[j++];
la->length=k;
la->listsize=k;
free(pa);
}
void main()
{SqList L1,L2;
InitList_Sq(&L1);
InitList_Sq(&L2);
Create_Sq(&L1);
Disp_Sq(&L1);
printf("\n");
Create_Sq(&L2);
Disp_Sq(&L2); //************注意这里
printf("\n");
Combine(&L1,&L2);
Disp_Sq(&L1); //************注意这里
}
//---------------------------------------------------------------------------
㈨ 一个C语言的基础问题,C语言实现顺序表。
你这是C++的代码,用C编译器编译不过的。
如果是vc6里面,把你源程序的后缀改成.cpp再试试应该就可以了。
㈩ c语言实现顺序表
--顺序表.h
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define TRUE 1
#define FLASE 0
typedef int Elemtype;
typedef int Status;
/*接口定义
Status InitList_Sq(SqList &L,int size,int inc);
void CreateList_Sq(SqList &L);
void print_Sq(SqList &L);
int Search_Sq(SqList L, Elemtype e);
Status DestroyList_Sq(SqList &L);
Status ClearList_Sq(SqList &L);
Status ListEmpty_Sq(SqList L);
int ListLength_Sq(SqList L);
Status GetElem_Sq(SqList L, int i, Elemtype &e);
Status PutElem_Sq(SqList &L, int i, Elemtype e);
Status Append_Sq(SqList &L, Elemtype e);
Status DeleteLast_Sq(SqList &L, Elemtype &e);
*/
--------------------
#include "顺序表.h"
//定义顺序表类型
typedef struct {
Elemtype *elem;
int length;
int listsize;
int increment;
}SqList;
//初始化顺序表
Status InitList_Sq(SqList &L,int size,int inc) {
L.elem = (Elemtype *)malloc(size * sizeof(Elemtype));
L.length = 0;
L.listsize = size;
L.increment = inc;
return TRUE;
}
//创建顺序表
Status CreateList_Sq(SqList &L) {
int i;
printf("请输入你要创建的顺序表元素个数:\n");
scanf_s("%d", &L.length);
if (L.length >= L.listsize) {
L.elem = (Elemtype *)realloc(L.elem, (L.listsize + L.increment) * sizeof(Elemtype));
}
if (!L.elem) {
return FLASE;
}
printf("请输入你要创建的顺序表:\n");
for (i = 0; i<L.length; i++) {
scanf_s("%d", &L.elem[i]);
}
}
//遍历顺序表
void print_Sq(SqList &L) {
int i;
for (i = 0; i<L.length; i++) {
printf("%4d", L.elem[i]);
}
}
//查找元素的位置
int Search_Sq(SqList L, Elemtype e) {
int i = 0;
while (L.elem[i] != e&&i<L.length) {
i++;
}
if (i>L.length)
return -1;
else
return i + 1;//因为C语言是从下标为0开始的,当i=0时表示第一个元素
}
//销毁顺序表
Status DestroyList_Sq(SqList &L) {
if (L.elem == NULL)
return -1;
else
free(L.elem);
printf("\n销毁成功\n");
return TRUE;
}
//清空顺序表
Status ClearList_Sq(SqList &L) {
if (L.elem == NULL)
exit(0);
int i;
Elemtype *p_elem = L.elem;
for (i = 0; i<L.length; i++) {
*L.elem = NULL;
L.elem++;
}
L.elem = p_elem;
}
//判断顺序表是否为空
Status ListEmpty_Sq(SqList L) {
int i;
Elemtype* p_elem = L.elem;
for (i = 0; i<L.length; i++) {
if (*L.elem != 0) {
L.elem = p_elem;
return FLASE;
}
L.elem++;
}
return TRUE;
}
//求顺序表的长度
int ListLength_Sq(SqList L) {
return L.length;
}
//用e返回顺序表L中第i个元素的值
Status GetElem_Sq(SqList L, int i, Elemtype &e) {
int j;
Elemtype* p_elem = L.elem;
if (i<1 || i>L.length)
return FLASE;
for (j = 1; j <= i; j++)
L.elem++;
e = *L.elem;
L.elem = p_elem;
return TRUE;
}
//将顺序表L中第i个元素赋值为e
Status PutElem_Sq(SqList &L, int i, Elemtype e) {
L.elem[i - 1] = e;
return TRUE;
}
//在顺序表L表尾添加元素e
Status Append_Sq(SqList &L, Elemtype e) {
L.elem[L.length] = e;
L.length++;
L.listsize += L.increment;
return TRUE;
}
//删除顺序表L表尾元素
Status DeleteLast_Sq(SqList &L, Elemtype &e) {
e = L.elem[L.length - 1];
L.length--;
return TRUE;
}
********************************************主函数.c*************************************************
#include <stdio.h>
#include <stdlib.h>
#include "顺序表.h"
#include "源代码.h"
//--------------------主函数入口--------------------
int main(){
SqList L;
int size, inc;
int e;
int a;
int length;
int i;
int temp;
int j=10;
int ee;
printf("\n--------------------顺序表初始化------------------\n");
printf("请输入顺序表的长度size以及扩容量:\n");
scanf_s("%d %d", &size, &inc);
InitList_Sq(L, size, inc);
CreateList_Sq(L);
printf("\n--------------------判断是否为空------------------\n");
if(ListEmpty_Sq(L)){
printf("该顺序表为空\n");
}
else
printf("该顺序表不为空\n");
printf("\n--------------------遍历顺序表--------------------\n");
printf("此时顺序表为:\n");
print_Sq(L);
printf("\n--------------------查找元素----------------------\n");
printf("\n请输入要查找的元素:\n");
scanf_s("%d",&e);
a = Search_Sq(L, e);
printf("%d为第%d位:\n",e,a);
printf("\n--------------------输出长度----------------------\n");
length = ListLength_Sq(L);
printf("顺序表的长度为%d\n",length);
printf("\n----------将顺序表L中第i个元素赋值为temp----------\n");
printf("请输入第i个元素的i值和temp值:\n");
scanf_s("%d %d",&i,&temp);
PutElem_Sq(L, i, temp);
printf("\n此时顺序表为:\n");
print_Sq(L);
printf("\n---------------在顺序表表尾添加元素---------------\n");
Append_Sq(L, j);
printf("\n此时顺序表为:\n");
print_Sq(L);
printf("\n---------------在顺序表表尾删除元素---------------\n");
DeleteLast_Sq(L, ee);
printf("\n被删除的元素为%d\n",ee);
printf("此时顺序表为:\n");
print_Sq(L);
printf("\n-------------------清空顺序表---------------------\n");
ClearList_Sq(L);
if(ListEmpty_Sq(L)){
printf("\n清空成功\n");
}
printf("\n------------------销毁顺序表----------------------\n");
DestroyList_Sq(L);
getchar();
getchar();
return 0;
}