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;
}