建立单向链表的两个程序

链表操作与算法实现

1.第一个较简单,建立两个结点(一个是空结点)

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
	int data;
    node* next;
}a;
node* creat()//或者struct* creat
{
	node* p,*s;
	int i;
	p=(node*)malloc(sizeof(node));
	s=(node*)malloc(sizeof(node));
	p->next=NULL;
	printf("input a number\n");
	scanf("%d",&i);
	s->data=i;

	s->next=p->next;
	p->next=s;
 return p;
} 
node* put(node* p)
{
	node* q=p->next;
	if(p->next==NULL)
	{

		return 0;
	}
	else
	{printf("*********\n");
		printf("%d\n",q->data);
	}
}
int main()
{
	int i,j;
	node* p=creat();
	put(p);
	return 0;
}
2.第二个比第一个更进一步

#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct node
{
	int data;
	node* next;
};
node* creat()
{
	node* p,*s;
	char a[30],i;
	printf("please enter a string:\n");
	scanf("%s",a);
	int len=strlen(a);
	p=(node*)malloc(sizeof(node));//node 和struct的字节为8;
	//printf("*****sizeof struct:%d\n",sizeof(node));
	p->next=NULL;
	for(i=0;i<len;i++)
	{
			s=(node*)malloc(sizeof(node));
			s->data=a[i];
			s->next=p->next;
			p->next=s;	
	}
	return p;

}
void print(node* p)
{
	node* q=p->next;

	while(q!=NULL)//相当于头结点的地址的指向不=NULL;
	{				     //输出表中非最后一个元素
		printf("%c",q->data);
		q=q->next;
	}
	//printf("%c",q->data);  //输出表中最后一个数据
	printf("\n");	
}
 void elete(node* p,char a)//此处函数名不能是delete
{
	node* e=p->next;
	
	while(e!=NULL)
	{
		if(e->data==a)
		{
			p->next=e->next;
			//free(e->data);释放的是e
			free(e);
			break;
		}
		e=e->next;
		p=p->next;
	}
	if(e==NULL)//e 于此处就是个地址指向
		printf("this node is not exist!\n");
}

	//print函数另一种写法:
node* find(node* p,char x)
{
	node* q=p->next;
	int a=0;
	//while(q->next!=NULL)*******此类语句不能再加->next,如此会忽略最后一个结点
	while(q!=NULL)
	{
		if(q->data==x)
			return q;
		q=q->next;
	}
	return q;
}
node* insert(node* p,char d)
{
	char h;
	node* q=p->next,*t;
	t=(node*)malloc(sizeof(node));//不要忘记开辟区间,由于忘记导致程序多次意外结束;
	printf("please enter the character you want to insert:\n");
	getchar();
	scanf("%c",&h);
	printf("********\n");
	t->data=h;
	printf("%c\n",h);
	if(q->next==NULL)
	{printf("3********\n");
		t->next=q->next;
		q->next=t;
	}printf("1********\n");
	node* w;
	w=find(p,d);
	if(w==NULL)
		printf("fause:this character is not exist!\n");
	else
	{ 
			t->next=w->next;
			w->next=t;
	}
}
 int main()
 {
	//node* p,w;这种写法错误****注意 注意
	node* p,*w;
	char x,a,v;
	p=creat();
	print(p);
	printf("input the number you want to find in the link:\n");
	getchar();//吸收\n
	scanf("%c",&x);		 //********************************************
						//*******此处scanf竟然会吸收‘\n’,不懂!!!!!!!!**
    w=find(p,x);                   //*********************************************
	printf("please enter the character you want to delete:\n");

	getchar();
	scanf("%c",&a);
	elete(p,a);
	print(p);
	printf("input the number you want to insert before:\n");
	getchar();
	scanf("%c",&v);
	insert(p,v);
	print(p);
	return 0;
}

<span style="font-size:18px;color:#6600CC;"><strong>(1)creat函数(尾插法正序)
         a,需要三个指针变量q,s,r
         b,r->next指向s
         c,q与s循环建立链表</strong></span>
node* creat()
 {
	node* p,*s,*r;
	int i,len;
	char a[30];
	printf("please enter a string:\n");
	scanf("%s",a);
	len=strlen(a);
	r=(node*)malloc(sizeof(node));
	s=(node*)malloc(sizeof(node));
	r->next=s;
	s->data=a[0];
	for(i=1;i<len;i++)
	{
		p=(node*)malloc(sizeof(node));
		p->data=a[i];
		s->next=p;
		s=s->next;
	}
	s->next=NULL;//
return r;
}


模板分析:
1.creat函数(头插法倒序)
    a,先建立空结点,malloc函数

    b,依次建立后续结点

2.print函数
3.find函数
    a,链表为空时,返回NULL
    b,链表不为空时,依次查找
4.elete函数(首先此处不能用delete)
    a,依次查找后删除
    b,并释放内存free函数
    c,找不到该结点就报错
5.insert函数
    a,链表为空时,直接插入,此时链表共有两个结点
    b,链表不为空时,调用find函数
    c,对find所返回的指针进行插入操作
6.change 与count函数较为简单,此处不再详述

### 创建单向链表 为了使用 C 语言创建一个可以存储多个整数的单向链表,可以通过定义结构体 `struct link` 来表示链表中的节点。每个节点包含两个部分:一个是用于保存数据的数据域;另一个是指向下个节点地址的指针。 #### 定义链表节点结构 ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构 struct link { int data; struct link* next; }; ``` 此段代码定义了一个名为 `link` 的结构体,它有两个成员变量:`data` 指向下一个节点的指针 `next`[^1]。 #### 实现添加新节点到链表尾部的功能 对于向链表中追加新的节点操作,可编写如下函数: ```c // 向链表尾部添加节点 struct link* AppendNode(struct link* head, int data) { // 动态分配内存给新节点 struct link* newNode = (struct link*)malloc(sizeof(struct link)); newNode->data = data; newNode->next = NULL; if (!head) { // 如果头节点为空,则返回新建的节点作为头部 return newNode; } struct link* temp = head; while (temp->next != NULL) { // 找到最后一个节点 temp = temp->next; } temp->next = newNode; // 将最后一个节点的next设为新节点 return head; } ``` 这段代码实现了当传入 `-1` 结束标志前不断接收用户输入并将这些依次加入到由 `head` 指定的链表之后的操作逻辑。 #### 主程序读取输入并构建链表 最后,在主函数里调用上述方法完成整个过程: ```c int main() { struct link* head = NULL; int num; printf("请输入若干个正整数(-1 表示结束):\n"); while(1){ scanf("%d", &num); if(num == -1){ break;} head = AppendNode(head,num); } // 输出链表内容 struct link* current = head; printf("您输入的数字组成的链表为:\n"); while(current!=NULL){ printf("%d -> ",current->data); current=current->next; } printf("NULL\n"); return 0; } ``` 以上就是完整的基于C语言实现通过输入若干个整数建立单向链表的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值