Data structure code questions (2)

Decimal number to octal number

C input method

void Ten_to_Ei()
{
    
    
int n,a,sum = 0,i =0;
scanf("%d",&n);
while(n)
{
    
    
a = n%8;
n = n/8;
sum += a*pow(10,i);
i++;
}
printf("八进制:%d",sum);
}

Insert X into stack i's stack

void DBIpush(DblStack &s,SElemType x,int i){
    
    
	if(s.top[0]+1==s.top[1] || s.top[1]-1==s.top[0])
		return;
	else{
    
    
		s.V[s.top[i]]=X;
		s.top[i]++;
	}
}

Please add a picture description

Use the sequential stack to reverse the singly linked list (a1, a2,...,an) with the head node to (an,an-1...)

typedef struct node
{
    
    
   int data;
   struct node *next;
} Lnode;
void fun(Lnode *h) {
    
    
   Lnode *p, *q, *r;
   p = h->next;
   if (p == NULL)
      return;
   q = p->next;
   p->next = NULL;
   while (q) {
    
    
      r = q->next;
      q->next = p;
      p = q;
      q = r;
   }
   h->next = p;
}
Lnode *creatlist(int a[]) {
    
    
   Lnode *h, *p, *q;
   int i;
   h = (Lnode *)malloc(sizeof(Lnode ));
   h->next = NULL;
   for (i = 0; i < N; i++) {
    
    
      q = (Lnode *)malloc(sizeof(Lnode ));
      q->data = a[i];
      q->next = NULL;
      if (h->next == NULL)
         h->next = p = q;
      else
      {
    
    
         p->next = q;
         p = q;
      }
   }
   return h;
}
void outlist(Lnode *h){
    
    
   Lnode *p;
   p = h->next;
   if (p == NULL)
      printf("NULL\n");
   else {
    
    
      printf("\start");
      do {
    
    
         printf("->%d", p->data);
         p = p->next;
      } while (p != NULL);
      printf("FINISH\n");
   }
}

Guess you like

Origin blog.csdn.net/m0_51581537/article/details/127719793