链表的操作

       链表的操作包括利用链表的插入运算建立线性链表,然后利用链表的查找、删除、计数、输出等运算反复实现链表的这 些操作(插入、删除、查找、计数、输出单独写成函数的形式。今天对链表的实现进行了代码的总结。
代码如下:
#include<stdio.h> 
#include<windows.h>
typedef struct Node  //节点的构造   
{
 int data;
 struct Node *PNext;
}NODE,*PNODE;
PNODE create_list(void)  //链表的创建
{
 int len;
 int i;
 int val;
 PNODE phead = (PNODE)malloc(sizeof(NODE));
 if (NULL == phead)
 {
  printf("创建链表失败!");
  exit(-1);
 }
 else
 {
  PNODE ptail = phead;
  phead->PNext = NULL;
  printf("请输入表的长度:");
  scanf("%d",&len);
  getchar();
  for (i = 0; i < len; i++)
  {
   PNODE p = (PNODE)malloc(sizeof(NODE));
   if (NULL == p)
   {
    printf("内存申请失败!");
    exit(-1);
   }
   else
   {
    printf("请输入链表的值:");
    scanf("%d",&val);
    getchar();
    p->data = val;
    ptail->PNext = p;
    p->PNext = NULL;
    ptail = p;
   }
   printf("\n");
  }
 }
 return phead;
}
PNODE Insert_list(PNODE phead, int pos, int val)//插入
{
 int i = 0;
 PNODE p= phead;
 while (p && (i < pos - 1))
 {
  p = p->PNext;
  i++;
 }
 if (!p || i > pos - 1)
 {
  printf("输入位置有误!");
  return 0;
 }
 PNODE q = (PNODE)malloc(sizeof(NODE));
 q->data = val;
 q->PNext = p->PNext;
 p->PNext = q;
 return phead;
}
PNODE Insert_list(PNODE phead, int i,int e)
{
 PNODE pre, s;
 int k;
 if (i <= 0)
  return ERROR;
 pre = phead;
 k = 0;
 while (pre != NULL&&k < i - 1)
 {
  pre = pre->PNext;
  k = k + 1;
 }
 if (!pre)
 {
  printf("插入位置不合理!");
  return ERROR;
 }
 s = (PNODE)malloc(sizeof(NODE));
 s->data = e;
 s->PNext = pre->PNext;
 pre->PNext = s;
 printf("插入成功!");
 return phead;
}
void delect_list(PNODE phead, int pos )
{
 int i = 0;
 PNODE p = phead,q;
 while ((NULL != p->PNext) && (i < pos - 1))
 {
  p = p->PNext;
  i++;
 }
 if (!(p->PNext))
 {
  printf("输入位置有误!");
  return ERROR;
 }
  q = p->PNext;
 p->PNext = q->PNext;
 free(q);
 q = NULL;//防止出现野指针
 printf("删除成功!");
}
int length_list(PNODE phead)//求 链表的长度
{
 int len = 0;
 PNODE p = phead->PNext;
 while (NULL != p)
 {
  len++;
  p = p->PNext;
 }
 return len;
}
void sord_list(PNODE phead)//链表排序
{
 int i, j;
 int temp;
 int len = length_list(phead);
 PNODE p=phead, q=phead;
 for (i = 1; i < len; i++)
 {
  q = p;
  p = p->PNext;
  for (j = i; j <= len; j++)
  {
   int temp;
   q = q->PNext;         
   if (p->data > q->data)  
   {
    temp = p->data;
    p->data = q->data;
    q->data = temp;
   }
  }
 }
 //for (i = 0, p = phead->PNext; i < len - 1; i++, p = p->PNext)
 //{
 // for (j = i + 12, q = p->PNext; j < len; j++, q = q->PNext)
 // {
 //  if (p->data > q->data)//交换数据
 //  {
 //   temp = p->data;
 //   p->data = q->data;
 //   q->data = temp;
 //  }
 // }
 //}
}
void list_traverse(PNODE phead)
{
 PNODE p;
 p = phead->PNext;
 printf("phead=");
 while (p)
 {
  printf("%d", p->data);
  p = p->PNext;
 }
 printf("\n");
}
void get_elem(PNODE phead, int pos)
{
 int j = 1;
 PNODE p = phead->PNext;
 while ((j != pos)&&(p->data != NULL))
 {
  j++;
  p = p->PNext;
 }
 if (j == pos)
 {
  printf("%d\n", p->data);
 }
 else
     printf("未找到!");
}
void menu()
{
 /*利用链表的插入运算建立线性链表,然后利用链表
  的查找、删除、计数、输出等运算反复实现链表的这
  些操作(插入、删除、查找、计数、输出单独写成函数的形式),
  并能在屏幕上输出操作前后的结果。画出搜索顺序示意图。*/
 printf("************************************************\n");
 printf("************************************************\n");
 printf("***********      1.   链表的插入         *******\n");
 printf("***********      2.   链表的删除         *******\n");
 printf("***********      3.   链表的查找         *******\n");
 printf("***********      4.   链表的计数         *******\n");
 printf("***********      5.   链表的排序         *******\n");
 printf("************************************************\n");
 printf("************************************************\n");
}
int main()
{
 int put,pos,val;
 PNODE phead;
 phead = create_list();
 list_traverse(phead);
 while (1)
 {
  menu();
  printf("请选择您需要的功能:>");
  scanf("%d", &put);
  getchar();
  switch (put)
  {
  case 1:
   printf("请输入插入的位置:>");
   scanf("%d", &pos);
   getchar();
   printf("请输入插入的值:>");
   scanf("%d", &val);
   getchar();
   phead=Insert_list(phead, pos,val);
   list_traverse(phead);
   break;
   case 2:
    printf("输入删除元素的位置:>");
    scanf("%d",&pos);
    getchar();
       delect_list(phead,pos);
    list_traverse(phead);
   break;
   case 3:
    printf("输入查找位置:>");
    scanf("%d",&pos);
    get_elem(phead, pos);
    break;
   case 4:
    printf("表长为:%d\n", length_list(phead));
       break;
   case 5:
    sord_list(phead);
    list_traverse(phead);
    break;
   default:
    printf("选择功能有误,请重新选择:\n");
  }
 }
 getchar();
 return 0;
}
阅读更多
换一批

没有更多推荐了,返回首页