数据结构单链表的基本操作

#include "head.h"
//#include "com_h.H"
#include<stdio.h>
#include<iostream>
int main()
{
    char str;
    int i,n; 
    int k;
    LinkList L;
    menu1();
    cout<<"请选择建立方式:"<<endl;
    cin>>k;
    cout<<"请先输入数据个数:"<<endl;
    cin>>n;
    cout<<"请输入你的数字:"<<endl ; 
    if(k==1)
    {
    	
    	CreateList_R(L,n);
	}
	else
	{
	
		 CreateList_H(L,n);
	}
    ElemType e;
    menu();
    
    while(1)
    {
        cout<<"请输入操作代码:";
        cin>>str;
        if(str=='a')
        {
            cout<<"链表已经被清空!"<<endl;
            ClearList(L);//调用操作函数
            

        }
        else if (str=='b')
        {
            if(ListEmpty(L))
                cout<<"链表为空!"<<endl;
            else
                cout<<"线性表不为空!"<<endl;
                

        }
        else if (str=='c')
        {
            cout<<"链表的长度为:"<<ListLength(L)<<endl;
            

        }
        else if (str=='d')
        {
            cout<<"请输入指定的位置:"<<endl;
            cin>>i;
            if(GetElem(L,i,e) == 1) cout<<"这个位置的数据是:"<<e<<endl;
            else cout <<"error!"<<endl;
            
        }
        else if (str=='e')
        {
            int n;
            cout<<"请输入你想查找哪个元素的前驱:"<<endl;
            cin>>n;
            if(ListPrior(L,n,&e) == 1) cout<<n<<"的前驱为:"<<e<<endl;
                else cout<<"error"<<endl;
                
        }
        else if (str=='f')
        {
            int n;
            cout<<"请输入你想查找哪个元素的后继:"<<endl;
            cin>>n;
            if(ListNext(L,n,&e)==1) cout<<n<<"的后继为:"<<e<<endl;
                else cout<<"error"<<endl;
               
        }
        else if (str=='g')
        {
            cout<<"请输入插入元素及其位置:"<<endl;
            cin>>e>>i;
            if(ListInsert(L,i,e)==ERROR) cout<<"您的输入不合法"<<endl;
            

        }
        else if (str=='h')
        {
            cout<<"请输入你想要删除哪个位置的元素:"<<endl;
            cin>>i;
            if(ListDelete(L,i)==ERROR)  cout<<"error"<<endl;
          

        } 
        else if (str=='i')
        {
            cout<<"整个链表输出的元素为:"<<endl;
            DisplayList(L);
           

        }
         else if (str=='j')
         {
         	cout<<"请输入查找的内容e的值:"<<endl;
         	cin>>e;
         	if(LocateElem(L,e)==ERROR)
         	{
         		cout<<"不存在!"<<endl; 
			 }
			 else
			 {
			 	cout<<"元素"<<e<<"在链表中的位置为"<<LocateElem(L,e)<<endl;
			 }
		 }
        else if (str=='0')
        {
            break;
        }
        else
        {
            cout<<"\n操作码错误!!!"<<endl;
            menu();
        }
    }
    //调用销毁线性表函数,如Destroy_List(L);
    DestroyList(L);
    return 0;
	}
#include <stdlib.h>
#include "head.h"
//初始化链表 
Status InitList(LinkList &L)
{
    L=new LNode;//头结点 
    L->next=NULL;
    return OK;
}
//销毁
Status DestroyList(LinkList &L)
{
       LinkList p;
       while(L)
        {
            p=L;
            L=L->next;
            delete p;
        }
       return OK;
 }
//清空
Status ClearList(LinkList L)
{// 将L重置为空表
   LinkList p,q;
   p=L->next;   //p指向第一个结点
   while(p)       //没到表尾
       {q=p->next;
	    delete p;
		 p=q;
		}
   L->next=NULL;   //头结点指针域为空
   return OK;
 }
//求长度
int  ListLength(LinkList L)
{  //返回L中数据元素个数
    LinkList p=L->next; //p指向第一个结点
    int count=0;
    while(p){//遍历单链表,统计结点数
        ++count;
        p=p->next;
    }
    return count;
 }
//判断是否为空
bool ListEmpty(LinkList L)
{//若L为空表,则返回true,否则返回false
   if(L->next==NULL)
       return true;
   else
       return false;
 }
//按照位置取出元素 
Status GetElem(LinkList L,int i,ElemType &e)
{
    LinkList p=L->next;
    int j=1;
    while(p&&j<i)
    {
        p=p->next;
        ++j;
    }
    if(!p||j>i) return ERROR;
    e=p->data;
    return OK;
}
//按值查找位置 
Status LocateElem(LinkList L,ElemType e){
	LinkList p=L->next;
	int j=1;
	while(p&&p->data!=e){
		p=p->next;
		j++;
	}
	if(!p)
		return ERROR;
	return j;	
}

//插入
Status ListInsert(LinkList &L,int i,ElemType e)
{
    LinkList p=L;
    int j=0;
    while(p && (j<i-1))
    {
        p=p->next;
        ++j;
    }
    if(!p||j>i-1) return ERROR;
    LinkList s=new LNode;
    s->data=e;
    s->next=p->next;
    p->next=s;
    return OK;
}
//删除
Status ListDelete(LinkList &L,int i)
{
    LinkList p=L;
    int j=0;
    while((p->next)&&(j<i-1))
    {
        p=p->next;
        ++j;
    }
    if(!(p->next)||(j>i-1)) return ERROR;
    LinkList q=p->next;
    p->next=q->next;
    delete q;
    return OK;
}
//尾插法创建单链表
void CreateList_H(LinkList &L,int n)
{
    L=new LNode;
    L->next=NULL;
    for(int i=0; i<n; ++i)
    {
        LinkList p=new LNode;
        cin>>p->data;
        p->next=L->next;
        L->next=p;
    }
}
//头插法创建单链表
void CreateList_R(LinkList &L,int n)
{
    L=new LNode;
    L->next=NULL;
    LinkList r=L;
    for(int i=0; i<n; ++i)
    {
        LinkList p=new LNode;
        cin>>p->data;
        p->next=NULL;
        r->next=p;
        r=p;
    }
}
//查找哪个元素的前驱
Status ListPrior(LinkList L,ElemType cur_e,ElemType *pre_e)
{
	LinkList q=L->next;//第一个结点
	if(!q)//若链表为空
	    return ERROR;
	LinkList p=q->next;//第二个结点
	while(p)
	{
		if(p->data==cur_e)
		{
			*pre_e=q->data;
			return OK;
		}
		else
		{
			q=p;
			p=p->next;
		}
	}
    return ERROR;
}
//查找哪个元素的后继
Status ListNext(LinkList L,ElemType cur_e,ElemType *next_e)
{
	LinkList p=L->next;
	while(p)
	{
		if(p->data==cur_e&&p->next)
		{
			*next_e=p->next->data;
			return OK;
		}
		else
		    p=p->next;
	}
	return ERROR;
}
//显示链表
void DisplayList(LinkList L)
{
    LinkList p=L->next;
    while(p)
    {
        cout<<p->data<<"  ";
        p=p->next;
    }
    cout<<endl;
    return;
}
void menu()
{
    cout<<"\t\t\t******** 请选择您要进行的操作 *********\t\t"<<endl;
    cout<<"\t\t\t*                                     *\t\t"<<endl;
    cout<<"\t\t\t*        a----清空线性表              *\t\t"<<endl;
    cout<<"\t\t\t*        b----判断线性表是否为空      *\t\t"<<endl;
    cout<<"\t\t\t*        c----求线性表长度            *\t\t"<<endl;
    cout<<"\t\t\t*        d----获取线性表指定位置元素  *\t\t"<<endl;
    cout<<"\t\t\t*        e----求前驱                  *\t\t"<<endl;
    cout<<"\t\t\t*        f----求后继                  *\t\t"<<endl;
    cout<<"\t\t\t*        g----在线性表指定位置插入元素*\t\t"<<endl;
    cout<<"\t\t\t*        h----删除线性表指定位置元素  *\t\t"<<endl;
    cout<<"\t\t\t*        i----显示线性表              *\t\t"<<endl;
    cout<<"\t\t\t*        j----查找元素位置            *\t\t"<<endl;
    cout<<"\t\t\t*        0----退出                    *\t\t"<<endl;
    cout<<"\t\t\t***************************************\t\t"<<endl; 

}
void menu1()
{
	cout<<"\t\t==============选择建立链表的方式==================\t\t"<<endl;
	cout<<"\t\t                                                  \t\t"<<endl;
	cout<<"\t\t             1.头插法                             \t\t"<<endl;
	cout<<"\t\t             2.尾插法                             \t\t"<<endl;
	cout<<"\t\t==================================================\t\t"<<endl; 
 } 
#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef int Status;

//单链表的存储结构
typedef struct LNode
{
    ElemType data;
    struct LNode *next;
} LNode,*LinkList;
extern Status InitList(LinkList &L);
extern Status DestroyList(LinkList &L);
extern Status ClearList(LinkList L);
extern int  ListLength(LinkList L);
extern bool ListEmpty(LinkList L);
extern Status GetElem(LinkList L,int i,ElemType &e);
extern Status LocateElem(LinkList L,int e);
extern Status ListInsert(LinkList &L,int i,ElemType e);
extern Status ListDelete(LinkList &L,int i);
extern void CreateList_H(LinkList &L,int n);
extern void CreateList_R(LinkList &L,int n);
extern Status ListPrior(LinkList L,ElemType cur_e,ElemType *pre_e);
extern Status ListNext(LinkList L,ElemType cur_e,ElemType *next_e);
extern void DisplayList(LinkList L);
extern void menu();//菜单函数
extern void menu1(); 

这是单链表的基本操作。包括单链表的存储结构。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值