本文目录一览:
C语言顺序表的基本操作
#include stdio.h
#include stdlib.h
typedef int DataType; // 定义数据数据类型
typedef struct {
DataType *data; // data指向数据区的首个数据
int length; // 数据长度
}SqList;
void Sort(SqList *L) {
int i,j,k;
DataType tmp;
for(i = 0; i L-length - 1; ++i) {
k = i;
for(j = i + 1; j L-length; ++j)
if(L-data[k] L-data[j])
k = j;
if(k != i) {
tmp = L-data[k];
L-data[k] = L-data[i];
L-data[i] = tmp;
}
}
}
SqList *CreateList(DataType a[],int n) {
int i;
SqList *L;
L = (SqList *)malloc(sizeof(SqList));
L-data = (DataType *)malloc(n * sizeof(DataType));
L-length = n;
for(i = 0; i n; ++i) L-data[i] = a[i];
Sort(L);
return L;
}
int InsertList(SqList *L,DataType x) {
int i,j;
for (i = 0;i L-length;i++) {
if(x = L-data[i]) {
for(j = L-length;j = i;j--)
L-data[j + 1] = L-data[j]; // 结点后移
L-data[i] = x;
L-length++;
return 1;
}
}
L-data[L-length++] = x;
return 1;
}
int RemoveListElem(SqList *L,DataType d) {
int i,j;
for(i = 0; i L-length; ++i) {
if(L-data[i] == d) {
for(j = i; j L-length - 1; ++j)
L-data[j] = L-data[j + 1];
L-length--;
return 1;
}
}
return 0;
}
SqList *AndList(SqList *A, SqList *B) { /* A∩B */
int i,j,k = 0;
int len = (A-length B-length) ? B-length : A-length;
SqList *C = (SqList *)malloc(sizeof(SqList));
C-length = len;
C-data = (DataType *)malloc(len * sizeof(DataType));
for(i = 0; i A-length; ++i) {
for(j = 0; j B-length; ++j) {
if(A-data[i] == B-data[j]) {
C-data[k++] = A-data[i];
break;
}
}
}
C-length = k;
return C;
}
SqList *OrList(SqList *A, SqList *B) { /* A∪B */
int i,j,flag;
DataType e;
SqList *C = (SqList *)malloc(sizeof(SqList));
C-length = A-length + B-length;
C-data = (DataType *)malloc(C-length * sizeof(DataType));
for(i = 0; i A-length; ++i) C-data[i] = A-data[i];
for(i = 0; i B-length; ++i) {
e = B-data[i];
flag = 1;
for(j = 0; j C-length; ++j) {
if(e == C-data[j]) {
flag = 0;
break;
}
}
if(flag) InsertList(C,e);
}
return C;
}
void PrintList(SqList *L) {
int i;
for(i = 0; i L-length; ++i)
printf("%d ",L-data[i]);
printf("\n");
}
void FreeList(SqList *L) {
free(L-data);
free(L);
}
void main() {
DataType x;
DataType arra[] = {36,24,31,5,90,65,70,39,37};
DataType arrb[] = {9,8,43,51,37,89,33,46,29,80,56};
int alen = sizeof(arra)/sizeof(arra[0]);
int blen = sizeof(arrb)/sizeof(arrb[0]);
SqList *A = CreateList(arra,alen);
printf("A线性表为: ");
PrintList(A);
SqList *B = CreateList(arrb,blen);
printf("B线性表为: ");
PrintList(B);
SqList *C = AndList(A,B);
SqList *D = OrList(A,B);
printf(" C = A∩B: ");
PrintList(C);
printf(" D = A∪B: ");
PrintList(D);
printf("在D表中插入数据 : ");
scanf("%d",x);
InsertList(D,x);
printf("D表插入x后 :");
PrintList(D);
printf("删除D表中数据 : ");
scanf("%d",x);
RemoveListElem(D,x);
printf("删除x后的D表为 :");
PrintList(D);
FreeList(A);
FreeList(B);
FreeList(C);
FreeList(D);
}
使用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;in;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(i1||iL-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语言实现!
顺序存储的线性表的算法
#include "stdio.h"
#include "stdlib.h"
#define Status int
#define OVERFLOW 0
#define TRUE 1
#define FALSE 0
#define OK 1
#define MAXSIZE 100
typedef int ElemType;
typedef struct list
{ElemType elem[MAXSIZE];
int length;
}SqList;
void InitList(SqList L){
L.length=0;
}
/*建立顺序表*/
void CreateList(SqList L)
{
int i;
printf("input the length");
scanf("%d",L.length);//输入表长
for(i=1;i=L.length;i++)
scanf("%d",L.elem[i-1]);//输入元素
}
//顺序表的遍历
void printdata(ElemType e){
printf("%4d",e);
}
void Traverse(SqList L,void (*visit)(ElemType e))
{ int i;
printf("The elements of the lists are:\n");
for(i=1;i=L.length;i++){
if (i%10==0)printf("\n");//每行显示10个元素
visit(L.elem[i-1]);//输出表中元素
}
printf("\n");
}
//有序顺序表L中插入元素e使序列仍有序
void Insert(SqList L,ElemType e)
{int i,j;
if (L.length==MAXSIZE)exit(OVERFLOW);//表满,不能插入
for(i=1;i=L.lengthL.elem[i-1]=e;i++);//向后查找
for(j=L.length;j=i;j--)
L.elem[j]=L.elem[j-1];//元素后移
L.elem[i-1]=e;//插入e
L.length=L.length+1;//表长加1
}
//建立递增有序的顺序表
void CreateList_Sorted(SqList L)
{int i,num;
ElemType e;
L.length=0;
printf("Create a sorted list,Input the length of the list\n");
scanf("%d",num);
printf("Input the data %d numbers\n",num);
for(i=1;i=num;i++){
scanf("%d",e);
Insert(L,e);
}
}
/*Merge two sorted lists*/
void MergeList(SqList La,SqList Lb,SqList Lc)
{int *pa,*pb,*pc;
if (La.length+Lb.lengthMAXSIZE) exit(OVERFLOW);
else
{pa=La.elem;pb=Lb.elem;pc=Lc.elem;
while (paLa.elem+La.lengthpbLb.elem+Lb.length)
*pc++=(*pa=*pb)?*pa++:*pb++;/*公共部分合并*/
while (paLa.elem+La.length) *pc++=*pa++;
/*R1表的剩余部分放到R的后部*/
while (pbLb.elem+Lb.length) *pc++=*pb++;
/*R2表的剩余部分放到R的后部*/
Lc.length=La.length+Lb.length;/*R表长*/
}
}
//判断元素是否对称,对称返回TRUE 否则返回FALSE
Status Symmetric(SqList L)
{int low,high;
low=0;
high=L.length-1;
while(lowhigh)
if (L.elem[low]==L.elem[high]){low++;high--;}
else return(FALSE); return(TRUE); }
//顺序表的主函数部分
//#include "seqlist.h"
void main()
{SqList L1,L2,L;
int select;
ElemType e;
do{printf("\n1 insert 2 merge");
printf("\n3 symmetric 0 exit \n");
printf("Please select(0--3)\n");
scanf("%d",select);
switch(select){
case 1:
InitList(L);
CreateList_Sorted(L);
Traverse(L,printdata);
printf("\nInput the element of inserted\n");
scanf("%d",e);
Insert(L,e);
Traverse(L,printdata);
break;
case 2:
InitList(L1);
CreateList_Sorted(L1);
Traverse(L1,printdata);
InitList(L2);
CreateList_Sorted(L2);
Traverse(L2,printdata);
InitList(L);
MergeList(L1,L2,L);
Traverse(L,printdata);
break;
case 3:
InitList(L);
CreateList(L);
Traverse(L,printdata);
if (Symmetric(L)) printf("Yes!\n"); else printf("Not\n");
break;
case 0:break;
default:printf("Error! Try again!\n");
}
}while(select);
}
/*单向链表的有关操作示例*/
/*类型定义及头文件部分,文件名为sllink.h*/
#include stdio.h
#include stdlib.h
typedef int ElemType;//元素实际类型
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList; //定义结点、指针类型名
//头插法建立无序链表
void CreateList(LinkList L){
LinkList p;
ElemType e;
L=(LinkList)malloc(sizeof(LNode));
L-next=NULL;
printf("头插法建立链表,以0结束\n");
scanf("%d",e);
while(e){
p=(LinkList)malloc(sizeof(LNode));
p-data=e;
p-next=L-next;
L-next=p;
scanf("%d",e);
}
}
/*非递减有序单向链表L插入元素e序列仍有序*/
void Insert_Sort(LinkList L,ElemType e){
LinkList p,s;
s=(LinkList)malloc(sizeof(LNode));
s-data=e;
p=L;
while(p-nextp-next-data=e)
p=p-next;/*查找插入位置*/
s-next=p-next; /*插入语句*p结点后插入*s结点*/
p-next=s;
}
/*建立递增有序的单向链表*/
void Create_Sort(LinkList L){
ElemType e;
L=(LinkList)malloc(sizeof(LNode));
L-next=NULL;
printf("建立有序表,输入任意个整型数据以0结束\n");
scanf("%d",e);
while(e){
Insert_Sort(L,e);
scanf("%d",e);
}
}
/*单向链表的遍历*/
void Traverse(LinkList L){
LinkList p;
printf("遍历链表");
for(p=L-next;p;p=p-next)
printf("%5d",p-data);
printf("\n");
}
/*在单向链表删除元素e*/
void Delete(LinkList L,ElemType e){
LinkList p,q;
p=L;
q=L-next;
while(q q-data!=e){//查找元素的删除位置
p=q;
q=q-next;
}
if(!q) printf("\nnot deleted");/*未找到元素e*/
else {p-next=q-next;/*找到删除*/
free(q);}
}
/*单向链表的逆置*/
void exch(LinkList L){
LinkList p,s;
p=L-next;
L-next=NULL;
while(p){
s=p;
p=p-next;
s-next=L-next;
L-next=s;
}
}
/*两个非递减有序单向链表合并后仍为非递减序列*/
void MergeIncrease(LinkList La,LinkList Lb,LinkList Lc){
LinkList p,q,s,rear;
p=La-next;
q=Lb-next;
Lc=rear=La;
free(Lb);
while(pq){
if (p-dataq-data) {s=p;p=p-next; }
else {s=q;q=q-next; }
rear-next=s;/*较小元素插入表尾*/
rear=rear-next;
}
if (p) rear-next=p; else rear-next=q;
}
/*主函数部分,文件名为sllink.c*/
//#include "sllink.h"
void main(){
LinkList La,Lb,Lc;
ElemType e;
int select;
do{
printf(" 1 建立无序表,再删除指定元素\n");
printf(" 2 建立递增有序表,再逆置\n");
printf(" 3 建立两个递增有序表,将它们和并为一个仍递增表\n");
printf(" 0 退出,请输入选项(0-3)\n");
scanf("%d",select);
switch(select){
case 0:
break;
case 1:
CreateList(La);
Traverse(La);
printf("输入需删除的元素\n");
scanf("%d",e);
Delete(La,e);
Traverse(La);
break;
case 2:
Create_Sort(La);
Traverse(La);
exch(La);
printf("The list that is exchaged\n");
Traverse(La);
break;
case 3:
Create_Sort(La);Traverse(La);
Create_Sort(Lb);Traverse(Lb);
MergeIncrease(La,Lb,Lc);Traverse(Lc);
break;
default:
printf("输入选项错误,请重新输入!\n");
}
}while(select);
}
这些内容不知道能不能帮助到你。