顺序表
#include <stdio.h>
#include <stdlib.h>
#define INITSIZE 50
typedef int ElemType;
typedef struct LNode{
ElemType *data;
int Maxsize,lentgh;
}SqlList;
SqlList init(SqlList &L){
L.data=(ElemType*)malloc(sizeof(ElemType)*INITSIZE);
L.Maxsize=100;
L.lentgh=5;
ElemType x;
for(int i=0;i<L.lentgh; i++){
scanf("%d",&x);
L.data[i] = x;
}
return L;
}
bool ListInsert(SqlList &L, int i, ElemType x){
if(i<1 || i>L.lentgh+1){
return false;
}
if(L.lentgh>=L.Maxsize){
return false;
}
for (int j=L.lentgh;j>=i;j--){
L.data[j] = L.data[j-1];
}
L.data[i-1] = x;
L.lentgh++;
return true;
}
bool ListDelete(SqlList &L, int i, ElemType &e){
if(i<1 || i>L.lentgh){
return false;
}
e=L.data[i-1];
for(int j=i; j<L.lentgh; j++){
L.data[j-1] = L.data[i];
}
L.lentgh--;
return true;
}
int LocateElem(SqlList L,ElemType e){
for(int i=0; i<L.lentgh; i++){
if(L.data[i]==e){
return i+1;
}
}
return 0;
}
void SqlList_print(SqlList L){
for(int i=0; i<L.lentgh; i++){
printf("%d ",L.data[i]);
}
printf("\n");
}
int main() {
SqlList L;
init(L);
SqlList_print(L);
ListInsert(L,4,34);
SqlList_print(L);
ElemType e;
ListDelete(L,5,e);
SqlList_print(L);
printf("值为34的元素在顺序表中的下标:%d",LocateElem(L,34));
return 0;
}
单链表
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct LNode{
ElemType data;
LNode *next;
}LNode, *LinkList;
LinkList List_HeadInsert(LinkList &L){
LNode *s;
int x;
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
scanf("%d",&x);
while(x!=999){
s=(LNode*)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
scanf("%d",&x);
}
return L;
}
LinkList List_TailInsert(LinkList &L){
int x;
L=(LinkList)malloc(sizeof(LNode));
LNode *s,*r=L;
scanf("%d",&x);
while(x!=999){
s=(LNode *)malloc(sizeof(LNode));
s->data=x;
r->next=s;
r=s;
scanf("%d",&x);
}
r->next = NULL;
return L;
}
LNode *GetElem(LinkList L, int i){
int j=1;
LNode *p=L->next;
if(i==0){
return L;
}
if(i<1){
return NULL;
}
while(p && j<i){
p = p->next;
j++;
}
return p;
}
LNode *LocateElem(LinkList L,ElemType e){
LNode *p=L->next;
while(p && p->data!=e){
p=p->next;
}
return p;
}
LinkList List_add(LinkList L,ElemType x,int i){
LNode *s,*p;
s=(LNode *)malloc(sizeof(LNode));
p=(LNode *)malloc(sizeof(LNode));
p = GetElem(L,i-1);
s->data = x;
s->next=p->next;
p->next=s;
return L;
}
LinkList List_delete(LinkList &L,int i){
LNode *q,*p;
q=(LNode *)malloc(sizeof(LNode));
p=(LNode *)malloc(sizeof(LNode));
p=GetElem(L,i-1);
q=p->next;
p->next=q->next;
free(q);
return L;
}
LinkList List_delete2(LinkList &L,ElemType x){
LNode *q,*p;
q=(LNode *)malloc(sizeof(LNode));
p=(LNode *)malloc(sizeof(LNode));
p=LocateElem(L,x);
q=p->next;
p->data=q->data;
p->next=q->next;
free(q);
return L;
}
void List_print(LinkList L){
L=L->next;
while(L){
printf("%d,",L->data);
L=L->next;
}
printf("\n");
}
int List_Length(LinkList L){
int cout=0;
L=L->next;
while(L){
cout++;
L=L->next;
}
return cout;
}
int main() {
LinkList L;
L=List_HeadInsert(L);
printf("单链表中的元素:\n");
List_print(L);
printf("单链表长度:%d\n",List_Length(L));
LNode *p,*q;
p=(LNode *)malloc(sizeof(LNode));
q=(LNode *)malloc(sizeof(LNode));
p=GetElem(L,2);
printf("单链表中第2个结点的值:%d\n",p->data);
List_delete(L,2);
printf("删除第2个位置的元素后的链表为:\n");
List_print(L);
printf("在第3个位置插入34后,新链表为:\n");
List_add(L,34,3);
List_print(L);
printf("删除链表中第一个值为23的元素后,新链表为:\n");
List_delete2(L,23);
List_print(L);
return 0;
}