线性表的链式表示和实现(插入删除建空合并)

题解:后续的功能会不定期更新

emmm框架:

代码:

#include <iostream>
#include <cstdio>
using namespace std;
typedef struct LNode
{
     int data;
     struct LNode *next;
}LNode,*LinkList;

int GetElem_L(LinkList L, int i, int &e)
{
    LNode *p = L->next;
    int j = 1;
    while (p&&j < i)
    {
        p = p->next;
        ++j;
    }
    if (!p || j>i)
        return 0;
    e = p->data;
    return 1;
}
int ListInsert_L(LinkList &L, int i, int e)
{
     LNode *p = L->next;
     int j = 1;
     while (p && j < i-1)
     {
         p = p->next;
         ++j;
     }
     if (!p || j>i-1)
         return 0;
     LNode *node =(LNode*)malloc(sizeof(LNode));
     node->data = e;
     LNode* q = p->next;
     p->next = node;
     node->next = q;
     return 1;
}
int ListDelete_L(LinkList &L, int i, int &e)
{
     LNode *p = L->next;
     int j = 1;
     while (p->next&&j < i - 1)//注意此处为p->next,因为若是p,则出来的p可能为空
     {
         p = p->next;
         ++j;
     }
     if (!p->next || j>i - 1)
         return 0;
     LNode*q= p->next;
     e = q->data;
     p->next = p->next->next;
     free(q);
     return 1;
}
void CreateList_L(LinkList &L, int n)
{
     printf("Enter the value of the node:");
   // L = (LinkList)malloc(n*sizeof(LNode)); 如果像这样创建的话,
     //那就是生成连续存储空间的线性表,应该单独对每一个节点分配内存空间
     L = (LinkList)malloc(sizeof(LNode));
     L->next = nullptr;//先生成一个表头的单链表
     for (int i = n;i > 0;--i)
     {
          LNode *p = (LinkList)malloc(sizeof(LNode));
          cin>>p->data;
          p->next = L->next;//插入方式为向表头的后一个插入,不然插在表尾太麻烦
          L->next = p;
     }
}
void MergeList_L(LinkList &L,LinkList &Lb,LinkList &Lc)//合并
{
    LNode *p=L->next;
    LNode *pb=Lb->next;
    LNode *pc=Lc=L;
    while(p&&pb)
    {
        if(p->data<=pb->data)
        {
            pc->next=p;
            pc=p;
            p=p->next;
        }
        else
        {
            pc->next=pb;
            pc=pb;
            pb=pb->next;
        }
    }
    pc->next=p?p:pb;
    free(Lb);
}
/*
int Length_L(LinkList &L)//计算长度
{
    int j=0;
    LNode *p=L->next;
    while(p)
    {
        p=p->next;
        j++;
    }
    return j;
}
*/  //没用到emmmm
void ShowList(LinkList &L)
{
      LNode *p = L->next;
      while (p)
      {
          cout<<p->data<<" ";
          p = p->next;
      }
}
int main()
{
     LinkList L;
     cout<<"Enter the length of the linked list:";
     int num;
     cin>>num;
     CreateList_L(L, num);//建表
     ShowList(L);//展示
     int e1,temp;

     cout<<"
To increase the number of positions:";
     int place;
     cin>>place;
     cout<<"
Enter the number to insert:";
     cin>>e1;
     ListInsert_L(L, place, e1);
     ShowList(L);

     cout<<"
The number of digits to be deleted:";
     int place1;
     cin>>place1;
     ListDelete_L(L, place1, temp);
     ShowList(L);
     printf("
The deleted node value is :%d
", temp);

     LinkList Lb,Lc;
     cout<<"
Enter the length of the linked list:";
     int num1;
     cin>>num1;
     CreateList_L(Lb, num1);//建表
     cout<<"
The two linked lists are:";
     MergeList_L(L,Lb,Lc);
     ShowList(L);
     cout<<endl;
    return 0;
}

今天也是元气满满的一天!good luck!

原文地址:https://www.cnblogs.com/cattree/p/7501881.html