循环链表

来源:互联网 发布:sql是系统软件 编辑:程序博客网 时间:2024/05/22 22:08
#include<stdio h="">  #include<string h="">  #include <malloc h="">  #include<stdlib h="">    typedef struct Node  {      char data;      struct Node *next;  }node;    node *head=NULL;  node *p=NULL;    void creat(char c[])    {            int i=0;                               while(c[i] != '\0')                 {          p = (node*)malloc(sizeof(node));          p->next=NULL;          p->data=c[i];          if(head == NULL)          {              head = p;              head->next = p;          }          else          {              p->next = head->next;              head->next = p;              head = p;          }          i++;      }  }    void add()  {      char c;      scanf(" %c",&c);      p = (node*)malloc(sizeof(node));       p->data=c;      if(head == NULL)      {          head = p;          head->next = p;      }      else      {          p->next = head->next;          head->next = p;          head = p;      }  }    void delate()  {      if (head == NULL)      {                }else if (head->next == head)      {          p = head;          head = NULL;            }else{          p = head->next;          head->next = p->next;                }        p = NULL;        }    void move()  {      if(head!=NULL)      {          head = head->next;      }  }    int main()  {            int steps,option,count = 0;      char S[100];      scanf("%d",&steps);      scanf("%s",&S);      creat(S);            while(count < steps)               {          scanf("%d",&option);                  switch(option)          {              case 1:add();break;                       case 2:delate();break;                          case 3:move();break;                 }          count++;           }      p = head;      printf("%c",p->data);      p=p->next;      while(p!=head)      {          printf("%c",p->data);          p=p->next;      }      printf("\n");    }  

0 0