求线性表的一些操作 希望能给写出程序c语言的 好的话加分 也可以发到邮箱 [email protected]

键盘输入一组无序数据,添加到线性表中; 排序线性表并输出排序结果; 键盘输入一个数,并插入到排好序的线性表中。
键盘输入一个数,并删除此数据

/*
14 93 81 96 50 78 13 47 26 52 41 21 37 84 53 8 55 41 73 48
8 13 14 21 26 37 41 41 47 48 50 52 53 55 73 78 81 84 93 96
Press any key to continue
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//函数返回状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define Status int //函数的返回值
#define ElemType int //线性表元素定义

#define LIST_INIT_SIZE 50 //线性表存储空间的初始分配量
#define LISTINCREMENT 10 //线性表存储空间的分配增量
typedef struct Sq {
ElemType *elem; //存储空间基址
int length; //当前长度
int listsize; //当前分配的存储容量(以sizeof(ElemType)为单位)
}SqList;

Status InitList_Sq(SqList *L) { // 构造一个空的线性表L。
L->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (!L->elem) return OVERFLOW; // 存储分配失败
L->length = 0; // 空表长度为0
L->listsize = LIST_INIT_SIZE; // 初始存储容量
return OK;
}

void Sort(SqList *L) {
int i,j,k;
ElemType t;
for(i = 0; i < L->length - 1; ++i) {
k = i;
for(j = i + 1; j < L->length; ++j)
if(L->elem[k] > L->elem[j]) k = j;
if(k != i) {
t = L->elem[k];
L->elem[k] = L->elem[i];
L->elem[i] = t;
}
}
}

Status ListInsert(SqList *L,ElemType e) {
if(L->length >= L->listsize) { //当前存储空间已满,增加分配
L->elem = (ElemType *)realloc(L->elem,(L->listsize + LISTINCREMENT)*sizeof(ElemType));
if(L->elem == NULL) return OVERFLOW; //存储分配失败
L->listsize += LISTINCREMENT; //增加存储容量
}
L->elem[L->length] = e; //插入e
++L->length; //表长增1
return OK;
}

int main() {
int i,n = 20;
SqList *L = (SqList *)malloc(sizeof(Sq));
srand((unsigned)time(NULL));
InitList_Sq(L);
for(i = 0; i < n; ++i) ListInsert(L,(ElemType)rand()%100 + 1);
for(i = 0; i < n; ++i) printf("%d ",L->elem[i]);
printf("\n");
Sort(L);
for(i = 0; i < n; ++i) printf("%d ",L->elem[i]);
printf("\n");
free(L->elem);
free(L);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-04-09
给你一个量表合并待排序的
你自己将其中一个链表改成#include<stdio.h>
#include<stdlib.h>
typedef struct lnote
{
int data;
struct lnote* next;
}LNode,*LinkList;
LinkList Get_LinkList()
{
LinkList L=NULL;
LNode *s,*r=NULL;
s=(LNode*)malloc(sizeof(LNode));
s->data=0;
s->next=NULL;
L=s;
r=s;
int x;
scanf("%d",&x);
while(x!=-1)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
r->next=s;
r=s;
scanf("%d",&x);
}
if(r!=NULL)
r->next=NULL;
return L;
}
void PrintList(LinkList L)
{
LNode *f=L->next;printf("\tH");
while(1)
{
printf("-->%d",f->data);
if(f->next!=NULL)
f=f->next;
else
break;
}
printf("\n");
}
LinkList Combine_List(LinkList A,LinkList B)
{
LinkList C=NULL;
LNode *s,*a=A->next,*b=B->next;
s=(LNode*)malloc(sizeof(LNode));
s->data=0;
s->next=NULL;
C=s;
LNode*c=C;
while(a&&b)
{
s=(LNode*)malloc(sizeof(LNode));
if(a->data<b->data){s->data=a->data;a=a->next;}
else {s->data=b->data;b=b->next;}
s->next=c->next;//即为空
c->next=s;
c=s;
}
if(a==NULL)a=b;//a不空
while(a)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=a->data;
a=a->next;
s->next=c->next;//即为空
c->next=s;
c=s;
}
return C;
}
void main()
{
LinkList L,L0,C;
printf("请输入升序链表1(输入'-1'结束):\n");
L=Get_LinkList();
printf("链表1:\n");
PrintList(L);
printf("请按顺序输入升序链表2(输入'-1'结束):\n");
L0=Get_LinkList();
printf("链表2:\n");
PrintList(L0);
C=Combine_List(L,L0);
printf("合并后的链表为:\n");
PrintList(C);
}结点就行了本回答被提问者采纳
第2个回答  2012-04-09
像这些你可以上王道论坛,专业计算机考研论坛 上面很多考研用的C 和数据结构的东西
相似回答
大家正在搜