回 帖 发 新 帖 刷新版面

主题:[b]原创]链表操作精华版,数据结构初学者的首选[/b]

//大家好,近来看到很多关于询问如何建立链表,和链表的操作的程序,我自己
//做了一个,觉得还可以,发出来给大家参考参考.
//请大家多和我联系.QQ:393870322
//email:lyt8604@sohu.com
//linklist.cpp VC++6.0下运行

#include <iostream>
using namespace std;
typedef int state;
#define overflow -1;
#define ERROR 0;
#define OK 1;

void Menu()   //菜单 
{
     cout<<"         线 性 表 系 统\n";
     cout<<"********************************\n";
     cout<<"*       1-----建      表       *\n";
     cout<<"*       2-----插      入       *\n";
     cout<<"*       3-----删      除       *\n";
     cout<<"*       4-----显      示       *\n";
     cout<<"*       5-----查      找       *\n";
     cout<<"*       6-----求  表  长       *\n";
     cout<<"*       0-----返      回       *\n";
     cout<<"********************************\n";
     cout<<"      请选择菜单号(0--6):";
}

typedef struct Node
{
       int num;
       struct Node *next;
}Lnode,*LinkList;

int initlist(LinkList &L)//置空表
{
    L=new Lnode;
    if(!L)
        return overflow;
    L->next=NULL;
    return OK;
}

void Creat_LinkList(LinkList &L)   //创建 

         Lnode *s,*r;   
         int x;
         r=L;
         cout<<"请输入表的数据以0结束: ";
         cin>>x;
         while (x!=0)       
         {
               s=new Lnode;   
               s->num=x;
               if (L==NULL)
               {
                   s->next=L->next;
                   L->next=s;
               }
               else
               {
                   while(r->next!=NULL)
                      r=r->next;                   
                   s->next=r->next;
                   r->next=s;
               }
               cin>>x;
         }
}

void Show_LinkList(LinkList L)  //显示 
{
    Lnode *p;
    p=L->next;
    if (p==NULL)
        cout<<"空表";
    else
      {
          cout<<"单链表为:";         
          while (p!=NULL)
            {
                cout<<p->num<<"->";
                p=p->next;
            }
          cout<<"\n";
      }
}

Lnode *Get_LinkList(LinkList L,int i)  //取元 
{
      Lnode *p;
      int j=0;
      p=L;
      while (p->next!=NULL && j<i)
        {
            p=p->next;
            j++;
        }
      if (j==i)
        return p;
      else
        return NULL;
}         
int Insert_LinkList(LinkList &L,int i,int x) //插入 
{
  LinkList p,s;
  p = L;   
  int j = 0;
  while (p && j < i-1) {  
    p = p->next;
    ++j;
  } 
  if (!p || j > i-1) return ERROR;    
  s = new Lnode;
  s->num = x;  s->next = p->next;     
  p->next = s;
  return OK;    
}
//下面还有

回复列表 (共4个回复)

沙发

int Delete_LinkList(LinkList &L,int i)   //删除 
{
    Lnode *p,*s;
    p=Get_LinkList(L,i-1);
    if (p==NULL)
      {
          cout<<"第"<<i-1<<"个结点不存在"<<endl;
          return -1;
      }
    else
      if (p->next==NULL)
        {    
             cout<<"第"<<i-1<<"个结点不存在"<<endl;
             return 0;
        }
      else
        {
             s=p->next;
             p->next=s->next;
             delete s;
             return OK;
        }
}

Lnode *Locate_LinkList(LinkList L,int x)   //查找 
{
      Lnode *p;
      p=L;
      p=p->next;
      while (p!=NULL && p->num!=x)
            p=p->next;
      return p;
}

int Length_LinkList(LinkList L)          //求表长 
{
    Lnode *p;
    int j;
    p=L;
    j=0;
    while (p->next)
      {
          p=p->next;
          j++;
      }
      return j;
}

void main()
{
    LinkList L,p;
    int n,flag=1,j,x;
    initlist(L);
    while(1)
    {
     Menu();
     cin>>n;
     switch(n)
     {
     case 1:
            Creat_LinkList(L);
            flag=0;
            break;
     case 2:
         if(flag==0)
         {
         cout<<"请输入插入的位置: ";
         cin>>j;
         cout<<"请输入插入的元素: ";
         cin>>x;
         Insert_LinkList(L,j,x);
         break;
         }
         else
         {
             cout<<" 请先建表\n";
             break;
         }
     case 3:
         if(flag==0)
         {
         cout<<"请输入删除的位置: ";
         cin>>j;
         Delete_LinkList(L,j);
         break;
         }
         else
         {
             cout<<" 请先建表\n";
             break;
         }
     case 4:
         if(flag==0)
         {
         Show_LinkList(L);
         break;
         }
         else
         {
             cout<<" 请先建表\n";
             break;
         }
     case 5:
         if(flag==0)
         {
         cout<<"请输入查找的元素: ";
         cin>>x;
         p=Locate_LinkList(L,x);
         if(p!=NULL)
            cout<<"找到的元素为: "<<p->num<<endl;
         else
             cout<<"表为空或查找不成功\n";
         break;
         }
         else
         {
             cout<<" 请先建表\n";
             break;
         }
     case 6:
         if(flag==0)
         {
         cout<<"表长为: "<<Length_LinkList(L)<<endl;
         break;
         }
         else
         {
             cout<<" 请先建表\n";
             break;
         }
     case 0:
         return;
     }
    }
}

板凳

你真棒!!!

3 楼


good!!!!!!!!!!!1

4 楼

参考一下,呵呵^0^

我来回复

您尚未登录,请登录后再回复。点此登录或注册