内容:编写一个程序 ,实现单链表的各种基本运算和整体建表算法(假设单链表的元素类型 〔elemtype 为 char))并在此基础上设计一个程序,完成如下功能:
(1)初始化单链表h。
(2)依次采用尾插法插入a、b、c、d、e元素。
(3)输出单链表h。
(4)输出单链表h长度。
(5)判断单链表h是否为空。
(6)输出单链表h的第3个元素。
(7)输出元素a的位置。
(8)在第4个元素位置上插入f元素。
(9)输出单链表h。
(10)删除单链表h的第3个元素。
(11)输出单链表h。
(12)释放单链表h。
#include<stdio.h>
#include<stdlib.h>
typedef char elemtype; //设置元素类型为char,即elemtype代替char
typedef struct Lnode
{
elemtype data;
struct Lnode* next;
}link_node;
void createlist_t(link_node*& L, elemtype a[], int n)//头插法建立单链表
{
link_node* s;
L = (link_node*)malloc(sizeof(link_node));//创建头结点
L->next = NULL;
for (int i = 0; i < n; i++)
{
s = (link_node*)malloc(sizeof(link_node));//创建新结点s
s->data = a[i];
s->next = L->next;//将结点s插在原开始结点之前,头结点之后
L->next = s;
}
}
void createlist_w(link_node*& L, elemtype a[], int n)//尾插法建立单链表
{
link_node* s,*r;
L = (link_node*)malloc(sizeof(link_node));
L->next = NULL;
r = L;
for (int i = 0; i < n; i++)
{
s = (link_node*)malloc(sizeof(link_node));
s->data = a[i];
r=s->next;
r = s;
}
r->next = NULL;
}
void intlist(link_node*&L)//初始化线性表
{
L = (link_node*)malloc(sizeof(link_node));//创建头结点
L->next = NULL;
}
void destroy_list(link_node*&L) //销毁线性表
{
link_node* pre = L,*p = pre->next;
while (p != NULL)
{
free(pre);
pre=p;
p = pre->next; //pre,p同时后移一个结点
}
free(pre); //此时p为NULL,pre指向尾结点,释放它
}
bool listempty(link_node* L) //判断线性表是否为空表
{
return (L->next == NULL);
}
int listlength(link_node* L)//求线性表的长度
{
int i = 0;
link_node* p = L->next;
while (p->next != NULL)
{
i++;
p = p->next;
}
return i;
}
void displist(link_node* L) //输出线性表
{
link_node* p = L->next; //p指向首结点
while (p != NULL)
{
printf("%c ", p->data);
p = p->next;
}
printf("\n");
}
bool getelem(link_node* L, int i, elemtype& e)//求线性表中第i个元素值
{
int j = 0;
link_node* p = L;
if (i <= 0)
{
return false;
}
while (j < i && p != NULL)
{
j++;
p = p->next;
}
if (p==NULL)
{
return false;
}
else
{
e = p->data;
return true;
}
}
int locateelem(link_node* L, elemtype e) //查找一个值域为e的元素符号
{
int i = 1; //首结点的序号为1
link_node* p = L->next;
while (p != NULL && p->data != e)
{
p = p->next;
i++;
}
if (p == NULL)
{
return 0;
}
else
{
return i;
}
}
bool listinsert(link_node*& L, int i, elemtype e)//插入第i个元素
{
int j = 0; //p指向头结点,=0;
link_node* p = L, * s;
if (i <= 0)
{
return false;
}
while (j < i - 1 && p!= NULL) //查找第i-个结点p
{
j++;
p = p->next;
}
if (p == NULL)
{
return false;
}
else
{
s = (link_node*)malloc(sizeof(link_node)); //创建新结点s,其data域置为e
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
}
bool listdelete(link_node*& L, int i, elemtype e) //删除第i个元素
{
int j = 0;
link_node* p = L, *q;
if (i <= 0)
{
return false;
}
while (j < i - 1 && p->data != NULL) //查找第i-1个结点p
{
j++;
p = p->next;
}
if (p == NULL)
{
return false;
}
else
{
q = p->next;
if (q == NULL)
{
return false;
}
e = q->data;
p->next = q->next;
free(q);
return true;
}
}
int main()
{
link_node* h=NULL;
elemtype e;
printf("单链表基本运算如下\n");
printf("1.初始化单链表h\n");
intlist(h);
printf("2.依此采用尾插法插入abcde元素\n");
listinsert(h, 1, 'a');
listinsert(h, 2, 'b');
listinsert(h, 3, 'c');
listinsert(h, 4, 'd');
listinsert(h, 5, 'e');
printf("输出单链表h\n");
displist(h);
printf("输出单链表h的长度\n");
listlength(h);
printf("单链表h是否为空?:%s\n",(listempty(h)?"空":"非空"));
printf("输出单链表h的第3个元素\n");
getelem(h, 3, e);
printf("输出元素a的位置%d\n", locateelem(h,'a'));
printf("在第4个元素位置上插入f元素\n");
listinsert(h, 4, 'f');
printf("输出单链表h\n");
displist(h);
printf("删除单链表h的第3个元素\n");
listdelete(h, 3, e);
printf("输出单链表h\n");
displist(h);
printf("释放单链表h\n");
destroy_list(h);
return 1;
}
长度若未显示可改为
printf("输出单链表h的长度%d\n",listlength(h));
运行结果如下: