为什么输出窗口首字母会是乱码
程序代码:
#include<stdio.h> #include<malloc.h> #define MAXLEN 100 typedef char DataType; typedef struct node { DataType data; struct node *next; }ListNode,*LinkList; int GetLength(LinkList head);//求单链表长度 LinkList CreatListF(void);//头插法建单链表 LinkList CreatListL(void);//尾插法建单链表 ListNode *GetNode(LinkList head, int i);//在单链表中按序号查找元素 ListNode *Locate(LinkList head, DataType x);//在单链表中按值查找元素 void InsertList(LinkList head, DataType x, int i);//在单链表中插入元素 void DeleteList(LinkList head, int i);//删除元素 void delete_list(LinkList head);//删除重复的元素 void PutOut(LinkList head);//单链表输出 int main(void) { int Length; ListNode *p; LinkList head=NULL; head = CreatListL(); p = (ListNode*)malloc(sizeof(ListNode)); PutOut(head); Length = GetLength(head); printf("长度为:%d\n", Length); p=GetNode(head, 2); printf("该位置的值为:%c\n", p->data); p = Locate(head, 'F'); printf("该位置的值为:%c\n", p->data); InsertList(head, 'L', 3); PutOut(head); DeleteList(head, '3'); PutOut(head); free(p); return 0; } LinkList CreatListF(void)//头插法建单链表 { char ch; LinkList head = (ListNode*)malloc(sizeof(ListNode)); ListNode *s; ch = getchar(); while (ch != '\n') { s = (ListNode*)malloc(sizeof(ListNode)); s->data = ch; s->next = head->next; head->next = s; ch = getchar(); } return head; } LinkList CreatListL(void)//尾插法建单链表 { char ch; LinkList head = (LinkList *)malloc(sizeof(LinkList)); ListNode *s, *r; r = head; ch = getchar(); while ((ch = getchar()) != '\n') { s = (ListNode *)malloc(sizeof(ListNode)); s->data = ch; r->next = s; r = s; } r->next = NULL; return head; } ListNode *GetNode(LinkList head, int i)//在单链表中按序号查找元素 { int j; ListNode *p; if (i<1 || i>GetLength(head)) exit(1); p = head->next; j = 1; while (p != NULL&&j < i) { p = p->next; j++; } return p; } ListNode *Locate(LinkList head, DataType x)//在单链表中按值查找元素 { ListNode *p = head->next; while (p&&p->data != x) p = p->next; return p; } void InsertList(LinkList head, DataType x, int i)//在单链表中插入元素 { ListNode *p, *q, *s; int j = 1; p = head; if (i<1 || i>GetLength(head) + 1) exit(1); s = (ListNode *)malloc(sizeof(ListNode)); s->data = x; while(j <= i) { q = p; p = p->next; j++; } s->next = q->next; q->next = s; } void DeleteList(LinkList head, int i)//在单链表中删除元素 { ListNode *p, *q; int j = 1; p = head; if (i<1 || i>GetLength(head)) exit(1); while (j < i) { p = p->next; j++; } q = p->next; p->next = q->next; free(q); } void delete_list(LinkList head)//在单链表中删除重复的结点 { ListNode *p, *q, *s; p = head->next; if (p == NULL) exit(1); while (p->next != NULL) { q = p; while (q->next) { if (q->next->data == p->data) { s = q->next; q->next = q->next->next; free(s); } else q = q->next; } p = p->next; } } int GetLength(LinkList head)//求单链表长度 { int Length=0; ListNode *p; p = head; while (p) { p = p->next; Length++; } return Length; } void PutOut(LinkList head)//单链表输出 { ListNode *p; p = head; while (p != NULL) { printf("%c", p->data); p = p->next; } printf("\n"); }
如图,输入ABCDEFG后输出中A变成了乱码,就算首字母换成其他也是同样问题,求大神帮助解决一下,非常感谢