一条简单的单向链表

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
	char name[20];
	struct node *next;
};
struct node *create()
{
    struct node *head,*current,*next;
	char str[20],flag;
	head=(struct node*)malloc(sizeof(struct node));
	printf("请输入姓名");
	scanf("%s",str);
     getchar();
	strcpy(head->name,str);
	current=head;
	printf("是否继续输入");
	flag=getchar();
	while(flag!='N')
	{   

		printf("请输入名字:");
		scanf("%s",str);
		getchar();
		next=(struct node*)malloc(sizeof(struct node));
		strcpy(next->name,str);
		current->next=next;
	    current=next;
		printf("是否继续输入");
		scanf("%c",&flag);
		getchar();
	}
	current->next=NULL;
	return head;
}
void list(struct node *p)
{     
   
   while(p!=NULL)
   {
	   printf("%s\n",p->name);
	   p=p->next;
   }
}
void change(struct node *p,int n)
{    int i;
     
       
	 for(i=1;i<n;i++)
	 { p=p->next;}
     scanf("%s",p->name);
	 getchar();	 
}
void detelist(struct node *p,int n)
{   int i;
	struct node *current;
	
	for(i=1;i<n-1;i++)
	  p=p->next;
	current=p;
	p=current->next;
	current->next=p->next;
}
void insert(struct node *p,int n)
{
     struct node *current,*node,*next;
	 int i;
    for(i=1;i<n-1;i++)
	  p=p->next;
     node=(struct node*)malloc(sizeof(struct node));
     scanf("%s",node->name);
        current=p;
    next=current->next;
	 current->next=node;
     node->next=next;
}
int main()
{
   struct node *p;
	   int n;
   
   p=create();
   list(p);
   scanf("%d",&n);
   change(p,n);
   list(p);
   scanf("%d",&n);
   detelist(p,n);
   list(p);
   scanf("%d",&n);
   insert(p,n);
   list(p);
   return 0;
}
```这是作者自己写的一条链表,包含链表的创造,删除,插入以及遍历


猜你喜欢

转载自blog.csdn.net/weixin_44146373/article/details/85489952