单链表的基本操作

来源:互联网 发布:fifo算法c语言实现 编辑:程序博客网 时间:2024/06/03 22:43
#include<stdio.h>#include<stdlib.h> #define OVERFLOW 1#define OK 1#define ERROR 0#define LEN sizeof(struct LNode)typedef  int ElemType;typedef  int Status;typedef  struct LNode{    ElemType data;//数据域     struct  LNode  *next;//指针域}LNode;struct LNode *Init_L()  //建立空链表{    struct LNode *head;    struct LNode *p,*q;    head= (struct LNode*)malloc(LEN);    if(!head) exit(0);//分配内存失败    head->data=0;    head->next=NULL;    return(head);}struct LNode *creat(struct LNode *head)//输入元素 {    LNode *p,*q;    int a;//输入元素个数     printf("请输入要创建的元素个数:\t");    scanf("%d",&a);    getchar();    int i;    for(i=0;i<a;i++)    {       p=(struct LNode*)malloc(sizeof(LNode));       if(!p) exit(0);//分配内存失败       scanf("%d",&p->data);       p->next=NULL;       if(i==0){         q=p;         head->next=p;       }       else       {         q->next=p;        }         q=p;//q指向尾结点         q->next=NULL;    }    return (head);}Status Destory_L(struct LNode *head)//销毁链表{    struct LNode *p;    if(!head) return OK;    while(head)    {        p=head->next;        free(head);        head=p;    }    printf("完成链式表的销毁\n");    return OK;} struct LNode *Clear_L(struct LNode *head)//清空链表{    head->next=NULL;    return head;}void ListEmpty(struct LNode *head)//验证空表{    if(head->next==NULL)    printf("是空表\n");    else    printf("不是空表\n"); }void ListLength(struct LNode *head)//返回元素个数{    struct LNode *p;    int i=0;    p=head->next;    while(p)    {        i++;         p=p->next;    }    printf("元素个数为%d个",i);} struct LNode *Insert_L(struct LNode *head,int i,ElemType e)//在第i个元素之前插入e {    struct LNode *p,*q;    q=(struct LNode*)malloc(LEN);//创建插入结点     if(!q) exit(0);//创建失败    q->data=e;    p=head->next;    int j=1;    while((p!=NULL)&&(j<i-1))    {        p=p->next;        j++;     }     q->next=p->next;     p->next=q;     return(head);}struct LNode *Delete_L(struct LNode *head,int i)//删除第i个元素 {    struct LNode *p,*q;    ElemType e;    p=head->next;    int j=1;    while((p!=NULL)&&(j<i-1))    {        p=p->next;        j++;    }    q=p->next;    p->next=q->next;    e=q->data;    free(q);    return(head);    } struct LNode *Inverse_L(struct LNode *head)//就地逆置 {    if(head->next==NULL) return ERROR;    struct LNode *p,*q,*r;    p=head->next;    q=r=NULL;    while(p)    {        q=p->next;        p->next=r;        r=p;//r用来保存第一个结点(p=head->next)         p=q;//q指向待操作结点     }    head->next=r;    return head; }Status print(struct LNode *head){    int i;    LNode *p;    if(!head) return ERROR;    p=head->next;    while(p!=NULL)    {        printf("%d\t",p->data);        p=p->next;    }     printf("\n") ;     return OK;} int main(){    struct LNode *head;    int a=1;    while(a)     {     printf("\n请输入要进行的操作:\n1.建立\t2.插入\t3.删除\t4.打印\t5.销毁\t6.清空\t7.验证空表\t8.元素个数\t9.逆置");    int k;    scanf("%d",&k);    switch(k)     {        case 1:               head=Init_L();               creat(head);                break;        case 2:               int i;ElemType e;               printf("请输入插入位置及元素(空格隔开)");               scanf("%d%d",&i,&e);               Insert_L(head,i,e);               print(head);               break;        case 3:               int j;               printf("删除第j个元素:\n");               scanf("%d",&j);                Delete_L(head,j);               print(head);               break;        case 4:print(head);               break;        case 5:Destory_L(head);               break;        case 6:Clear_L(head);               break;        case 7:ListEmpty(head);               break;        case 8:ListLength(head);               break;        case 9:Inverse_L(head);               break;          default:printf("输入的数字不正确\n");               break;         }    printf("\n是否还要进行操作?(选择是按 1,选择否按 0):");    int i;    scanf("%d",&i);    if(i==1)    a=0;    }//while     return 0;}
原创粉丝点击