C 创建并打印字符的链接列表
我正在尝试使用此代码实现一个链表。此代码成功执行,但导致分段错误(核心转储)错误。我如何解决此问题C 创建并打印字符的链接列表,c,string,data-structures,linked-list,segmentation-fault,C,String,Data Structures,Linked List,Segmentation Fault,我正在尝试使用此代码实现一个链表。此代码成功执行,但导致分段错误(核心转储)错误。我如何解决此问题 #include<stdio.h> #include<stdlib.h> struct node{ char ch; struct node *next; }; struct node *head=(struct node *)malloc(sizeof(struct node)); struct node *p1=NULL; void addnode(ch
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node *head=(struct node *)malloc(sizeof(struct node));
struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
head->ch=ch;
head->next=NULL;
}
else {
struct node *New=(struct node *) malloc (sizeof(struct node));
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
}
}
void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\0')
addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
printf("\n%c",p1->ch);
}
#包括
#包括
结构节点{
char ch;
结构节点*下一步;
};
结构节点*头=(结构节点*)malloc(sizeof(结构节点));
结构节点*p1=NULL;
void addnode(char ch){
if(head==NULL){
头部->通道=通道;
head->next=NULL;
}
否则{
结构节点*New=(结构节点*)malloc(sizeof(结构节点));
用于(p1=头部;p1->next!=NULL;p1=p1->next);
p1->next=新建;
}
}
void main(){
char ch,frm,to;
printf(“\n输入字符串”);
而((ch=getchar())!=“\0”)
addnode(ch);
用于(p1=头部;p1!=NULL;p1=p1->next)
printf(“\n%c”,p1->ch);
}
这样做效果更好,我克服了错误:)。我错过了清晰的指针,在这里被纠正了
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node *head=(struct node *)malloc(sizeof(struct node));
struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
head->ch=ch;
head->next=NULL;
}
else {
struct node *New=(struct node *) malloc (sizeof(struct node));
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
}
}
void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\0')
addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
printf("\n%c",p1->ch);
}
#include<stdio.h>
#include<stdlib.h>
struct Node{
char ch;
struct Node *next;
};
struct Node head={'\0',NULL};
struct Node *p1=NULL;
void add(char ch){
if(head.ch=='\0')
head.ch=ch;
else{
struct Node *new=(struct node *)malloc(sizeof(struct Node));
new->ch=ch;
for(p1=&head;p1->next!=NULL;p1=p1->next);
p1->next=new;
}
}
void main(){
char c;
while((c=getchar())!='\n')
add(c);
for(p1=&head;p1!=NULL;p1=p1->next)
printf("%c\n",p1->ch);
}
这样做效果更好,我克服了错误:)。我错过了清晰的指针,在这里被纠正了
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node *head=(struct node *)malloc(sizeof(struct node));
struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
head->ch=ch;
head->next=NULL;
}
else {
struct node *New=(struct node *) malloc (sizeof(struct node));
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
}
}
void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\0')
addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
printf("\n%c",p1->ch);
}
#include<stdio.h>
#include<stdlib.h>
struct Node{
char ch;
struct Node *next;
};
struct Node head={'\0',NULL};
struct Node *p1=NULL;
void add(char ch){
if(head.ch=='\0')
head.ch=ch;
else{
struct Node *new=(struct node *)malloc(sizeof(struct Node));
new->ch=ch;
for(p1=&head;p1->next!=NULL;p1=p1->next);
p1->next=new;
}
}
void main(){
char c;
while((c=getchar())!='\n')
add(c);
for(p1=&head;p1!=NULL;p1=p1->next)
printf("%c\n",p1->ch);
}
我不确定这是c方式。但你必须考虑你的代码如何释放分配的指针…比如自由列表函数
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node *head=(struct node *)malloc(sizeof(struct node));
struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
head->ch=ch;
head->next=NULL;
}
else {
struct node *New=(struct node *) malloc (sizeof(struct node));
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
}
}
void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\0')
addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
printf("\n%c",p1->ch);
}
我来
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node *head=(struct node *)malloc(sizeof(struct node));
struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
head->ch=ch;
head->next=NULL;
}
else {
struct node *New=(struct node *) malloc (sizeof(struct node));
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
}
}
void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\0')
addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
printf("\n%c",p1->ch);
}
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node * addnode(struct node *head, struct node *p1, char ch) {
if(head==NULL) {
printf("......return 2... \r\n");
head=(struct node *)malloc(sizeof(struct node));
head->ch=ch;
head->next=NULL;
return head;
}
else {
struct node *New=NULL;
printf("......return ... \r\n");
New=(struct node *) malloc (sizeof(struct node));
New->ch = ch;
New->next=NULL;
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
return head;
}
}
void main() {
char ch,frm,to;
struct node *head=NULL, *p1=NULL;
printf("\nEnter the string \n");
while((ch=getchar())!='q')
head = addnode(head, p1, ch);
for(p1=head;p1!=NULL;p1=p1->next)
{
printf("\n%c",p1->ch);
}
}
#包括
#包括
结构节点{
char ch;
结构节点*下一步;
};
结构节点*添加节点(结构节点*头,结构节点*p1,字符ch){
if(head==NULL){
printf(“……返回2…\r\n”);
head=(结构节点*)malloc(sizeof(结构节点));
头部->通道=通道;
head->next=NULL;
回流头;
}
否则{
结构节点*New=NULL;
printf(“……返回…\r\n”);
New=(结构节点*)malloc(sizeof(结构节点));
新建->ch=ch;
新建->下一步=空;
用于(p1=头部;p1->next!=NULL;p1=p1->next);
p1->next=新建;
回流头;
}
}
void main(){
char ch,frm,to;
结构节点*head=NULL,*p1=NULL;
printf(“\n输入字符串\n”);
而((ch=getchar())!='q')
头部=添加节点(头部,p1,ch);
用于(p1=头部;p1!=NULL;p1=p1->next)
{
printf(“\n%c”,p1->ch);
}
}
另一个
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node *head=(struct node *)malloc(sizeof(struct node));
struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
head->ch=ch;
head->next=NULL;
}
else {
struct node *New=(struct node *) malloc (sizeof(struct node));
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
}
}
void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\0')
addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
printf("\n%c",p1->ch);
}
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
char ch;
struct node *next;
} *pNODE, NODE;
pNODE addnode2(pNODE head, pNODE p1, char ch) {
if(head==NULL) {
printf("......return 2... \r\n");
head=(pNODE)malloc(sizeof(NODE));
head->ch=ch;
head->next=NULL;
return head;
}
else {
struct node *new=NULL;
printf("......return ... \r\n");
new=(pNODE) malloc (sizeof(NODE));
new->ch = ch;
new->next=NULL;
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=new;
return head;
}
}
void main() {
char ch,frm,to;
pNODE head=NULL;
pNODE p1=NULL;
printf("\nEnter the string \n");
while((ch=getchar())!='q')
head = addnode2(head, p1, ch);
for(p1=head;p1!=NULL;p1=p1->next)
{
printf("\n%c",p1->ch);
}
}
#包括
#包括
类型定义结构节点{
char ch;
结构节点*下一步;
}*pNODE,NODE;
pNODE addnode2(pNODE头、pNODE p1、字符通道){
if(head==NULL){
printf(“……返回2…\r\n”);
head=(pNODE)malloc(sizeof(NODE));
头部->通道=通道;
head->next=NULL;
回流头;
}
否则{
结构节点*new=NULL;
printf(“……返回…\r\n”);
new=(pNODE)malloc(sizeof(NODE));
新建->ch=ch;
新建->下一步=空;
用于(p1=头部;p1->next!=NULL;p1=p1->next);
p1->next=新建;
回流头;
}
}
void main(){
char ch,frm,to;
pnodehead=NULL;
pNODE p1=NULL;
printf(“\n输入字符串\n”);
而((ch=getchar())!='q')
head=addnode2(head,p1,ch);
用于(p1=头部;p1!=NULL;p1=p1->next)
{
printf(“\n%c”,p1->ch);
}
}
我不确定这是c方式..但你必须考虑你的代码如何释放分配的指针…比如自由列表函数
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node *head=(struct node *)malloc(sizeof(struct node));
struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
head->ch=ch;
head->next=NULL;
}
else {
struct node *New=(struct node *) malloc (sizeof(struct node));
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
}
}
void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\0')
addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
printf("\n%c",p1->ch);
}
这是我的路
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node *head=(struct node *)malloc(sizeof(struct node));
struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
head->ch=ch;
head->next=NULL;
}
else {
struct node *New=(struct node *) malloc (sizeof(struct node));
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
}
}
void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\0')
addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
printf("\n%c",p1->ch);
}
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node * addnode(struct node *head, struct node *p1, char ch) {
if(head==NULL) {
printf("......return 2... \r\n");
head=(struct node *)malloc(sizeof(struct node));
head->ch=ch;
head->next=NULL;
return head;
}
else {
struct node *New=NULL;
printf("......return ... \r\n");
New=(struct node *) malloc (sizeof(struct node));
New->ch = ch;
New->next=NULL;
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
return head;
}
}
void main() {
char ch,frm,to;
struct node *head=NULL, *p1=NULL;
printf("\nEnter the string \n");
while((ch=getchar())!='q')
head = addnode(head, p1, ch);
for(p1=head;p1!=NULL;p1=p1->next)
{
printf("\n%c",p1->ch);
}
}
#包括
#包括
结构节点{
char ch;
结构节点*下一步;
};
结构节点*添加节点(结构节点*头,结构节点*p1,字符ch){
if(head==NULL){
printf(“……返回2…\r\n”);
head=(结构节点*)malloc(sizeof(结构节点));
头部->通道=通道;
head->next=NULL;
回流头;
}
否则{
结构节点*New=NULL;
printf(“……返回…\r\n”);
New=(结构节点*)malloc(sizeof(结构节点));
新建->ch=ch;
新建->下一步=空;
用于(p1=头部;p1->next!=NULL;p1=p1->next);
p1->next=新建;
回流头;
}
}
void main(){
char ch,frm,to;
结构节点*head=NULL,*p1=NULL;
printf(“\n输入字符串\n”);
而((ch=getchar())!='q')
头部=添加节点(头部,p1,ch);
用于(p1=头部;p1!=NULL;p1=p1->next)
{
printf(“\n%c”,p1->ch);
}
}
另一个
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node *head=(struct node *)malloc(sizeof(struct node));
struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
head->ch=ch;
head->next=NULL;
}
else {
struct node *New=(struct node *) malloc (sizeof(struct node));
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
}
}
void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\0')
addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
printf("\n%c",p1->ch);
}
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
char ch;
struct node *next;
} *pNODE, NODE;
pNODE addnode2(pNODE head, pNODE p1, char ch) {
if(head==NULL) {
printf("......return 2... \r\n");
head=(pNODE)malloc(sizeof(NODE));
head->ch=ch;
head->next=NULL;
return head;
}
else {
struct node *new=NULL;
printf("......return ... \r\n");
new=(pNODE) malloc (sizeof(NODE));
new->ch = ch;
new->next=NULL;
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=new;
return head;
}
}
void main() {
char ch,frm,to;
pNODE head=NULL;
pNODE p1=NULL;
printf("\nEnter the string \n");
while((ch=getchar())!='q')
head = addnode2(head, p1, ch);
for(p1=head;p1!=NULL;p1=p1->next)
{
printf("\n%c",p1->ch);
}
}
#包括
#包括
类型定义结构节点{
char ch;
结构节点*下一步;
}*pNODE,NODE;
pNODE addnode2(pNODE头、pNODE p1、字符通道){
if(head==NULL){
printf(“……返回2…\r\n”);
head=(pNODE)malloc(sizeof(NODE));
头部->通道=通道;
head->next=NULL;
回流头;
}
否则{
结构节点*new=NULL;
printf(“……返回…\r\n”);
new=(pNODE)malloc(sizeof(NODE));
新建->ch=ch;
新建->下一步=空;
用于(p1=头部;p1->next!=NULL;p1=p1->next);
p1->next=新建;
回流头;
}
}
void main(){
char ch,frm,to;
pnodehead=NULL;
pNODE p1=NULL;
printf(“\n输入字符串\n”);
而((ch=getchar())!='q')
head=addnode2(head,p1,ch);
用于(p1=头部;p1!=NULL;p1=p1->next)
{
printf(“\n%c”,p1->ch);
}
}
首先是简单的错误:当您在全局内存中分配内存时,您启动了一个函数调用(malloc也是一个函数)。函数调用只能在main或其他函数内进行。所以只要声明head在全局中不使用malloc
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node *head=(struct node *)malloc(sizeof(struct node));
struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
head->ch=ch;
head->next=NULL;
}
else {
struct node *New=(struct node *) malloc (sizeof(struct node));
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
}
}
void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\0')
addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
printf("\n%c",p1->ch);
}
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node *head=NULL;
struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
struct node *New=(struct node *) malloc (sizeof(struct node));
head=New;
New->ch=ch;
New->next=NULL;
}
else {
struct node *New=(struct node *) malloc (sizeof(struct node));
New->ch=ch;
New->next=NULL;
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
}
}
void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\n')
addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
printf("\n%c",p1->ch);
}
#包括
#包括
结构节点{
char ch;
结构节点*下一步;
};
结构节点*head=NULL;
结构节点*p1=NULL;
void addnode(char ch){
if(head==NULL){
结构节点*New=(结构节点*)malloc(sizeof(结构节点));
头=新的;
新建->ch=ch;
新建->下一步=空;
}
否则{
结构节点*New=(结构节点*)malloc(sizeof(结构节点));
新建->ch=ch;
新建->下一步=空;
用于(p1=头部;p1->next!=NULL;p1=p1->next);
p1->下一步