c语言中的单链表
下面是我用c语言编写的单链表代码。有人能帮我吗 这是我的主c文件:c语言中的单链表,c,C,下面是我用c语言编写的单链表代码。有人能帮我吗 这是我的主c文件: #include <stdio.h> #include <stdlib.h> #include "myclib.c" struct mydata { int num; char name; struct mydata *next; }; int main() { struct mydata *head, *newnode, *temp; head = (st
#include <stdio.h>
#include <stdlib.h>
#include "myclib.c"
struct mydata
{
int num;
char name;
struct mydata *next;
};
int main()
{
struct mydata *head, *newnode, *temp;
head = (struct mydata*)malloc(sizeof(struct mydata));
newnode = (struct mydata*)malloc(sizeof(struct mydata));
temp = (struct mydata*)malloc(sizeof(struct mydata));
head -> num = 123;
head -> name = 'k';
head -> next = NULL;
newnode -> num = 456;
newnode -> name = 'd';
newnode -> next = NULL;
printf("before.app.head = %p\n",head);
printf("before.app.newnode = %p\n",newnode);
printf("before.app.head->next = %p\n",head -> next);
printf("before.app.newnode->next = %p\n",newnode -> next);
head = (struct mydata*)addNodeAtHead(head, newnode, (newnode -> next));
printf("after.app.head = %p\n",head);
printf("after.app.newnode = %p\n",newnode);
printf("after.app.head->next = %p\n",head -> next);
printf("after.app.node->next = %p\n",newnode -> next);
temp = head;
while(temp != NULL)
{
printf("num : %d\n",temp -> num);
printf("name : %c\n",temp -> name);
temp = temp -> next;
}
free(temp);
free(head);
return 0;
}
#包括
#包括
#包括“myclib.c”
结构mydata
{
int-num;
字符名;
结构mydata*下一步;
};
int main()
{
结构mydata*head、*newnode、*temp;
head=(struct mydata*)malloc(sizeof(struct mydata));
newnode=(struct mydata*)malloc(sizeof(struct mydata));
temp=(struct mydata*)malloc(sizeof(struct mydata));
head->num=123;
head->name='k';
head->next=NULL;
newnode->num=456;
newnode->name='d';
newnode->next=NULL;
printf(“before.app.head=%p\n”,head);
printf(“before.app.newnode=%p\n”,newnode);
printf(“before.app.head->next=%p\n”,head->next);
printf(“before.app.newnode->next=%p\n”,newnode->next);
head=(struct mydata*)addNodeAtHead(head,newnode,(newnode->next));
printf(“after.app.head=%p\n”,head);
printf(“after.app.newnode=%p\n”,newnode);
printf(“after.app.head->next=%p\n”,head->next);
printf(“after.app.node->next=%p\n”,newnode->next);
温度=水头;
while(temp!=NULL)
{
printf(“数值:%d\n”,临时->数值);
printf(“名称:%c\n”,临时->名称);
温度=温度->下一步;
}
免费(临时);
自由(头);
返回0;
}
这是myclib.c文件:
#include <stdio.h>
void * addNodeAtHead(void *head, void *node, void *nodenext)
{
printf("\nbefore.head = %p\n",head);
printf("before.node = %p\n",node);
printf("before.nodenext = %p\n",nodenext);
nodenext = head;
head = node;
printf("after.head = %p\n",head);
printf("after.node = %p\n",node);
printf("after.nodenext = %p\n\n",nodenext);
return head;
}
#包括
void*addNodeHead(void*head,void*node,void*nodenext)
{
printf(“\nbefore.head=%p\n”,head);
printf(“before.node=%p\n”,node);
printf(“before.nodenext=%p\n”,nodenext);
nodenext=头部;
头部=节点;
printf(“after.head=%p\n”,head);
printf(“after.node=%p\n”,node);
printf(“after.nodenext=%p\n\n”,nodenext);
回流头;
}
我试图在头部前面添加新节点,而不是将头部指针更改为新节点。您需要将添加节点上的
下一个指针设置为指向原始头部节点。
我更改了AddNodeHead的签名:如果总是传递mydata*类型的指针,则不应传递void*。我还更改了变量名,以便(IMO)更清楚地了解它们的用途
mydata * addNodeAtHead(mydata * original_head, mydata * new_node)
{
new_node -> next = original_head;
return new_node; // new_node is now the head of the list!
}
将(newnode->next)
传递到函数addNodeAtHead
时。(newnode->next)
的值被复制到函数中的节点
变量中。您正在用新值head
更新该节点
变量。执行函数后,节点
变量被销毁,与(newnode->next)
没有关系。因此(newnode->next)
保持不变
若要解决此问题,只需更改您的addNodeHead
,如下所示:
void * addNodeAtHead(void *head, void *node)
{
printf("\nbefore.head = %p\n",head);
printf("before.node = %p\n",node);
((mydata *)node)-> next = (mydata *) head;
printf("after.head = %p\n",head);
printf("after.node = %p\n",node);
return node;
}
简单地说:
head = (struct mydata*)addNodeAtHead(head, newnode);
现在一切都应该好了。#包括
#include <stdio.h>
#include <stdlib.h>
//#include "myclib.c"
struct mydata
{
int num;
char name;
struct mydata *next;
};
struct mydata* addNodeAtHead(struct mydata* head, struct mydata* node)
{
#ifdef DEBUG
printf("\nbefore.head = %p\n",head);
printf("before.node = %p\n",node);
// printf("before.nodenext = %p\n",nodenext);
#endif
if(node){
node->next = head;
head = node;
}
#ifdef DEBUG
printf("after.head = %p\n",head);
printf("after.node = %p\n",node);
// printf("after.nodenext = %p\n\n",nodenext);
#endif
return head;
}
int main()
{
struct mydata *head, *newnode, *temp;
head = (struct mydata*)malloc(sizeof(struct mydata));
newnode = (struct mydata*)malloc(sizeof(struct mydata));
//temp = (struct mydata*)malloc(sizeof(struct mydata));//unused and rewrite to other pointer
head -> num = 123;
head -> name = 'k';
head -> next = NULL;
newnode -> num = 456;
newnode -> name = 'd';
newnode -> next = NULL;
#ifdef DEBUG
printf("before.app.head = %p\n",head);
printf("before.app.newnode = %p\n",newnode);
printf("before.app.head->next = %p\n",head -> next);
printf("before.app.newnode->next = %p\n",newnode -> next);
#endif
head = (struct mydata*)addNodeAtHead(head, newnode);
#ifdef DEBUG
printf("after.app.head = %p\n",head);
printf("after.app.newnode = %p\n",newnode);
printf("after.app.head->next = %p\n",head -> next);
printf("after.app.node->next = %p\n",newnode -> next);
#endif
temp = head;
while(temp != NULL)
{
printf("num : %d\n",temp -> num);
printf("name : %c\n",temp -> name);
temp = temp -> next;
}
/*
free(temp);//NULL
free(newnode);//...
free(head);//already changed
*/
temp=head;
while(temp != NULL){
struct mydata *prev = temp;
temp=temp->next;
free(prev);
}
return 0;
}
#包括
//#包括“myclib.c”
结构mydata
{
int-num;
字符名;
结构mydata*下一步;
};
结构mydata*添加节点头(结构mydata*头,结构mydata*节点)
{
#ifdef调试
printf(“\nbefore.head=%p\n”,head);
printf(“before.node=%p\n”,node);
//printf(“before.nodenext=%p\n”,nodenext);
#恩迪夫
如果(节点){
节点->下一步=头部;
头部=节点;
}
#ifdef调试
printf(“after.head=%p\n”,head);
printf(“after.node=%p\n”,node);
//printf(“after.nodenext=%p\n\n”,nodenext);
#恩迪夫
回流头;
}
int main()
{
结构mydata*head、*newnode、*temp;
head=(struct mydata*)malloc(sizeof(struct mydata));
newnode=(struct mydata*)malloc(sizeof(struct mydata));
//temp=(struct mydata*)malloc(sizeof(struct mydata));//未使用并重写为其他指针
head->num=123;
head->name='k';
head->next=NULL;
newnode->num=456;
newnode->name='d';
newnode->next=NULL;
#ifdef调试
printf(“before.app.head=%p\n”,head);
printf(“before.app.newnode=%p\n”,newnode);
printf(“before.app.head->next=%p\n”,head->next);
printf(“before.app.newnode->next=%p\n”,newnode->next);
#恩迪夫
head=(struct mydata*)addNodeAtHead(head,newnode);
#ifdef调试
printf(“after.app.head=%p\n”,head);
printf(“after.app.newnode=%p\n”,newnode);
printf(“after.app.head->next=%p\n”,head->next);
printf(“after.app.node->next=%p\n”,newnode->next);
#恩迪夫
温度=水头;
while(temp!=NULL)
{
printf(“数值:%d\n”,临时->数值);
printf(“名称:%c\n”,临时->名称);
温度=温度->下一步;
}
/*
空闲(临时);//空
自由(新节点);/。。。
自由(头部);//已更改
*/
温度=水头;
while(temp!=NULL){
结构mydata*prev=temp;
温度=温度->下一步;
免费(上);
}
返回0;
}
/----单链表程序(创建和遍历)---X
#包括
#包括
结构节点
{
国际信息;
结构节点*链接;
};
结构节点*第一;
int main()
{
void create();
无效遍历();
创建();
导线();
返回0;
}
void create()
{
结构节点*cpt,*ptr;
char ch;
ptr=(结构节点*)malloc(sizeof(结构节点));
printf(“输入数据”);
scanf(“%d”和&ptr->info);
第一个=ptr;
做
{
cpt=(结构节点*)malloc(sizeof(结构节点));
printf(“输入数据”);
扫描频率(“%d”,&cpt->info);
ptr->link=cpt;
ptr=cpt;
printf(“是否要创建新节点:\n”);
ch=getch();
}
while(ch='y');
ptr->link=NULL;
}
无效遍历()
{
结构节点*ptr;
ptr=第一;
while(ptr!=NULL)
{
printf(“输入的节点为:%d\n”,ptr->info);
ptr=ptr->link;
}
}
**单个链接器列表上的程序(创建和遍历带有注释)**
#包括//用于输入和输出操作的头文件。
#包括
结构节点//结构声明。
{
int info;//存储信息。
struct node*link;//存储下一个链接的地址。
};
结构节点*第一;//用于指向第一个节点。
**PROGRAM ON SINGLY LINKER LIST ( CREATION AND TRAVERSING WITH COMMENTS )**
#include<stdio.h> //Header file for input and output operations.
#include<conio.h>
struct node // structure declaration .
{
int info; // stores information.
struct node *link; // stores the address of next link.
};
struct node *first; // used to point to the first node.
void create(); // function call |NOTE| This line is optional.
void traverse(); // function call |NOTE| This line is optional.
int main() // main function.
{
create(); // function call for creation.
traverse(); // function call for traversing i.e nothing but display.
return 0;
}
void create() // declaration of create function , creation of nodes.
{
char ch; // variable to take an input from the user for decision.
struct node *ptr,*cpt; // these are used to create a node and referred to as
//previous pointer and current pointer.
ptr=(struct node*)malloc(sizeof(struct node)); //dynamic declaration
printf("Enter data into the first node \n");
scanf("%d",&ptr->info); // taking the input from the user.
first=ptr; //assigning the information taken from previous pointer to first for
//future identification.
do // loop which continuously generates and links new nodes to previous nodes.
{
cpt=(struct node*)malloc(sizeof(struct node)); //dynamic declaration of current
//pointer.
printf("Enter the data for another node \n");
scanf("%d",&cpt->info); // getting input from the user of next node
// information.
ptr->link=cpt; // linking the previous pointer to the new pointer.
ptr=cpt; // transferring the previous node information to the current node
//i.e previous pointer to the current pointer.
printf("Do you want to create another node <Y/N>:\n\n");
ch=getch(); // getting the users decision.
}
while(ch=='y'); // checking the users decision.
ptr->link=NULL; // assigning the previous pointer link to null;
}
void traverse() // function declaration of display or traversing.
{
int count=1; // counting variable for naming the nodes.
struct node *ptr; // pointer variable used for traversing.
ptr=first; // assigning ptr to the first for starting traversing from the
//first node.
printf("TRAVERSING OF A LINKED LIST \n");
while(ptr!=NULL) // checking whether the ptr is null or not, And if not
//executes the statements written in the body.
{
printf("The data stored in node %d is:%d \n",count,ptr->info); //prints the
// node id and information present in the node.
ptr=ptr->link; // This statement is used to traverse to the next node.
count++; // count incrementation.
}
}
// f☺ll☺w ☺n instagram ♥ ---> @cyber_saviour
#include <stdio.h>
void addNodeAtHead(void **head, void *node){
void *prH=*head;
*head=node;
node->next=prH;
}