//链式结构
#include <stdio.h>
#include<stdlib.h>
#include <malloc.h>
#define MAXSIZE 1000
typedef char ElemType;
typedef struct LNode{//定义单链表结点类型
ElemType data;
struct LNode *next;
}LinkList;
void InitList(LinkList *&L){//带头结点的单链表
L = (LinkList *)malloc(sizeof(LinkList));
L -> next = NULL;
}
void DestroyList(LinkList *&L){
LinkList *p = L,*q = p -> next;
while(q != NULL){
free(p);
p = q;
q = p -> next;
}
free(p);
}
int ListEmpty(LinkList *L){
return(L -> next == NULL);
}
int ListLength(LinkList *L){
LinkList *p = L;int i = 0;
while(p -> next != NULL){
i++;
p = p -> next;
}
return(i);
}
void DispList(LinkList *L){
LinkList *p = L -> next;
while (p != NULL){
printf("%c",p -> data);
p = p -> next;
}
printf("\n");
}
int GetElem(LinkList *L,int i,ElemType &e){
int j = 0;
LinkList *p =L;
while(j < i && p != NULL){
j++;
p = p -> next;
}
if (p == NULL)
return 0;
else {
e = p -> data;
return 1;
}
}
int LocateElem(LinkList *L,ElemType e){
LinkList *p = L -> next;
int n = 1;
while(p != NULL && p -> data != e){
p = p -> next;
n++;
}
if(p == NULL)
return(0);
else
return(n);
}
int ListInsert(LinkList *&L,int i,ElemType e){
int j = 0;
LinkList *p = L,*s;
while (j < i-1 && p != NULL){
j++;
p = p -> next;
}
if(p == NULL)//未找到第i-1个结点
return 0;
else //找到第i-1个结点*p
{
s = (LinkList *)malloc(sizeof(LinkList));//创建新结点*s
s -> data = e;
s -> next = p -> next; //将*s插入到*p之后
p -> next = s;
return 1;
}
}
int ListReplace(LinkList *&L,int i,ElemType e){//替换
int j = 0;
LinkList *p = L;
while(j < i && p != NULL){
j++;
p = p -> next;
}
if (p == NULL)//未找到第i-1个结点*p
return 0;
else{ //找到第i-1个结点
p -> data = e;
return 1;
}
}
int ListDelete(LinkList *&L,int i,ElemType &e){
int j = 0;
LinkList *p = L,*q;
while(j < i-1 && p != NULL){
j++;
p = p -> next;
}
if (p == NULL) //未找到第i-1个结点
return 0;
else{ //找到第i-1个结点*p
q = p -> next; //q指向要删除的结点
if(q == NULL) return 0;
e = q -> data;
p -> next = q -> next;//从单链表中删除*q结点
free(q); //释放*q结点
return 1;
}
}
void showmenu(){
printf("\n\n\n");
printf(" --线性表的基本运算-- \n");
printf("********************************************\n");
printf("* 1-----插入一个新元素到第i个位置 *\n");
printf("* 2-----删除第i个位置的元素 *\n");
printf("* 3-----存一个新元素到第i个位置 *\n");
printf("* 4-----显示链表中的所有元素值 *\n");
printf("* 5-----检索表中第i个元素 *\n");
printf("* 6-----求表的长度 *\n");
printf("* *\n");
printf("* 0-----退出 *\n");
printf("********************************************\n");
printf("请选择菜单号(0--6):");
}
void clear() //自动清屏
{
system("pause");
system("cls");
}
void Charu()
{
char choice = \'N\';
ElemType item;
int Position;
LinkList *L;
InitList(L); //创建一个顺序表
printf("请输入一个新元素的值:");
flushall();
scanf("%c",&item);
printf("请输入插入的位置:");
scanf("%d",&Position);
if (ListInsert(L,Position,item)){
printf("插入成功! \n");
}
else{
printf("插入失败!请检查输入位序号是否正确\n");
}
}
void Shanchu()
{
char choice = \'N\';
ElemType item;
int Position;