#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;
}
pta单链表的插入和删除
最新推荐文章于 2024-06-20 11:47:52 发布