单链表

数据结构C语言链表信息管理系统

链表的增添,查找和删除等功能!

头文件部分

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ERROR
#define OK

结构体部分

typedef struct
{
    char ID[30];      //存储ID
    char name[30];    //存储name
    char OP[30];      //存储职业
}ElemType;            //结构体类型名

typedef struct node
{
    ElemType data;              //存储ElemType的数据域
    struct node *next;          //指向ListNode结构体的指针
}ListNode;                      //结构体类型名
typedef ListNode *LinkList;            //结构体别名
LinkList head;                         //声明单链表的头节点
ListNode *p;   //声明指向单链表ListNode类型指针p

声明下列函数

int List_menu(void);                               //菜单函数
LinkList Create_List(void);                        //创建单链表函数
void ListNode_Insert(LinkList head,ListNode *p);   //插入节点函数
LinkList FindListNode_ID(LinkList head);           //查找ID节点函数
ListNode* FindListNode_Name(LinkList head);        //查找name节点函数
ListNode* FindListNode_Delete(void);               //删除查找函数
void printf_List(LinkList head);                   //输出函数
int Delete_ListNode(LinkList head);                //删除节点函数
void Destroy_List(ListNode *L);                    //销毁节点函数

主函数

 int main()
    {
    
  for( ;  ; )
        {
            switch(List_menu())
            {
            
                case 1:system("cls");
                       head=Create_List();
                       system("cls");
                       printf_List(head);
                       break;
    
                case 2:system("cls");
                       p = (ListNode *)malloc(sizeof(ListNode));
                       printf("Please enter member information\n");
                       printf("Input ID: ");
                       scanf("%s",p->data.ID);
                       printf("Input Name: ");
                       scanf("%s",p->data.name);
                       printf("Input Occupation: ");
                       scanf("%s",p->data.OP);
                       ListNode_Insert(head,p);
                       system("cls");
                       printf_List(head);
                       break;
    
                case 3:system("cls");
                      int xz;
                      printf("*********** Welcome to Find Function ***********\n");
                      printf("1.Find by ID:                  2.Find by Name: \n");
                      printf("please input: ");
                      scanf("%d",&xz);
                      if(xz==1)
                        {
                             system("cls");
                             p = FindListNode_ID(head);
                        }
                      if(xz==2)
                        {
                             system("cls");
                             p = FindListNode_Name(head);
                        }
    
                      if(p!=NULL)
                        {
                            printf("****************************************\n");
                            printf("ID: %s",p->data.ID);
                            printf("   Name: %s",p->data.name);
                            printf("   Occupation: %s\n",p->data.OP);
                            printf("****************************************\n\n\n");
                            p=p->next;
                        }
                       else
                        printf("Not found\n");
                        break;
    
                case 4: printf_List(head);
                        break;
    
                case 5:system("cls");
                       printf("********************* Welcome to Delete Function ******************\n");
                       Delete_ListNode(head);
                       printf_List(head);
                       break;
    
                case 6:exit(1);
                       break;
    
                case 7: Destroy_List(head);
            }//switch
        }//for
        }

菜单(menu)

   int List_menu(void)
{
    int sn;
    printf("********************* information system **********************\n");
    printf("****************                               ****************\n");
    printf("1.Input information                        2.Insert information\n");
    printf("3.Find Membership Information              4.Information List\n");
    printf("5.Delete member information                6.Exit System\n");
    printf("Input:1-6: ");
    scanf("%d",&sn);
    return sn;
}

创建链表(功能1)

LinkList Create_List(void)
{
    LinkList head =(ListNode *)malloc(sizeof(ListNode));    //创建单链表头结点
    head->next= NULL;                                       //头结点的下一个结点制空
	ListNode *rear;                                         //声明一个指向链表的尾部指针(ListNode类型的指针)
    rear = head;                                            //尾指针指向头指针
    int flag=0;
    while(flag==0)
    {
        p = (ListNode *)malloc(sizeof(ListNode));            //添加一个新的节点空间p
        printf("Please enter member information\n");         //下列输入信息进p节点
        printf("Input ID: ");
        scanf("%s",p->data.ID);
        printf("Input Name: ");
        scanf("%s",p->data.name);
        printf("Input Occupation: ");
        scanf("%s",p->data.OP);
        rear->next = p;                                      //尾指针的下一个节点为p
        rear = p;                                            //将尾指针移向p节点(read指向p)
        printf("Stop Creating List? (1:Stop  or  0:continun)\n");
        printf("please Input: ");
        scanf("%d",&flag);
    }
    rear->next=NULL;
    return head;                             //返回一个LinkList类型的单链表head

}

插入信息(功能2)

void ListNode_Insert(LinkList head,ListNode *p)
{
    ListNode *p1,*p2;                        //定义两个指向链表的指针p1,p2(指向ListNode结构体类型的指针)
    p1 = head->next;                         //p1指向head节点的下一个节点
    p2 = p1->next;                           //p2指向p1的下一个节点
    while(p2 && strcmp(p2->data.ID,p->data.ID)<0)   //p2和p1相比较不想等的条件
    {
        p1=p2;                                //p1指向p2
        p2= p2->next;                         //p2移向下一个结点
    }
    p->next = p1->next;                      //p1的下一个节点等于p的下一个节点
    p1->next = p;                            //p1的下一个节点等于p
    p->next = p2;                            //p的下一个节点等于p2
}

按照ID查找信息(功能3)

LinkList FindListNode_ID(LinkList head)
{
    ListNode *p;                           //定义一个新的局域指针p
    char ID[30];
    p = head->next;                        //将p指向head的下一个节点
    head->next=p;                          //head的下一个节点等于p
    printf("Please enter the ID you are looking for: ");
    scanf("%s",ID);
    while(p && strcmp(p->data.ID,ID)!=0)
        p=p->next;                           //p向后移
    if(p==NULL || strcmp(p->data.ID,ID)!=0)
        p = NULL;
        return p;                         //返回p所指的节点
}

按照name查找信息(功能3)

ListNode* FindListNode_Name(LinkList head)   //返回值为ListNode类型
{
    ListNode *p;                         //定义一个新的局域指针p
    char name[30];
    p = head->next;                      //将p指向head的下一个节点
    head->next=p;                        //head的下一个节点等于p
    printf("Please enter the Name you are looking for:  ");
    scanf("%s",name);
    while(p && strcmp(p->data.name,name)!=0)
        p=p->next;                          //p向后移
    if(p==NULL || strcmp(p->data.name,name)!=0)
        p = NULL;
        return p;                           //返回p所指的节点
}

删除的查找

ListNode* FindListNode_Delete(void)
{
    int An;
    ListNode *p;
    printf("1.Find deletion objects by ID      2.Find deletion objects by Name\n");
    printf("please input: ");
    scanf("%d",&An);
    if(An==1)
    {
        p=FindListNode_ID(head);
    }
    else if(An==2)
    {
        p=FindListNode_Name(head);
    }
        return p;
}

//输出显示(功能4)1
void printf_List(LinkList head)
{
    ListNode *p;
    p = head->next;
    if(p)
    {
        printf("\n");
        printf("********* Membership Information List **********\n\n");
    }
    else  printf("No record!\n");
    while(p)
    {
            printf("ID: %s",p->data.ID);
            printf("   Name: %s",p->data.name);
            printf("   Occupation: %s\n",p->data.OP);
            p=p->next;
    }
      printf("************************************************\n\n\n");
}

输出(功能4)

void printf_List(LinkList head)
{
     head=head->next;                  //指向head的下一个
     if(head)
    {
        printf("\n");
        printf("********* Membership Information List **********\n\n");
    }
    else  printf("No record!\n");
    while(head)
    {
            printf("ID: %s",head->data.ID);
            printf("   Name: %s",head->data.name);
            printf("   Occupation: %s\n",head->data.OP);
            head=head->next;           //指向head的下一个
    }
      printf("************************************************\n\n\n");
}

删除(功能5)

int Delete_ListNode(LinkList head)
{
   char ch;
   ListNode *p,*q;
   p = FindListNode_Delete();
   if(p==NULL)
   {
       printf("Not found!\n");
   }
   fflush(stdin);
   printf("Delete it?  Y or N\n");
   printf("please input: ");
   scanf("%c",&ch);
   if(ch=='y' || ch=='Y')
   {
      q=head;
      while(q && q->next!=p)
        q = q->next;
        q->next = p->next;
        free(p);
        printf("It has been deleted!\n");
   }
   return 0;
}

销毁函数

void Destroy_List(ListNode *L)
{
    LinkList p;
    while(L)
    {
        p=L;
        L=L->next;
        free(p);
    }
    return OK;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值