一个c链表程序

一个c链表程序,第1张

常识性错误,你把位置换一下就对了。

struct node {

int item;

link next;

};

typedef struct node link;

#define N 9;

#define M 5;

link NODE(int item,link next)

{

link t=malloc(sizeof (link ));

titem=item;

tnext=next;

return t;

}

貌似你的程序问题还多,比如内存释放,NOD函数写写得也有问题,什么时候该用传值,什么时候该传址,你还是先弄清楚概念先吧。

写好了,你看下

#include <stdioh>

#include <stdlibh>

#include <malloch>

typedef struct node

{

int data;

struct node next;

}Node;

void InitList(Node head);

void CreateList(Node head);

void InsertList(Node head, int key);

void DeleteList(Node head, int key);

void PrintList(Node head);

//初始化链表

void InitList(Node head)

{

(head) = (Node )malloc(sizeof(Node));

(head)->next = NULL;

}

//创建链表

void CreateList(Node head)

{

int i;

printf("您好,请输入您要插入的数据:\n");

scanf("%d", &i);

while(i != 0)

{

InsertList(head, i);

scanf("%d", &i);

}

}

//插入链表

void InsertList(Node head, int key)

{

Node p, q, s;

q = (head);

p = (head)->next;

while(p)

{

q = p;

p = p->next;

}

s = (Node )malloc(sizeof(Node));

s->data = key;

s->next = NULL;

q->next = s;

}

//删除链表

void DeleteList(Node head, int key)

{

Node p, q;

q = (head);

p = (head)->next;

while(p && p->data != key)

{

q = p;

p = p->next;

}

if(p)

{

q->next = p->next;

free(p);

p = NULL;

}

}

//输出链表

void PrintList(Node head)

{

Node p;

p = (head)->next;

while(p)

{

printf("%d\n", p->data);

p = p->next;

}

}

int main(void)

{

Node head;

int i;

InitList(&head);

CreateList(&head);

printf("删除前的数据:\n");

PrintList(&head);

printf("请输入您要删除的数据:\n");

scanf("%d", &i);

DeleteList(&head, i);

printf("删除后的数据:\n");

PrintList(&head);

return 0;

}

Makefile:

#the simplest example

OBJS = tmpo

CC = gcc

CFLAGS = -Wall -O -g

tmp: $(OBJS)

$(CC) $(OBJS) -o tmp

tmpo: tmpc

$(CC) $(CFLAGS) -c tmpc -o tmpo

clean:

rm -f o ~ tmp

您好,请输入您要插入的数据:

1 2 3 4 0

删除前的数据:

1

2

3

4

请输入您要删除的数据:

1

删除后的数据:

2

3

4

这是个很简单的链表创建和输出

#include

#include

typedef

struct

linkednode

{

char

data;

struct

linkednode

next;

}node,link_list;//链表节点的结构及重命名

link_list

creat()//创建一个链表返回类型是链表的首地址

{

link_list

L;

node

p1,p2;

char

data;

L=(node)malloc(sizeof(node));//开辟存储空间

p2=L;

while((data=getchar())!='\n')//输入回车键时结束输入

{

p1=(node)malloc(sizeof(node));

p1->data=data;

p2->next=p1;

p2=p1;

}

p2->next=NULL;

return

L;

}

void

print(link_list

L)//把链表输出

{

node

p;

p=L->next;

while(p!=NULL)

{

printf("%c",p->data);

p=p->next;

}

printf("\n");

}

void

main()

{

link_list

L=NULL;

char

x;

printf("请输入链表节点:\n");

L=creat();

print(L);

}

问题出在:s create_list()这个函数

我们来分析下:

p=(s )malloc(sizeof(s));//申请一个新的空间

p->i=a[j];//为该空间的数据单元赋值

j--;//循环条件变量

p->next=h->next;//意味着p的下一个空间与头指针h一致

h->next=p;//h的下一个元素为p这个新空间

好了,现在我们分析最后这两句:(倒数第二句我们称为句A,最后一句称句B)

1、h为定址:h指针指向的空间始终不变这个比较好理解,因为AB句对h的 *** 作只 *** 作它的next。那好,现在我们令h的地址为0x00

2、如图:看看程序运行的流程。

所以另一种改法是:去掉p->next = '\0‘就可以了,不然的话,就前功尽弃了。当然,最后还是要加上free(p)

此时输出的是:12345

改后的程序如下:

s create_list()

{

int a[]={1,2,3,4,5},j=4;

s h,p;

h=(s )malloc(sizeof(s));

h->next='\0';

while(j>=0)

{

p=(s )malloc(sizeof(s));

p->i=a[j];j--;p->next=h->next;h->next=p;

}

/p->next='\0';/

free(p);

return h;

}

<<<<<最后,十分感谢你,让我琢磨出了双指针建单链表的方法。我第一次给你的方法要用三指针

//修改完了。。。。

#include<stdioh>

#include <stringh>

#include<stdlibh>

struct student{

char name[20];

char num[20];

int grade;

struct student Nextptr;

};

typedef struct student Stu;

typedef Stu St;

St creat();

main()

{

St headptr1=NULL;

St headptr2=NULL;

headptr1=creat();

//headptr2=find(&headptr1);

//print(headptr2);

//destory(headptr1,headptr2);

system("pause");

}

St creat()//建立链表1

{

St headptr1=NULL,last1=NULL,current1=NULL;

printf("Please input the information of the students:\n");

// fflush(stdin);

current1=(St) malloc(sizeof(current1));

gets(current1->name);

// fflush(stdin);

gets(current1->num);

scanf("%d",&current1->grade);

while(strcmp(current1->name,"#####")!=0)

{

if(headptr1==NULL)

{

headptr1=current1;

last1=current1;

}

else

{

last1->Nextptr=current1;

last1=current1;

}

current1=(St) malloc(sizeof(current1));

fflush(stdin);

gets(current1->name);

fflush(stdin);

gets(current1->num);

scanf("%d",&current1->grade);

}

current1->Nextptr=NULL;

return headptr1;

}

if(i==0)

pf=head=pb; //如果i=0即输入的为第一个元素,则把它设置为表头

else pf->next=pb;//否则,用上一个元素的next指针指向它,即入链表的 *** 作

pb->next=NULL;//把这个元素的next指针赋值为NULL

pf=pb;//把当前元素指针付给下次循环用的指针变量

不明白的,就再追问吧!

以上就是关于一个c链表程序全部的内容,包括:一个c链表程序、50分求用c语言编写链表程序、用C语言实现建立一个单链表的过程,并实现打印链表中每一个元素,写出完整程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9713405.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存