天天看点

线性表链式存储结构下基本操作的实现(初始化、赋值、取值、插入、删除、归并等)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define len sizeof(struct list)
struct list
{
  int data;
  struct list *next;
};
  struct list *la,*lb;
//初始化链表 
struct list * InitList()
{
  struct list *head;
  head=(struct list *)malloc(len);
  return head;
}
//为链表赋值 
struct list * ListInsert(struct list* &la)
{
  struct list *p1,*head;
  int n;
  head=la;
  printf("请输入数据的个数\n");
  scanf("%d",&n);
  printf("请输入数据,用空格分开\n");
  while(n--)
  {
    p1=(struct list*)malloc(len);
    scanf("%d",&p1->data);
    p1->next=NULL;
    la->next=p1;
    la=p1;
  }
  return head;
}
//向链表中取元素 
void GetElem(struct list * la)
{
  printf("请输入你要取得值的位置\n");
  int n;
  scanf("%d",&n);
  while(la->next&&n)
  {
    n--;
    la=la->next;
  }
  if(n)
  printf("输入的位置超出链表范围\n");
  else
  printf("所处位置的元素为%d\n",la->data);
}
//向链表指定位置插入元素 
void InsertElem(struct list * la)
{
  struct list* pos;
  printf("请输入你要插入的位置和元素\n");
  int n,x;
  scanf("%d %d",&n,&x);
  while(la->next&&n)
  {
    n--;
    pos=la;
    la=la->next;
  }
  if(n)
  printf("输入的位置超出链表范围\n");
  else
  {
    struct list * p1;
    p1=(struct list*)malloc(len);
    p1->data=x;
    p1->next=pos->next;
    pos->next=p1; 
    printf("插入成功\n");
  } 
}
//合并链表 
bool UnionList(struct list *pa,struct list *pb)
{
  struct list *head;
  head=pa;
  while(pa->next){
    pa=pa->next;
  }
  pa->next=pb->next;
  printf("合并后的链表为:\n"); 
  while(head->next){
    printf("%d ",head->next->data);
    head=head->next;
  }
  printf("\n");
}
int main(){
  while(1){
    printf("************************************************************\n");
    printf("*             请输入你要实现的功能前的序号                 *\n");
    printf("*             1 初始化链表                                 *\n");
    printf("*             2 对链表进行赋值                             *\n");
    printf("*             3 向链表中取值                               *\n");
    printf("*             4 向链表中插入元素                           *\n");
    printf("*             5 归并链表                                   *\n"); 
    printf("*             6 退出程序                                   *\n");
    printf("************************************************************\n");
    int x;
    scanf("%d",&x);
    if(x==1){
      printf("请输入你要初始化的链表a,b\n\n");
      getchar();
      char ch;
      ch=getchar();
      if(ch=='a'){
        la=InitList();
        if(!la)
          printf("初始化失败\n\n"); 
        else{
          la->next=NULL;
          printf("初始化成功\n\n"); 
        }
      }
      else if(ch=='b'){
        lb=InitList();
        if(!lb)
          printf("初始化失败\n\n"); 
        else{
          lb->next=NULL;
          printf("初始化成功\n\n"); 
        }
      }
      else
        printf("输入错误\n\n");
    }
    else if(x==2){
      printf("请输入你要进行赋值的链表a或者b\n\n");
      getchar();
      char ch=getchar();
      if(ch=='a') {
        if(!la)
        printf("请先对链表a进行初始化\n\n");
        else
        la=ListInsert(la); 
      }
      else if(ch=='b'){
        if(!lb)
        printf("请先对链表b进行初始化\n\n");
        else
        lb=ListInsert(lb);
      }
    }
    else if(x==3){
      printf("请输入你要进行取值的链表a或者b\n\n");
      getchar();
      char ch=getchar();
      if(ch=='a') {
        if(!la)
        printf("请先对链表a进行初始化\n\n");
        else
        GetElem(la); 
      }
      else if(ch=='b'){
        if(!lb)
        printf("请先对链表b进行初始化\n\n");
        else
        GetElem(lb);
      }
    }
    else if(x==4){
      printf("请输入你要进行插入的链表a或者b\n\n");
      getchar();
      char ch=getchar();
      if(ch=='a') {
        if(!la)
        printf("请先对链表a进行初始化\n\n");
        else
        InsertElem(la); 
      }
      else if(ch=='b'){
        if(!lb)
        printf("请先对链表b进行初始化\n\n");
        else
        InsertElem(lb);
      }
    } 
    else if(x==5){
      if(!la){
        printf("请先初始化链表a\n");
        break;
      }
      if(!la){
        printf("请先初始化链表b\n");
        break;
      }
      UnionList(la,lb);
    } 
    else
    break;
  }
  return 0;
}