我是C编程新手。我编写了以下程序,在开头和结尾插入链接的元素。但是它不起作用。请帮我。似乎scanf无法正常工作。 我是C语言编程的新手。我编写了以下程序,在开头和结尾插入链接的元素。但是它不起作用。请帮我。似乎scanf无法正常工作。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef char DATA ;
struct node
{
DATA d ;
struct node * next ;
};
void print(struct node* pr );
struct node* insertBeg(struct node* head, char c);
void insertEnd(struct node* head, char c);
main()
{
DATA c1;
struct node* head=(struct node*) malloc(sizeof(struct node));
head->next=NULL;
printf("please enter first element\n");
scanf( &c1);
head->d=c1;
char begenninglement;
char endelement;
printf("enter element to insert at the beggning \n");
scanf(&begenninglement);
insertBeg(head, begenninglement);
print(head);
printf("enter element to insert at the end \n");
scanf( &endelement);
insertEnd(head, endelement);
print(head);
return 0;
}
struct node* insertBeg(struct node* head, char c)
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->d=c;
temp->next=head;
head=temp;
return head;
}
void insertEnd(struct node* head, char c)
{
struct node* temp, *p;
temp=(struct node*)malloc(sizeof(struct node));
temp->d=c;
p=head;
while(p!=NULL)
{
p=p->next;
}
p->next=temp;
temp->next=NULL;
}
void print(struct node *pr) {
struct node *current_node = pr;
while ( current_node != NULL) {
printf("%c", current_node->d);
current_node = current_node->next;
}
}