链式结构实现线性表的基本操作

来源:互联网 发布:js div 位置 编辑:程序博客网 时间:2024/05/16 15:01
<pre name="code" class="cpp">#include <stdio.h>#include <stdlib.h>typedef int Elementype;typedef struct Node {    Elementype data;    struct Node *next;}List;List * Init_ListEnd(List *ptr)    ///尾插法建立单链表{    int num;    ptr=(List*)malloc(sizeof(List));     ptr->next=NULL;     List *p,*s;     p=ptr;    scanf ("%d",&num);    while (num!=10)       ///输入链表数据,当num=10时输入结束    {        s=(List*)malloc(sizeof(List));        s->data=num;        p->next=s;        p=s;        scanf("%d",&num);        p->next=NULL;    }    return ptr;}List* Init_ListHead(List *ptr)       ///头插法建立单链表{    int num;ptr=(List*)malloc(sizeof(List));  ///建立头结点ptr->next=NULL;scanf ("%d",&num);while (num!=10)    {       List*p=(List*)malloc(sizeof(List));       p->data=num;       p->next=ptr->next;       ptr->next=p;       scanf ("%d",&num);    }    return ptr;}int lengthList ( List *ptr)   ///求单链表表长{    int len=0;    List *p=ptr->next;    while (p)    {        p=p->next;        len++;    }    return len;}void Printf_List (List *ptr)   ///遍历链表{    List *p=ptr->next;    while (p!=NULL)    {        printf ("%d ",p->data);        p=p->next;    }    printf ("\n");}List *Find_posi (int i,List *ptr)  ///按位查找{    int j=1;    List *p=ptr;    while (p!=NULL && j<i)    {        p=p->next;        j++;    }    if  (j==i)    printf("%d\n",p->data);    else        printf ("Not found\n");}List *Find_elem(int x,List *ptr)   ///按值查找{    List *p;    p=ptr->next;    while (p && p->data!=x)    {        p=p->next;    }    printf ("%d\n",p->data);}List *Insert(Elementype x,int i,List *ptr)   ///插入新元素    {        List *p=ptr;        List *s;        int j=0;        while (p && j<i-1)        {            p=p->next;            ++j;        }        if (!p || j>i-1)        {            return;        }        s=(List*)malloc(sizeof(List));        s->data=x;        s->next=p->next;        p->next=s;        return 1;    }List *Delete (int i,List *ptr) ///删除元素    {        List *p=ptr;        List *q;        int x;        int j=0;        while (p->next && j<i-1)        {            p=p->next;            ++j;        }        if (p->next==NULL || j>i-1)            printf ("NOT Found!\n");            q=p->next;            p->next=q->next;            x=q->data;            printf ("%d\n",x);            free(q);            return 0;    }int main (){    List *ptr,*p;    printf ("Please input the elements of list (endinput when you input '10')\n");    p=Init_ListEnd(ptr);    Printf_List(p);    int n;    while (1)    {        int m;        printf ("please choose the operate\n1:Find element\n2:Insert new element\n3:Delete element\n");        scanf ("%d",&m);        switch (m)        {      case 1:        {           printf ("Please input position of the element you want to find\n");    scanf ("%d",&n);      Find_posi(n+1,p);        }break;      case 2:        {            int i,h;            printf ("Please input the position and the element you want to insert\n");            scanf ("%d%d",&i,&h);            Insert (h,i,p);            Printf_List(p);        }break;      case 3:        {            int w;            printf ("Please input the element you want to delete\n");            scanf ("%d",&w);            Delete(w,p);            Printf_List(p);        }break;        }    }    return 0;}



0 0
原创粉丝点击