建立双向链表

来源:互联网 发布:矩阵ppt模板 编辑:程序博客网 时间:2024/05/02 00:23

今天第一次写博客,所以说呢,请大家多多包含,首先就来个建立一个双向链表吧!接下来还有向双向链表的插入和删除的文章,请大家多多提意见啊!

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct DuNode{
 char date;
 struct DuNode *prior;
 struct DuNode *next;
}*head;

int main()
{
 struct DuNode *p,*q;
 int i;
 if(!(head=(struct DuNode *)malloc(sizeof(struct DuNode))))
  exit(0);
 head->date='/0';
 head->prior=NULL;
 head->next=NULL;
 p=head;
 for(i=0;i<5;i++)
 {
  if(!(q=(struct DuNode*)malloc(sizeof(struct DuNode))))
   exit(0);
  printf("please input date:/n");
  scanf("%c",&q->date);
  getchar();
  q->prior=p;
  q->next=NULL;
  p->next=q;
  p=q;
 }
 p=head->next;
 for(i=0;i<5;i++)
 {
  printf("p->date=%c/n",p->date);
  p=p->next;
 }
 return 0;
}

 

总结! scanf函数的使用:之前交过两个学期的c语言,都没有碰到过这个问题,真是对不起我所教过的学生了非常抱歉!如上例:当需要输入一连串的字符时,回车是会作为一个字符给赋值的,如果没有下面的getchar(),那么输入的时候就只能输入5个字符了,打印出来的时候就会有回车,所以大家以后编程的时候就要多多注意啊!

原创粉丝点击