C语言:
#include <stdio.h>
#include <stdlib.h>
#define ERROR 1
#define OK 0
/* 单链表结点定义 */
typedef struct LNode
{
int data; //data中存放结点数据域(默认是int型)
struct LNode *next; //指向后继结点的指针
}LNode;
//操作函数的声明
LNode *HeadCreateList(int len); //头插法建立单链表
LNode *TailCreateList(int len); //尾插法建立单链表
int Serch(LNode *L, int elem); //在表中寻找元素的位置
LNode *Replace(LNode *L, int pos, int elem); //替换第pos个位置的元素成elem
LNode *Insert(LNode *L, int pos, int elem); //在表中插入新结点
LNode *Delete(LNode *L, int pos, int *elem); //删除表中的结点
void PrintfList(LNode *L); //打印链表
int MenuSelect(void); //菜单
//测试函数的声明
void Test1(LNode *L); //测试"Serch"函数
void Test2(LNode *L); //测试"Replace"函数
void Test3(LNode *L); //测试"Insert"函数
void Test4(LNode *L); //测试"Delete"函数
LNode *Test5(void); //测试"TailCreateList"函数
LNode *Test6(void); //测试"HeadCreateList"函数
//主函数
int main(void)
{
int len = 0;
int cmd;
LNode *L;
/* 初始默认为尾插法建立单链表 */
printf("Please input list length:");
scanf("%d",&len);
L = TailCreateList(len);
PrintfList(L);
while(1)
{
cmd = MenuSelect();
switch(cmd)
{
case 1: Test1(L); break; //测试"Serch"函数
case 2: Test2(L); break; //测试"Replace"函数
case 3: Test3(L); break; //测试"Insert"函数
case 4: Test4(L); break; //测试"Delete"函数
case 5: L=Test5(); break; //测试"TailCreateList"函数
case 6: L=Test6(); break; //测试"HeadCreateList"函数
case 7: system("cls"); break; //清屏
case 8: exit(0); break; //退出
}
}
return 0;
}
//头创建
LNode *HeadCreateList(int len)
{
LNode *L = (LNode*)malloc(sizeof(LNode)); //创建一个头结点
LNode *temp = L;//声明一个中间变量,指向头结点,用于遍历链表(曾因没有这个中间变量而出错)
temp->next = NULL; //该链表此刻只带头结点
for(int i=1;i<=len;i++) //循环申请len个结点来接收scanf得到的元素
{
LNode *p = (LNode*)malloc(sizeof(LNode)); //生成新结点
scanf("%d",&p->data); //用新申请的结点来接收scanf得到的元素
/* 以下两条语句是头插法的关键步骤,与本工程"Insert"函数原理一样 */
p->next = temp->next; //新结点的next指针指向开始结点
temp->next = p; //头结点的next指针指向新结点
}
return (LNode*)L;
}
//尾插法创建单链表
LNode *TailCreateList(int len)
{
LNode *L = (LNode*)malloc(sizeof(LNode)); //创建一个头结点
LNode *temp = L;//声明一个中间变量,指向头结点,用于遍历链表(曾因没有这个中间变量而出错)
temp->next = NULL;//该链表此刻只带头结点
for(int i=1;i<=len;i++) //循环申请len个结点来接收scanf得到的元素
{
LNode *p = (LNode*)malloc(sizeof(LNode)); //生成新结点
scanf("%d",&p->data); //用新申请的结点来接收scanf得到的元素
/* 以下两条语句是尾插法的关键步骤 */
temp->next = p; //用来接纳新结点
temp = p; //指向终端结点,以便于接纳下一个到来的结点,此语句也可以改为"L = L->next"
}
temp->next = NULL; //此刻所有元素已经全装入链表L中,L的终端结点的指针域置为NULL
return (LNode*)L;
}
//Search
int Serch(LNode *L, int elem)
{
LNode *temp = L;
int pos = 0;
int i = 1;
while(temp->next)
{
temp = temp->next;
if(elem==temp->data)
{
pos = i;
printf("The %d position in the list is %d\n",elem,pos);
return pos; //返回elem元素在顺序表中的位置
}
i++;
}
printf("Serch error!\n");
return ERROR; //查找失败
}
//Replace
LNode *Replace(LNode *L, int pos, int elem)
{
LNode *temp = L; //引入一个中间变量,用于循环变量链表
temp = temp->next; //在遍历之前,temp指向开始结点
for(int i=1;i<pos;i++)
{
temp = temp->next;
}
temp->data = elem; //找到要替换的结点并替换其数据域的值为elem
return (LNode*)L; //注意!!不能写为 "return (LNode*)temp;"
}
//Insert
LNode *Insert(LNode *L, int pos, int elem)
{
LNode *temp = L; //引入一个中间变量,用于循环变量链表
int i = 0;
/* 首先找到插入结点的上一结点,即第pos-1个结点 */
while( (temp!=NULL)&&(i<pos-1) )
{
temp = temp->next;
++i;
}
/* 错误处理:链表为空或插入位置不存在 */
if( (temp==NULL)||(i>pos-1) )
{
printf("%s:Insert false!\n",__FUNCTION__);
return (LNode*)temp;
}
LNode *new = (LNode*)malloc(sizeof(LNode)); //创建新结点new
new->data = elem; //插入的新结点的数据域
new->next = temp->next; //新结点的next指针指向插入位置后的结点
temp->next = new; //插入位置前的结点的next指针指向新结点
return (LNode*)L; //注意!!不能写为 "return (LNode*)temp;"
}
/*********************************************************************************
* Function Name : Delete, 删除链表中的结点
* Parameter : L:链表 pos:要删除的位置 elem:被删除的结点的数据域
* Return Value : 删除结点之后生成的新链表
* Function Explain :
* Create Date : 2018.2.13 by lzn
**********************************************************************************/
LNode *Delete(LNode *L, int pos, int *elem)
{
LNode *temp = L; //引入一个中间变量,用于循环变量链表
int i = 0;
/* 首先找到删除结点的上一结点,即第pos-1个结点 */
while( (temp!=NULL)&&(i<pos-1) )
{
temp = temp->next;
++i;
}
/* 错误处理:链表为空或删除位置不存在 */
if( (temp==NULL)||(i>pos-1) )
{
printf("%s:Delete false!\n",__FUNCTION__);
return (LNode*)temp;
}
LNode *del = temp->next; //定义一个del指针指向被删除结点
*elem = del->data; //保存被删除的结点的数据域
temp->next = del->next; /*删除结点的上一个结点的指针域指向删除结点的下一个结点,
也可写为“temp->next = temp->next->next”*/
free(del); //手动释放该结点,防止内存泄露
del = NULL; //防止出现野指针
return (LNode*)L; //注意!!不能写为 "return (LNode*)temp;"
}
/*********************************************************************************
* Function Name : PrintfList,打印链表
* Parameter : L:要打印的链表
* Return Value : NULL
* Function Explain :
* Create Date : 2018.2.13 by lzn
**********************************************************************************/
void PrintfList(LNode *L)
{
LNode *temp = L;
int count = 0; //计数器
printf("List:\n");
while(temp->next)
{
temp = temp->next;
printf("%d\t",temp->data);
count++;
if(count%5==0) //每5个元素作为一行
{
printf("\n");
}
}
printf("\n");
}
//Menu
int MenuSelect(void)
{
int cmd;
printf("1.Serch test\n");
printf("2.Replace test\n");
printf("3.Insert test\n");
printf("4.Delete test\n");
printf("5.TailCreateList test\n");
printf("6.HeadCreateList test\n");
printf("7.Clear\n");
printf("8.Exit\n");
do
{
printf("Enter your choice: ");
scanf("%d",&cmd);
}while(cmd<0||cmd>8);
return cmd;
}
/* ====================================以下是测试函数============================== */
/*********************************************************************************
* Function Name : Test1, 测试"Serch"函数
* Parameter : L:链表
* Return Value : void
* Function Explain :
* Create Date : 2018.2.13 by lzn
**********************************************************************************/
void Test1(LNode *L)
{
int serchElem = 0; //存储要搜索的元素
printf("--------------------%s start!--------------------\n",__FUNCTION__);
PrintfList(L);
printf("Please input the element you want to serch:");
scanf("%d",&serchElem);
Serch(L,serchElem);
printf("--------------------%s end!--------------------\n",__FUNCTION__);
printf("\n");
}
/*********************************************************************************
* Function Name : Test2, 测试"Replace"函数
* Parameter : L:链表
* Return Value : void
* Function Explain :
* Create Date : 2018.2.13 by lzn
**********************************************************************************/
void Test2(LNode *L)
{
int replacePos = 0, replaceElem = 0; //存储替换的位置,替换的元素
printf("--------------------%s start!--------------------\n",__FUNCTION__);
PrintfList(L);
printf("Please input the position and the element you want replace(example:10,33):");
scanf("%d,%d",&replacePos,&replaceElem);
L = Replace(L,replacePos,replaceElem);
printf("After replace by position,list is:\n");
PrintfList(L);
printf("--------------------%s end!--------------------\n",__FUNCTION__);
printf("\n");
}
/*********************************************************************************
* Function Name : Test3, 测试"Insert"函数
* Parameter : L:链表
* Return Value : void
* Function Explain :
* Create Date : 2018.2.13 by lzn
**********************************************************************************/
void Test3(LNode *L)
{
int insertPos = 0, insertElem = 0; //存储插入的位置,插入的元素
printf("--------------------%s start!--------------------\n",__FUNCTION__);
PrintfList(L);
printf("Please input the position and the element you want insert(example:10,33):");
scanf("%d,%d",&insertPos,&insertElem);
L = Insert(L,insertPos,insertElem);
printf("After insert,list is:\n");
PrintfList(L);
printf("--------------------%s end!--------------------\n",__FUNCTION__);
printf("\n");
}
/*********************************************************************************
* Function Name : Test4, 测试"Delete"函数
* Parameter : L:链表
* Return Value : void
* Function Explain :
* Create Date : 2018.2.13 by lzn
**********************************************************************************/
void Test4(LNode *L)
{
int deletePos = 0; //存储要删除的位置
int elem = NULL;
printf("--------------------%s start!--------------------\n",__FUNCTION__);
PrintfList(L);
printf("Please input the position of the element you want to delete(example:10):");
scanf("%d",&deletePos);
L = Delete(L,deletePos,&elem);
printf("Delete node data is:%d\n",elem);
printf("After delete,list is:\n");
PrintfList(L);
printf("--------------------%s end!--------------------\n",__FUNCTION__);
printf("\n");
}
/*********************************************************************************
* Function Name : Test5, 测试"TailCreateList"函数
* Parameter : void
* Return Value : 初始化成功的链表
* Function Explain :
* Create Date : 2018.2.13 by lzn
**********************************************************************************/
LNode *Test5(void)
{
LNode *L;
int len = 0;
printf("--------------------%s start!--------------------\n",__FUNCTION__);
printf("Please input list length:");
scanf("%d",&len);
L = TailCreateList(len);
PrintfList(L);
printf("--------------------%s end!--------------------\n",__FUNCTION__);
printf("\n");
return (LNode*)L;
}
/*********************************************************************************
* Function Name : Test6, 测试"HeadCreateList"函数
* Parameter : void
* Return Value : 初始化成功的链表
* Function Explain :
* Create Date : 2018.2.13 by lzn
**********************************************************************************/
LNode *Test6(void)
{
LNode *L;
int len = 0;
printf("--------------------%s start!--------------------\n",__FUNCTION__);
printf("Please input list length:");
scanf("%d",&len);
L = HeadCreateList(len);
PrintfList(L);
printf("--------------------%s end!--------------------\n",__FUNCTION__);
printf("\n");
return (LNode*)L;
}
C++:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
#define Status int
#define ElemType int
//单链表结点数据结构
typedef struct LNode
{
ElemType data;//数据域
struct LNode *next;//指针域
}LNode,*LinkList;
//**************************基本操作函数***************************//
//初始化函数
Status InitList(LinkList &L)
{
L = new LNode;//生成头结点 这样删除等操作就不必分第一个结点和其他了
L->next = NULL;
return 1;
}
//获取单链表长度 头结点无数据,不算
int ListLength(LinkList L)
{
LinkList p=L;int sum=0;
while(p)
{
sum++;
p=p->next;
}
return sum-1;//去除头结点
}
//插入函数--后插法 插入到第i(1<=i<=length+1)个位置 即i-1之后 不必区分i的位置
bool ListInsert(LinkList &L,int i,ElemType e)
{
LNode* s;LinkList p=L;int j=0;
while(p&&(j<i-1))//j指到i-1位置或者p已经到最后时跳出
{
p=p->next;
++j;
}
if(!p||j>i-1)//i<1或者i>ListLength(L)+1时,插入位置无效 不调用ListLength,提高效率
{
printf("插入位置无效!!!\n");
return false;
}
s=new LNode;
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
//删除函数 删除位置i的结点 即删除i-1之后的结点
bool ListDelete(LinkList &L,int i)
{
LNode* s;LinkList p=L;int j=0;
LinkList q;
while(p&&(j<i-1))//j指到i-1位置
{
p=p->next;
++j;
}
if(!(p->next)||j>i-1)//i<1或者i>ListLength(L)时,删除位置无效
{
printf("删除位置无效!!!\n");
return false;
}
q=p->next;
p->next=q->next;
free(q);//释放空间
return true;
}
//查找函数 按值查找 查找第一个等于e的结点 成功返回该结点指针,否则返回NULL
LNode *LocateElem(LinkList L,ElemType e)
{
LNode *p=L;
while(p&&(p->data!=e))
{
p=p->next;
}
return p;
}
//**************************功能实现函数**************************//
//遍历输出函数
void PrintList(LinkList L)
{
LinkList p=L->next;//跳过头结点
if(ListLength(L))
{
printf("当前单链表所有元素:");
while(p)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
else
{
printf("当前单链表已空!\n");
}
}
//插入功能函数 调用ListInsert后插
void Insert(LinkList &L)
{
int place;ElemType e;bool flag;
printf("请输入要插入的位置(从1开始)及元素:\n");
scanf("%d%d",&place,&e);
flag=ListInsert(L,place,e);
if(flag)
{
printf("插入成功!!!\n");
PrintList(L);
}
}
//删除功能函数 调用ListDelete删除
void Delete(LinkList L)
{
int place;bool flag;
printf("请输入要删除的位置(从1开始):\n");
scanf("%d",&place);
flag=ListDelete(L,place);
if(flag)
{
printf("删除成功!!!\n");
PrintList(L);
}
}
//查找功能函数 调用LocateElem查找
void Search(LinkList L)
{
ElemType e;LNode *q;
printf("请输入要查找的值:\n");
scanf("%d",&e);
q=LocateElem(L,e);
if(q)
{
printf("找到该元素!\n");
}
else
printf("未找到该元素!\n");
}
//菜单
void menu()
{
printf("********1.后插 2.删除*********\n");
printf("********3.查找 4.输出*********\n");
printf("********5.退出 *********\n");
}
//主函数
int main()
{
LinkList L;int choice;
InitList(L);
while(1)
{
menu();
printf("请输入菜单序号:\n");
scanf("%d",&choice);
if(choice==5) break;
switch(choice)
{
case 1:Insert(L);break;
case 2:Delete(L);break;
case 3:Search(L);break;
case 4:PrintList(L);break;
default:printf("输入错误!!!\n");
}
}
return 0;
}
qq:1351006594