【无标题】11.19周记

链表


链表

带头结点的链表


提示:以下是本篇文章正文内容,下面案例可供参考

1.建立带头结点的链表

示例
**

建立结构体

**

typedef struct node
{
    
    
	int data;
	struct node* next;
}Node,*linklist;

初始化链表


linklist Initlist()
{
    
    
	linklist head;
	head = (Node*)malloc(sizeof(Node));
	head->next = NULL;
	return head;
}


尾插法

void creatlist1(linklist head)
{
    
    
	Node* r, * s;
	r = head;
	int data;
	while (1)
	{
    
    
		scanf("%d", &data);
		s = (Node*)malloc(sizeof(Node));
		if (data == 0)
		{
    
    
			break;
		}
		s->data = data;
		r->next = s;
		r = s;
	}
	r->next = NULL;
}

头插法

void creatlist2(linklist head)
{
    
    
	Node * s;
	int data;
	while (1)
	{
    
    
		scanf("%d", &data);
		s = (Node*)malloc(sizeof(Node));
		if (data == 0)
		{
    
    
			break;
		}
		s->data=data;
		s->next = head->next;
		head->next = s;
	}
} 

链表打印

void OutPut(linklist head)
{
    
    
	Node* p;
	p = head->next;
	while (p)
	{
    
    
		printf("%d", p->data);
		p = p->next;
	}
}

主函数

int main()
{
    
    
	linklist a,b;
	a=Initlist();
	creatlist1(a);
	creatlist2(b);
	OutPut(a);
	printf("\n");
	OutPut(b);
	return 0;
}

查找链表

linklist findnode(linklist head,int x)
{
    
    
	Node *r=head;
	
	while(r && x>=1)
	{
    
    
		r=r->next;
		x--;
	}
	if(!r)
	{
    
    
		printf("not found!");
		return NULL;
	}
	else
	{
    
    
		return r;
	}
}```

## **删除**

```c
void deltlist(linklist head,int x)
{
    
    
	Node* p=head,*r;
	int j=0;
	while(p && j<x-1)
	{
    
    
		p=p->next;
		j++;
	}
	r=p->next;
	if(p==NULL && p->next=NULL)
	{
    
    
		printf("no node!");
	}
	else
	{
    
    
		p->next=r->next;
		free(r);
	}
}

插入

void insert(linklist head,int x)
{
    
    
	Node*p=findnode(head,x-1);
	Node* s;
	int data;
	if(p==NULL)
	{
    
    
		printf("false!");
	}
	else
	{
    
    
		s=(Node*)malloc(sizeof(Node));
		scanf("%d",&data);
		s->data=data;
		s->next=p->next;
		p->next=s; 
	}
}

删除

void deltlist(linklist head,int x)
{
    
    
	Node* p=head,*r;
	int j=0;
	while(p && j<x-1)
	{
    
    
		p=p->next;
		j++;
	}
	r=p->next;
	if(p==NULL && p->next=NULL)
	{
    
    
		printf("no node!");
	}
	else
	{
    
    
		p->next=r->next;
		free(r);
	}
}

2.建立不带头结点的链表

代码如下(示例):

定义结构体

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node
{
    
    
	char name[10];
	int number;
	struct node *next;
}Node,*linklist;

初始化链表

代码如下(示例):

linklist creatlist()
{
    
    
	linklist head=NULL;
	Node *r,*s;
	char name[20];
	int number;
	printf("请输入学生的姓名和学号:\n"); 
	while(1)
	{
    
    
		scanf("%s",name);
		scanf("%d",&number);
		if(number==0)
		{
    
    
			break;
		}
		s=(Node*)malloc(sizeof(Node));
		strcpy(s->name,name);
		s->number=number;
		s->next=NULL;
		if(head==NULL)
		{
    
    
			head=s;
			r=head;
		}
		else
		{
    
    
			r->next=s;
			r=s;
		}
	}
	return head;
} 

打印链表

void output(a head)
{
    
    
	Node *p;
	p=head;
	while(p)
	{
    
    
		printf("%s\n",p->name);
		printf("%d\n\n",p->number);
		p=p->next;
	}
}

插入

linklist insert(Node head,int i)
{
    
    
	Node *p,*s;
	int j=0;
	p=head;
	printf("请输入待添加的学生的姓名和学号:\n");
	scanf("%s",s->name);
	scanf("%d",s->number); 
	if(i==1)
	{
    
    
		s->next=head;
		head=s;
	}
	else
	{
    
    
		while(j<i-1&&p)
		{
    
    
			p=p->next;
			j++;
		}
		s->next=p->next;
		p->next=s; 
	}
	return head;
}

删除

linklist Delete(linklist head,int pos)
{
    
    
	Node *p=head,*q;
	printf("************删除%d个学生*************\n",pos);
	if(head==NULL)
	{
    
    
		printf("the pos is ERROR!");
		return;
	}
	if(pos==1)
	{
    
    
		q=head;
		head=head->next;
		free(q);
	}
	else
	{
    
    
		int j=1;
		while(j<i-1&&p)
		{
    
    
			p=p->next;
			j++;
		}
		if(p==NULL||p->next==NULL)
		{
    
    
			printf("the pos is ERROR!");
			return;
		}
		else
		{
    
    
			q=p->next;
			q->next=p->next;
			free(q);
		}
	}
	return head;
}

主函数

int main()
{
    
    
	linklist list=NULL;
	list=creatlist();
	output(list);
	list=insert(list,1);
	output(list);
	list=Delete(list,2);
	output(list);
}

总结

这周学习了两种单链表的删插查改,学习了合并两个单链表和逆置单链表,下周准备解决循环链表和双向链表的删插改查,并且做一些链表题达到巩固的目的。

猜你喜欢

转载自blog.csdn.net/weixin_61966129/article/details/121429710
今日推荐