单链表的一些个人易错的问题(有理解不正确的地方请大佬指正)

请看下列代码

代码1:(正确示范)

#include<stdio.h>
#include<stdlib.h>
struct node
{
	int data;
	struct node *next;
 };
int main()
{
	int n;
	int a;
	printf("请输入数字个数:");
	scanf("%d",&n);
	struct node *head,*tail,*p,*num;
	p=(struct node*)malloc(sizeof(struct node));
	head=p;
	tail=p;
	head->next=NULL;
	
	printf("请输入已有数字:");
	for(int i=0;i<n;i++)
	{
		p=(struct node*)malloc(sizeof(struct node));
		scanf("%d",&p->data);
		
		tail->next=p;
		tail=p;
		tail->next=NULL;
	 } 
	 printf("输出单链表元素:");
	p=head->next;
	while(p!=NULL)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	return 0;
}

代码2:(错误示范)


#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
	int data;
	struct node *next;
 }node,*LinkList;

void suibian(LinkList &L)
{
	struct node *t;
	t=L->next;
		while(t!=NULL)
	{
		printf("%d ",t->data);
		t=t->next;
	}
}

int main()
{
	int n;
	int a;
	LinkList L;
	printf("请输入数字个数:");
	scanf("%d",&n);
	struct node *head,*tail,*p,*num;
	p=(struct node*)malloc(sizeof(struct node));
	head=p;
	tail=p;
	head->next=NULL;
	
	printf("请输入已有数字:");
	for(int i=0;i<n;i++)
	{
		p=(struct node*)malloc(sizeof(struct node));
		scanf("%d",&p->data);
		
		tail->next=p;
		tail=p;
		tail->next=NULL;
	 } 
	 printf("输出单链表元素:");
	 suibian(L);
	return 0;
}

OMG!!!!!你怎么会犯这种错误,你是不动脑子地copy过来的吗,说实话我当时确实是不想思考,简简单单当个摆烂人copy一下不香吗?咳咳,言归正传,我当时主要是对指针还是模糊的,似懂非懂的犯下这可笑的问题。

问题:插入的元素与链表L的关系 可以说是毫不相关啊,哥们这是定义了一个链表head啊

修改版本:


#include<stdio.h>
#include<stdlib.h>
struct node
{
	int data;
	struct node *next;
 }node,*LinkList;

void suibian(struct node *head)
{
	struct node *t;
	t=head->next;
		while(t!=NULL)
	{
		printf("%d ",t->data);
		t=t->next;
	}
}

int main()
{
	int n;
	int a;
	printf("请输入数字个数:");
	scanf("%d",&n);
	struct node *head,*tail,*p,*num;
	p=(struct node*)malloc(sizeof(struct node));
	head=p;
	tail=p;
	head->next=NULL;
	
	printf("请输入已有数字:");
	for(int i=0;i<n;i++)
	{
		p=(struct node*)malloc(sizeof(struct node));
		scanf("%d",&p->data);
		
		tail->next=p;
		tail=p;
		tail->next=NULL;
	 } 
	 printf("输出单链表元素:");
	 suibian(head);
	return 0;
}

 代码3.(正确示范)

#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct Lnode{
	ElemType data;
	struct Lnode *next; 
}Lnode,*LinkList;

int main()
{
	LinkList L;
	L=(struct Lnode*)malloc(sizeof(struct Lnode));
	struct Lnode *tail, *p;
	p=L;
	tail=L;
	L->next=NULL;
	int n;
	printf("元素个数:");
	scanf("%d",&n);
	printf("请输入元素:");
	for(int i=0;i<n;i++)
	{
		L=(struct Lnode*)malloc(sizeof(struct Lnode));
		scanf("%d",&L->data);
		tail->next=L;
		printf("%d ",tail->next->data);
		tail=L;
		tail->next=NULL; 
	
	 } 
	 printf("输出元素");
	 while(p->next!=NULL)
	 {
	 	printf("%d",p->next->data);
	 	p=p->next;
	 }
	 return 0;
 } 

代码4.(错误代码)

#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct Lnode{
	ElemType data;
	struct Lnode *next; 
}Lnode,*LinkList;

int main()
{
	LinkList L;
	L=(struct Lnode*)malloc(sizeof(struct Lnode));
	struct Lnode *tail, *p;
	tail=L;
	L->next=NULL;
	int n;
	printf("元素个数:");
	scanf("%d",&n);
	printf("请输入元素:");
	for(int i=0;i<n;i++)
	{
		L=(struct Lnode*)malloc(sizeof(struct Lnode));
		scanf("%d",&L->data);
		tail->next=L;
		printf("%d ",tail->next->data);
		tail=L;
		tail->next=NULL; 
	
	 } 
	 printf("\n");
	 printf("输出元素");
	 p=L;
	 while(p->next!=NULL)
	 {
	 	printf("%d",p->next->data);
	 	p=p->next;
	 }
	 return 0;
 } 

唉,兄弟,此时的L已经不是彼时的L了啊,啊?你问我为什么函数传L的时候是头结点 ,那个是系统默认在函数中使用链表L时指向它的头节点啊,哥们细心一点吧

问题:输出遍历起始位置不正确

在单链表中对指针的理解(引入第二个代码的修改版本吧,看的更直观一点)

简而言之就是,带*,&符号加变量的,指的是变量的内容(当然,一开始定义的类型得为指针类型),不带这两符号的就是取的地址,值得注意的是,运用结构体中的指针时需要使用->符号

在主函数中使用函数suibian时只需要传入地址就好即suibian(head);

一次的c语言程序作业,个人认为是一个比较好的模板,自己实力蒟蒻,大佬勿喷


#include <stdio.h>
#include <stdlib.h>

typedef int ElemType;
typedef struct Lnode {
    ElemType data;
    struct Lnode *next;
} Lnode, *LinkList;

int num = 0; // 统计链表中有多少个元素

void createlist(LinkList *L) {
    int n;
    struct Lnode *tail, *p;
    *L = (struct Lnode *)malloc(sizeof(struct Lnode));
    if (*L == NULL) {
        printf("Memory allocation failed!\n");
        exit(1);
    }
    tail = *L;
    tail->next = NULL;
    printf("请输入链表元素(以0标志结束输入):");
    while (1) {
        scanf("%d", &n);
        if (n == 0) break;
        p = (struct Lnode *)malloc(sizeof(struct Lnode));
        if (p == NULL) {
            printf("Memory allocation failed!\n");
            exit(1);
        }
        p->data = n;
        tail->next = p;
        tail = p;
        tail->next = NULL;
        num++;
    }
    printf("输出链表元素为:");
    p = *L;
    while (p->next != NULL) {
        printf("%d ", p->next->data);
        p = p->next;
    }
    printf("\n");
}

void insertx(LinkList *L, int i, int x) {
    struct Lnode *p, *q;
    p = *L;
    int pos = 0;

    // 如果i小于等于0,则将x插入表头之后
    if (i <= 0) {
        q = (struct Lnode *)malloc(sizeof(struct Lnode));
        if (q == NULL) {
            printf("Memory allocation failed!\n");
            exit(1);
        }
        q->data = x;
        q->next = p->next;
        p->next = q;
        num++;
    } else {
        // 查找第i个位置
        while (p->next != NULL && pos < i - 1) {
            p = p->next;
            pos++;
        }
        // 如果i大于链表长度,则将x插入链表末尾
        if (p->next == NULL) {
            q = (struct Lnode *)malloc(sizeof(struct Lnode));
            if (q == NULL) {
                printf("Memory allocation failed!\n");
                exit(1);
            }
            q->data = x;
            q->next = NULL;
            p->next = q;
        } else {
            // 在第i个位置之后插入x
            q = (struct Lnode *)malloc(sizeof(struct Lnode));
            if (q == NULL) {
                printf("Memory allocation failed!\n");
                exit(1);
            }
            q->data = x;
            q->next = p->next;
            p->next = q;
            num++;
        }
    }

    printf("插入元素后的链表为:");
    p = *L;
    while (p->next != NULL) {
        printf("%d ", p->next->data);
        p = p->next;
    }
    printf("\n");
}

int findx(LinkList &L,int x)
{
	LinkList p;
	p=L;
	int location=1;
	while(p->next!=NULL)
	{
		if(p->next->data==x)
		{
			return location;
		}
		location++;
		p=p->next;
	}
 return 0;
 } 
 
int countx(LinkList &L,int x)
 {
 	LinkList p;
	p=L;
	int count=0;
	while(p->next!=NULL)
	{
		if(p->next->data==x)
		{
		count++;
		}
		p=p->next;
	}
if(count==0)
return 0;
else
return count;
 }

int main() {
    int m, x, i;
    LinkList L;
    createlist(&L);
    printf("\n");
    printf("请输入您要查询的元素:");
scanf("%d",&x);
int getlocation=findx(L,x);
if(getlocation==0)
{
	printf("未找到您要查询的元素"); 
}
else
printf("您要查询的元素在序号%d位置",getlocation);
printf("\n");
printf("\n");


printf("请输入您要统计个数的元素:");
scanf("%d",&m); 
int getcount=countx(L,m);
printf("您要统计的元素%d在链表中总共有%d个",m,getcount); 

printf("\n");
printf("\n");
    printf("请输入第i个元素和且要在其后插入的值x(以空格分隔):");
    scanf("%d %d", &i, &x);
    insertx(&L, i+1, x);
    return 0;
}

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值