0
点赞
收藏
分享

微信扫一扫

pta单链表的插入和删除

他说Python 2022-01-20 阅读 56
#include <stdio.h>
#include <malloc.h>
typedef struct node
{
     int data;
     struct node *next;
}Snode,*ptr;

void insert(ptr h,int x)//将x插入在表头位置,即监督元之后
{
    ptr p;
    p=(ptr)malloc(sizeof(Snode));
    p->data=x;
    p->next=h->next;
    h->next=p;
}

void output(ptr h)//输出链表中的元素,即输出除监督元以外的所有元素
{
    ptr p;   
    p=h->next;
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}

void del(ptr h)//删除表头元素,即监督元后第一个元素结点
{
    ptr p;   
p=h->next;    
h->next=p->next;
    free(p);
}    

int main()
{
    ptr head;
    int x;
    head=(ptr)malloc(sizeof(Snode));
    head->next=NULL;
    scanf("%d",&x);
    while(x!=0)
    {
        insert(head,x);
        scanf("%d",&x);
    }
    output(head);
    del(head);
    output(head);
    return 0; 
}
举报

相关推荐

0 条评论