1. Chain queue. Using a single linked list with head nodes to achieve chain queues, the complexity of insertions and deletions is O (1)
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct QNODE
{
int data;
Qnode *next;
}qnode;
typedef struct LINKQUEUE
{
Qnode *front;
Qnode *rear;
}linkqueue;
void Initialize (Linkqueue *linkqueue)
{
Linkqueue->rear= (qnode*) malloc (sizeof (Qnode));
linkqueue->front=linkqueue->rear;
linkqueue->front->next=null;
}
void ENQUEUE (Linkqueue *linkqueue)
{
qnode* temp= (Qnode *) malloc (sizeof (Qnode));
printf ("inserted value? \ n");
scanf ("%d", &temp->data);
temp->next=null;
linkqueue->rear->next=temp;
linkqueue->rear=temp;
}
void Show_queue (Linkqueue *linkqueue1)
{
Qnode *temp=linkqueue1->front;
while (Temp->next!=null)
{
printf ("%d,", temp->next->data);
temp=temp->next;
}
printf ("\ n");
}
void DEQUEUE (Linkqueue *linkqueue)
{
printf ("The first node to delete a queue%d\n", linkqueue->front->next->data);
Qnode *temp=linkqueue->front;
linkqueue->front=temp->next;
Free (temp);
}
INT Main (void)
{
Linkqueue *linkqueue1;
linkqueue1= (Linkqueue *) malloc (sizeof (linkqueue));
Initialize (LinkQueue1);
ENQUEUE (LinkQueue1);
ENQUEUE (LinkQueue1);
ENQUEUE (LinkQueue1);
Show_queue (LinkQueue1);
DEQUEUE (LinkQueue1);
Show_queue (LinkQueue1);
DEQUEUE (LinkQueue1);
Show_queue (LinkQueue1);
DEQUEUE (LinkQueue1);
return 0;
}
Results show:
2. Implementation of the chain stack
Using a single linked list with head nodes, the insertion and deletion complexity is O (1)
#include <stdio.h>
#include <stdlib.h>
typedef long ELEMTYPE;
typedef struct STACK
{
Elemtype data;
struct stack* next;
}stack;
void Initialize (stack*);
void Add (stack*);
void delete_s (stack*);
void Get_top (stack*);
void Initialize (stack* top)
{
Top->next = null;//The top here is the equivalent of a head node pointer. This is the head node!!!!
}
void Add (stack* top)
{
Elemtype m;
Puts ("Would you like to insert the number is? \ n");
scanf ("%d", &m);
stack* temp;
temp = (stack*) malloc (sizeof (STACK));
Temp->data = m;
Temp->next = top->next;
Top->next = temp;
}
void delete_s (stack* top)
{
Elemtype value;
stack* temp;
temp = top->next;
Value = top->next->data;
Top->next = temp->next;
Free (temp);
printf ("Deleted%d\n", value);
}
void Get_top (stack* top)
{
printf ("Stack top is%d", top->next->data);
}
int main (void)
{
STACK top;
Initialize (&top);
int choice;
while (1)//Note that the GetChar () and scanf combination methods in C primer plus eighth cannot be applied because the switch is used below.
{
printf ("Select the action you want to take by following the options below \ n");
printf ("1. Add number \ n");
printf ("2. Delete number \ n");
printf ("3. Show stack top \ n");
printf ("4. Exit \ n");
scanf_s ("%d", &choice);
Switch (choice)
{
Case 1:
Add (&top); Break
Case 2:
delete_s (&top); Break
Case 3:
Get_top (&top); Break
Case 4:
return 0;
Default
Break
}
}
printf ("Thank you for using!\n");
}
Implementation of "algorithm design-chain stack and chain queue" chain stack and chain queue