一,实现顺序表的各种基本运算
#include<stdio.h>
#include<stdlib.h>
#define ElemType char
#define MaxSize 50
typedef struct
{
ElemType data[MaxSize];
int length;
}Sqlist;
void CreatList(Sqlist *&L,ElemType a[],int n)
{
int i=0,k=0;
L=(Sqlist *)malloc(sizeof(Sqlist));
while(i<n)
{
L->data[k]=a[i];
k++;i++;
}
L->length=k;
printf("创建成功\n\n");
}
void InitList(Sqlist *&L)
{
L=(Sqlist *)malloc(sizeof(Sqlist));
L->length=0;
printf("初始化成功\n");
}
void DestroyList(Sqlist *L)
{
free(L);
printf("销毁成功\n");
}
void DispList(Sqlist *L)
{
for(int i=0;i<L->length;i++)
printf("%c ",L->data[i]);
printf("\n");
}
bool ListEmpty(Sqlist *L)
{
if(L->length==0)
return false;
else
return true;
}
int ListLength(Sqlist *L)
{
return(L->length);
printf("%d",L->length);
}
bool GetElem(Sqlist *L,int i,ElemType &e)
{
if(i<1||i>L->length)
return false;
e=L->data[i-1];
printf("%c ",e);
return true;
}
int LocateElem(Sqlist *L,ElemType e)
{
int i=0;
while(i<L->length&&L->data[i]!=e)
i++;
if(i>=L->length)
return 0;
else
return (i+1);
}
bool ListInsert(Sqlist *L,int i,ElemType e)
{
int j;
if(i<1||i>L->length+1)
return false;
i--;
for(j=L->length;j>i;j--)
{
L->data[j]=L->data[j-1];
}
L->data[i]=e;
L->length++;
return true;
}
bool ListDelete(Sqlist *L,int i,ElemType &e)
{
if(i<1||i>L->length)
return false;
int j;
i--;
e=L->data[i];
for(j=i;j<L->length-1;j++)
{
L->data[j]=L->data[j+1];
}
L->length--;
printf("删除成功\n");
return true;
}
int main()
{
int num;
int i;
char a,b,c,d,e,f,j,x,y;
Sqlist *L;
char A[50]={0};
CreatList(L,A,50);
InitList(L);
printf("请插入a,b,c,d,e五个元素\n");
for(i=1,j='a';i<6;i++,j++)
ListInsert(L,i,j);
DispList(L);
printf("插入成功\n");
printf("请输入你要进行的操作\n");
while(scanf("%d",&num))
{
switch(num)
{
case 1:
printf("输出元素a的位置\n");
printf("%d\n",LocateElem(L,'a'));
break;
case 2:
printf("取出第3个元素\n");
GetElem(L,3,x);
printf("\n");
break;
case 3:
printf("在第四个元素位置上插入f\n");
ListInsert(L,4,'f');
DispList(L);
printf("插入成功\n");
break;
case 4:
printf("删除第3个元素\n");
ListDelete(L,3,y);
DispList(L);
printf("删除成功\n");
break;
case 5:
DestroyList(L);
break;
default:
break;
}
}
system("pause");
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/eb00f69d2e2dd3874036526a697dad3f.png)
二,实现单链表的各种基本运算的算法
(1)
#include <stdio.h>
#include <stdlib.h>
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)
{
int i = 0;
LinkList *p = L;
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)
return 0;
else
{
s = (LinkList *)malloc(sizeof(LinkList));
s->data = e;
s->next = p->next;
p->next = s;
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)
return 0;
else
{
q = p->next;
if(q == NULL)
return 0;
e = q->data;
p->next = q->next;
free(q);
return 1;
}
}
int main()
{
LinkList *h;
ElemType e;
printf("(1)初始化单链表h\n");
InitList(h);
printf("(2)依次采用尾插法插入a、b、c、d、e元素\n");
ListInsert(h, 1, 'a');
ListInsert(h, 2, 'b');
ListInsert(h, 3, 'c');
ListInsert(h, 4, 'd');
ListInsert(h, 5, 'e');
printf("(3)输出单链表h:");
DispList(h);
printf("(4)单链表h长度 = %d\n", ListLength(h));
printf("(5)单链表h为%s\n", (ListEmpty(h) ? "空" : "非空"));
GetElem(h, 3, e);
printf("(6)单链表h的第3个元素 = %c\n", e);
printf("(7)元素a的位置 = %d\n", LocateElem(h, 'a'));
printf("(8)在第4个元素位置上插入f元素\n");
ListInsert(h, 4, 'f');
printf("(9)输出单链表h:");
DispList(h);
printf("(10)删除h的第三个元素\n");
ListDelete(h, 3, e);
printf("(11)输出单链表h:");
DispList(h);
printf("(12)释放单链表h\n");
DestroyList(h);
system("pause");
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/88b2525f55f903ccb47f9f289edf92ef.png)
(2)
#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
int num;
}stu;
struct node
{
stu data;
struct node *next;
};
void Initlist(node *&head)
{
node *Head = (node *)malloc(sizeof(node));
Head->data.num = 10;
Head->next = NULL;
head = Head;
}
void addlist(node *&head)
{
node *p;
p = head;
while (p->next != NULL)
p = p->next;
node *q = (node*)malloc(sizeof(node));
scanf("%d", &q->data.num);
q->next = p->next;
p->next = q;
printf("增加成功!\n");
}
void deletelist(node *&head)
{
int n;
if (head == NULL)
{
printf("链表为空\n");
}
node *p, *p1 = NULL;
p = head;
printf("请输入你要删除的数据\n");
scanf("%d", &n);
while (p->data.num != n && p->next != NULL)
{
p1 = p;
p = p->next;
}
if (p->data.num = n)
{
if (p == head)
{
head = p->next;
}
else
{
p1->next = p->next;
}
free(p);
printf("删除成功\n");
}
else
{
printf("没有找到\n");
}
}
void alterlist(node *&head)
{
int n; int n1;
node *p;
p = head;
printf("请输入修改的数据\n");
scanf("%d", &n);
printf("请输入你修改后的数据\n");
scanf("%d", &n1);
while (p->data.num != n && p->next != NULL)
{
p = p->next;
}
if (p->data.num = n)
{
p->data.num = n1;
printf("修改成功\n");
}
else
printf("没有找到数据\n");
}
void printlist(node *&head)
{
node *p;
p = head;
while (p->next != NULL)
{
printf("% d", p->data);
p = p->next;
}
printf("% d", p->data);
}
int listlength(node *&head)
{
int n = 0;
node *p = head;
while (p->next != NULL)
{
n++;
p = p->next;
}
return n+1;
}
void nixu(node *&head)
{
node *p = head;
node *p1 = head->next;
node *p2=NULL;
if (p == NULL || p1 == NULL)
return;
while (p1!= NULL)
{
p2= p1->next;
p1->next = p;
p = p1;
p1 = p2;
}
head->next=NULL;
head=p;
}
int main()
{
node *head = NULL;
int number;
Initlist(head);
printf("请输入你要进行的操作\n");
while ( scanf("%d", &number))
{
switch (number)
{
case 1:
printf("增加一个数据\n");
addlist(head);
printlist(head);
printf("\n");
break;
case 2:
printf("删除一个数据\n");
deletelist(head);
printlist(head);
printf("\n");
break;
case 3:
printf("修改链表\n");
alterlist(head);
printlist(head);
printf("\n");
break;
case 4:
printf("输出链表\n");
printlist(head);
printf("\n");
break;
case 5:
int n;
printf("求链表的长度\n");
printf("%d\n",listlength(head));
break;
case 6:
printf("逆序输出链表\n");
nixu(head);
printlist(head);
printf("\n");
break;
default:
printf("请重新输入\n");
}
}
system("pause");
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/21c5906181791a7f9f065f1743f604c5.png)
三,实现双链表的各种基本运算的算法
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct DNode
{
ElemType data;
struct DNode *prior;
struct DNode *next;
}DLinkList;
void InitList(DLinkList *&L)
{
L = (DLinkList *)malloc(sizeof(DLinkList));
L->prior = L->next = NULL;
}
void DestroyList(DLinkList *&L)
{
DLinkList *p = L, *q = p->next;
while (q != NULL)
{
free(p);
p = q;
q = p->next;
}
free(p);
}
int ListEmpty(DLinkList *L)
{
return (L->next == NULL);
}
int ListLength(DLinkList *L)
{
int i = 0;
DLinkList *p = L;
while (p->next != NULL)
{
i++;
p = p->next;
}
return (i);
}
void DispList(DLinkList *L)
{
DLinkList *p = L->next;
while (p != NULL)
{
printf("%c ", p->data);
p = p->next;
}
printf("\n");
}
int GetElem(DLinkList *L, int i, ElemType &e)
{
int j = 0;
DLinkList *p = L;
while ((j < i) && (p != NULL))
{
j++;
p = p->next;
}
if (p == NULL)
return 0;
else
{
e = p->data;
return 1;
}
}
int LocateElem(DLinkList *L, ElemType e)
{
int n = 1;
DLinkList *p = L->next;
while ((p != NULL) && (p->data != e))
{
n++;
p = p->next;
}
if (p == NULL)
return 0;
else
return n;
}
int ListInsert(DLinkList *&L, int i, ElemType e)
{
int j = 0;
DLinkList *p = L, *s;
while ((j < i - 1) && (p != NULL))
{
j++;
p = p->next;
}
if (p == NULL)
return 0;
else
{
s = (DLinkList *)malloc(sizeof(DLinkList));
s->data = e;
s->next = p->next;
if (p->next != NULL)
p->next->prior = s;
s->prior = p;
p->next = s;
return 1;
}
}
int ListDelete(DLinkList *&L, int i, ElemType &e)
{
int j = 0;
DLinkList *p = L, *q;
while ((j < i - 1) && (p != NULL))
{
j++;
p = p->next;
}
if (p == NULL)
return 0;
else
{
q = p->next;
if (q == NULL)
return 0;
e = q->data;
p->next = q->next;
if (p->next != NULL)
p->next->prior = p;
free(q);
return 1;
}
}
int main(void)
{
DLinkList *h;
ElemType e;
printf("(1)初始化双链表h\n");
InitList(h);
printf("(2)依次采用尾插法插入a、b、c、d、e元素\n");
ListInsert(h, 1, 'a');
ListInsert(h, 2, 'b');
ListInsert(h, 3, 'c');
ListInsert(h, 4, 'd');
ListInsert(h, 5, 'e');
printf("(3)输出双链表h:");
DispList(h);
printf("(4)双链表h长度 = %d\n", ListLength(h));
printf("(5)双链表h为%s\n", (ListEmpty(h)? "空" : "非空"));
GetElem(h, 3, e);
printf("(6)双链表h的第3个元素 = %c\n", e);
printf("(7)元素a的位置 = %d\n", LocateElem(h, 'a'));
printf("(8)在第4个元素位置上插入f元素\n");
ListInsert(h, 4, 'f');
printf("(9)输出单链表h:");
DispList(h);
printf("(10)删除h的第三个元素\n");
ListDelete(h, 3, e);
printf("(11)输出双链表h:");
DispList(h);
printf("(12)释放双链表h\n");
DestroyList(h);
system("pause");
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/000d26bad4f64c2d6fc56320e25dc7a2.png)