C语言单链表,输出总是最后少一个,求大神
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<malloc.h>#defineOK1#def...
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <malloc.h>
#define OK 1
#define _CRT_SECURE_NO_WARNINGS
typedef int Status;
typedef char QElemType;
typedef struct LNode {
QElemType data;
struct LNode *next;
}LNode, *LinkList;
LinkList L1;
LinkList CreatList_L(LinkList L, int n);
Status DispList_L(LinkList L);
void main()
{
char c;
int n;
while (1)
{
printf(" 列表\n");
printf("===========================================\n");
printf("1.创建链表(字符型)\n2.遍历链表\n3.统计单链表中的字符\n");
printf("===========================================\n");
printf("请输入你的选择:");
scanf_s(" %c", &c);
switch (c)
{
case'1':
printf("请输入字符串的长度:\n");
scanf_s(" %d", &n);
L1 = CreatList_L(L1, n);
break;
case '2':
DispList_L(L1);
break;
}
}
system("PAUSE");
}
LinkList CreatList_L(LinkList L, int n)
{
int i;
LNode *p;
L = (LinkList )malloc(sizeof(LNode));
L->next = NULL;
for (i = n; i > 0; --i) {
p = (LinkList )malloc(sizeof(LNode));
printf("请输入%d个元素:\n", n - i + 1);
scanf_s("%c\n", &p->data);
p->next = L->next;
L->next = p;
}
printf("一个拥有%d个元素的链表已经被建立\n", n);
return L;
}
Status DispList_L(LinkList L)
{
LNode *p=L;
if (p == NULL) //if (p==NULL)
{
printf("A null list!");
return 0;
}
p = p->next;
while (p != NULL)//while (p!=NULL)
{
printf("%c", p->data);
p = p->next;
}
printf("\n");
return OK;
} 展开
#include<stdlib.h>
#include<string.h>
#include <malloc.h>
#define OK 1
#define _CRT_SECURE_NO_WARNINGS
typedef int Status;
typedef char QElemType;
typedef struct LNode {
QElemType data;
struct LNode *next;
}LNode, *LinkList;
LinkList L1;
LinkList CreatList_L(LinkList L, int n);
Status DispList_L(LinkList L);
void main()
{
char c;
int n;
while (1)
{
printf(" 列表\n");
printf("===========================================\n");
printf("1.创建链表(字符型)\n2.遍历链表\n3.统计单链表中的字符\n");
printf("===========================================\n");
printf("请输入你的选择:");
scanf_s(" %c", &c);
switch (c)
{
case'1':
printf("请输入字符串的长度:\n");
scanf_s(" %d", &n);
L1 = CreatList_L(L1, n);
break;
case '2':
DispList_L(L1);
break;
}
}
system("PAUSE");
}
LinkList CreatList_L(LinkList L, int n)
{
int i;
LNode *p;
L = (LinkList )malloc(sizeof(LNode));
L->next = NULL;
for (i = n; i > 0; --i) {
p = (LinkList )malloc(sizeof(LNode));
printf("请输入%d个元素:\n", n - i + 1);
scanf_s("%c\n", &p->data);
p->next = L->next;
L->next = p;
}
printf("一个拥有%d个元素的链表已经被建立\n", n);
return L;
}
Status DispList_L(LinkList L)
{
LNode *p=L;
if (p == NULL) //if (p==NULL)
{
printf("A null list!");
return 0;
}
p = p->next;
while (p != NULL)//while (p!=NULL)
{
printf("%c", p->data);
p = p->next;
}
printf("\n");
return OK;
} 展开
展开全部
字符输入完要用getchar()吃掉回车
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <malloc.h>
#define OK 1
#define _CRT_SECURE_NO_WARNINGS
typedef int Status;
typedef char QElemType;
typedef struct LNode {
QElemType data;
struct LNode *next;
}LNode, *LinkList;
LinkList L1;
LinkList CreatList_L(LinkList L, int n);
Status DispList_L(LinkList L);
void main()
{
char c;
int n;
while (1)
{
printf(" 列表\n");
printf("===========================================\n");
printf("1.创建链表(字符型)\n2.遍历链表\n3.统计单链表中的字符\n");
printf("===========================================\n");
printf("请输入你的选择:");
scanf("%c", &c);
getchar();
switch (c)
{
case'1':
printf("请输入字符串的长度:\n");
scanf("%d", &n);
getchar();
L1 = CreatList_L(L1, n);
break;
case '2':
DispList_L(L1);
break;
}
}
system("PAUSE");
}
LinkList CreatList_L(LinkList L, int n)
{
int i;
LNode *p;
L = (LinkList )malloc(sizeof(LNode));
L->next = NULL;
for (i = n; i > 0; --i) {
p = (LinkList )malloc(sizeof(LNode));
printf("请输入%d个元素:\n", n - i + 1);
scanf("%c", &p->data);
getchar();
p->next = L->next;
L->next = p;
}
printf("一个拥有%d个元素的链表已经被建立\n", n);
return L;
}
Status DispList_L(LinkList L)
{
LNode *p=L;
if (p == NULL) //if (p==NULL)
{
printf("A null list!");
return 0;
}
p = p->next;
while (p != NULL)//while (p!=NULL)
{
printf("%c", p->data);
p = p->next;
}
printf("\n");
return OK;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询