程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 數據結構。順序表,數據結構順序表

數據結構。順序表,數據結構順序表

編輯:關於C語言

數據結構。順序表,數據結構順序表


#include <stdio.h>
#include <stdlib.h>

#define LIST_INIT_SIZE 100
#define LIST_INCREMENT 10
typedef struct Point   //element type
{
    int x;
    int y;
}Point;
typedef Point ElemType;
typedef struct          //list type
{
    ElemType *elem;     //data
    int length;         //current length
    int listsize;       //maximum size
}Sqlist;
int cpy(ElemType *des,ElemType data)
{
    des->x=data.x;
    des->y=data.y;
    return 0;
}
int elem_cmp(ElemType data1,ElemType data2);
int list_init(Sqlist *mylist);
int list_insert(Sqlist *mylist,ElemType data);
int list_find(Sqlist *mylist,ElemType data);
int list_delete(Sqlist *mylist,ElemType data);
int list_disp(Sqlist mylist);
int main()
{
    Sqlist mylist;
    list_init(&mylist);
    printf("%d,%d\n",mylist.length,mylist.listsize);
    printf("Hello world!\n");

    Point tempt;
    tempt.x=11;
    tempt.y=12;

    list_insert(&mylist,tempt);
    list_disp(mylist);
    printf("%d,%d\n",mylist.length,mylist.listsize);
    getchar();

    tempt.x=21;
    tempt.y=22;
    list_insert(&mylist,tempt);
    list_disp(mylist);
    printf("%d,%d\n",mylist.length,mylist.listsize);
    getchar();

    list_delete(&mylist,tempt);
    list_disp(mylist);
    printf("%d,%d\n",mylist.length,mylist.listsize);
    getchar();

    return 0;
}
int elem_cmp(ElemType data1,ElemType data2)
{
    if( (data1.x==data2.x) &&(data1.y==data2.y) )
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int list_init(Sqlist *mylist)
{
    mylist->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(mylist->elem!=NULL)
    {
        mylist->length=0;
        mylist->listsize=LIST_INIT_SIZE;

        printf("%d,%d\n",mylist->length,mylist->listsize);
        return 0;
    }
    else
    {
        printf("allocate memory is failed! sorry\n");
        return 1;
    }
}
int list_insert(Sqlist *mylist,ElemType data)
{
    if(mylist->length>=mylist->listsize)
    {
        //if the memo is not enougth to use,realloc
        mylist->elem=(ElemType*)realloc(mylist->elem,LIST_INIT_SIZE+LIST_INCREMENT);
        if(mylist->elem==NULL)
        {
            printf("memo realloc is failed! sorry,friend\n");
            return 1;
        }
        else
        {
            mylist->listsize+=LIST_INCREMENT;
        }
    }
    cpy(&mylist->elem[mylist->length],data);
    mylist->length+=1;
    return 0;
}
int list_find(Sqlist *mylist,ElemType data)
{
    int i=0;
    for(i=0;i<mylist->length;i++)
    {
        if(elem_cmp(mylist->elem[i],data)>0)
        {
            return i+1;
        }
    }
    return 0;
}
int list_delete(Sqlist *mylist,ElemType data)
{
    int i=0,result=list_find(mylist,data);
    if(result==0)
    {
        printf("not found the data \n");
        return 1;
    }
    else
    {
        for(i=result-1;i<mylist->length;i++)
        {
            mylist->elem[i]=mylist->elem[i+1];
        }
        mylist->length--;
        return 0;
    }
}
int list_disp(Sqlist mylist)
{
    system("clear");
    int j=0;
    for(j=0;j<mylist.length;j++)
    {
        printf("{%d,%d}\t",mylist.elem[j].x,mylist.elem[j].y);
    }
    return 0;
}


數據結構順序表

不知道這個程序能不能滿足你的要求,該程序用順序表方式,能實現刪除線性表的元素,之後還可以往線性表中插入元素,後附有運行情況圖:
程序如下
#include<iostream>
using namespace std;
#include<malloc.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OVERFLOW -1
#define OK 1
#define ERROR 0
typedef int Status;
typedef int ElemType;
typedef int KeyType;
typedef struct{
ElemType *elem;
KeyType *key;
int length;
int listsize;
}SqList;
typedef struct{
KeyType key;
}SElemType;
Status InitList (SqList &L){ /*定義一個線性表*/
int length1;
printf("請確定順序表的長度:");
scanf("%d",&length1);
L.listsize=length1;
L.elem=(ElemType*)malloc(length1*sizeof(ElemType));
if(!L.elem){
printf("out of space");
exit(OVERFLOW);
}
L.length=0;
return OK;
}

Status Listinsert (SqList &L,int i, ElemType e){ /*在i元素的前面插入元素e*/
ElemType *p,*q,*newbase;
if(i<1||i>L.length+1){
return ERROR;
}
if(L.length>L.listsize)
{
newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if (newbase==NULL){
printf("out of space");
return (OVERFLOW);
}
L.listsize+=LISTINCREMENT;
}
p=&( L.elem[i-1]);
for (q=&(L.elem[L.length-1]) ;q>=p;q--){
*(q+1)=*q;
}
L.elem[i-1]=e;
L.length++;

return OK;
}
Status DeleteList (SqList &L,int i){/*刪除i個元素*/
ElemType *q ,*p;
if(i<1||i>L.length){
return ERROR;
}
q = &(L.elem[i-1]);
p = L......余下全文>>

 

數據結構順序表

#include<stdio.h>
#include<malloc.h> //頭文件包含命令

#define List_init_size 100 //符號常量的定義
#define Listincrement 100
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

typedef int Elemtype;//抽象數據類型elemtype具體化為int類型
typedef int Status;
typedef struct { //定義結構體
Elemtype *elem;
int length;
int listsize;
}Sqlist;
Status Initlist_sq(Sqlist *l) //函數(初始化順序表)初始化失敗則返回-2
{
l->elem =(Elemtype*)malloc(sizeof(Elemtype));
if(!l->elem ) return(OVERFLOW);
l->length =0;
l->listsize =List_init_size;
return OK;
}

Status Creatlist_sq(Sqlist *l,int n) //初始化順序表為含有n個元素的順序表
{
int i;
printf("Input %d numbers:\n",n);
for(i=0;i<n;i++)
scanf("%d",&(l->elem [i]));
l->length =n;
return OK;
}

void Outputlist_sq(Sqlist *l) //函數(輸出順序表的元素)
{
int i;
for(i=0;i<l->length ;i++)
{
printf("%5d",l->elem [i]);
if((i+1)%5==0)
printf("\n");
}
printf("\n");

}
void Destroylist_sq(Sqlist*l) //銷毀順序表l
{
free(l->elem );

}

Status Insertlist_sq(Sqlist*l,int i,Elemtype e) //在順序表的第i個位置前插入元素e
{
int j;
Elemtype *p;
if(i>l->length )
{
printf("輸入數據不合法!");
return (OVERFLOW);
}

if(l->length>=l->listsize )
{
Elemtype *p;
p=(Elemtype*)realloc(l->elem ,(List_init_size+Listincrement)*sizeof(Elemtype));
if(p==0)
{ ......余下全文>>
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved