Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<ctype.h>
  5.  
  6. typedef struct SNode_
  7. {
  8.   struct SNode_ *prev, *next;
  9.   int v;
  10. } SNode;
  11.  
  12. typedef struct SList2_
  13. {
  14.   SNode *first, *last, *cur;
  15. } SList2;
  16.  
  17. void Add(SList2 *l, int v);
  18. void Scan(SList2 *l, int n);
  19. void Print(SList2 *l);
  20. SList2 *Create(void);
  21.  
  22. SList2 *Create(void)
  23. {
  24.   SList2 *l;
  25.   l = (SList2*)malloc(sizeof(*l));
  26.   l -> cur = l -> first = l -> last = NULL;
  27.   return l;
  28. }
  29.  
  30.  
  31.  
  32. void Add(SList2 *l, int v)
  33. {
  34.   SNode *n;
  35.   n = (SNode*)malloc(sizeof(*n));
  36.   n -> v = v;
  37.   n -> prev = n -> next = NULL;
  38.   if (l -> cur == NULL)
  39.   {
  40.     l -> cur = l -> first = l -> last = n;
  41.   }
  42.   else
  43.   {
  44.     n -> prev = l -> cur;
  45.     n -> next = l -> cur -> next;
  46.     l -> cur -> next = n;
  47.     if (n -> next) n -> next -> prev = n;
  48.     else l -> last = n;
  49.   }
  50. }
  51.  
  52. void Scan(SList2 *l, int n)
  53. {
  54.   int x = n + 1;
  55.   for (int i = 0; i < n; i++)
  56.   {
  57.     x -= 1;
  58.     Add(l, x);
  59.   }
  60. }
  61.  
  62. void Print(SList2 *l)
  63. {
  64.   SNode *n = l -> first;
  65.   printf("%d", n -> v);
  66.   while (l -> cur != NULL)
  67.   {
  68.     printf("%d", n -> v);
  69.     if (n -> next != NULL)
  70.     {
  71.       n = n -> next;
  72.     }
  73.   }
  74. }
  75.  
  76. int main(void)
  77. {
  78.  SList2 *l;
  79.  l = Create();
  80.  Scan(l, 6);
  81.  Print(l);
  82.  return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement