#include<stdio.h>
#include<stdlib.h>
#define N 5
typedef struct node{
int data;
struct node * next;
}ElemSN;
ElemSN * Greatlink(int a[],int n)
{
ElemSN * h,*p,*t;
int i;
h=NULL;
for(i=0;i<N;i++)
{
p=(ElemSN *)(malloc(sizeof(ElemSN)));
p->data=a[i];
if(!h)
{
p->next=p;
h=t=p;
}
else
{
p->next=h;
t=t->next=p;
}
}
return h;
}
void PrintLink(ElemSN *h)
{
ElemSN *p;
p=h;
printf("%5d",p->data);
for(p=h->next;p!=h;p=p->next)
printf("%5d",p->data);
}
//删除值为key的结点
ElemSN * DelKey(ElemSN *h,int key){
ElemSN *p,*q;
for(q=h;q->next!=h;q=q->next);
printf("%d",q->data);
p=h;
if(p->data==key)
{
q->next=p->next;
free(p);
}
else
{
for(q=h,p=h->next;p!=h&&p->data-key;q=p,p=p->next);
q->next=p->next;
free(p);
}
return h;
}
int main(void)
{
int a[]={3,25,8,7,9};
int i,key;
ElemSN *head;
head=Greatlink(a,5);
PrintLink(head);
printf("\n");
printf("请输入key:");
scanf("%d",&key);
head=DelKey(head,key);
PrintLink(head);
printf("\n");
return 0;
}
#include<stdlib.h>
#define N 5
typedef struct node{
int data;
struct node * next;
}ElemSN;
ElemSN * Greatlink(int a[],int n)
{
ElemSN * h,*p,*t;
int i;
h=NULL;
for(i=0;i<N;i++)
{
p=(ElemSN *)(malloc(sizeof(ElemSN)));
p->data=a[i];
if(!h)
{
p->next=p;
h=t=p;
}
else
{
p->next=h;
t=t->next=p;
}
}
return h;
}
void PrintLink(ElemSN *h)
{
ElemSN *p;
p=h;
printf("%5d",p->data);
for(p=h->next;p!=h;p=p->next)
printf("%5d",p->data);
}
//删除值为key的结点
ElemSN * DelKey(ElemSN *h,int key){
ElemSN *p,*q;
for(q=h;q->next!=h;q=q->next);
printf("%d",q->data);
p=h;
if(p->data==key)
{
q->next=p->next;
free(p);
}
else
{
for(q=h,p=h->next;p!=h&&p->data-key;q=p,p=p->next);
q->next=p->next;
free(p);
}
return h;
}
int main(void)
{
int a[]={3,25,8,7,9};
int i,key;
ElemSN *head;
head=Greatlink(a,5);
PrintLink(head);
printf("\n");
printf("请输入key:");
scanf("%d",&key);
head=DelKey(head,key);
PrintLink(head);
printf("\n");
return 0;
}

本文介绍了一个使用C语言实现的单链表创建过程及其节点删除操作。通过定义结构体并利用指针操作,实现了单链表的构建与指定值节点的删除,并附带了完整的打印功能。
973

被折叠的 条评论
为什么被折叠?



