C语言用队列编写链表的建立,插入,删除,取数据元素,倒置
时间: 2024-05-10 07:20:48 浏览: 10
1. 队列的定义
```
typedef struct node {
int data;
struct node *next;
} Node;
typedef struct queue {
Node *front;
Node *rear;
} Queue;
```
2. 队列的初始化
```
void initQueue(Queue *q) {
q->front = q->rear = NULL;
}
```
3. 队列的插入
```
void enqueue(Queue *q, int data) {
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (q->rear) {
q->rear->next = newNode;
q->rear = newNode;
} else {
q->front = q->rear = newNode;
}
}
```
4. 队列的删除
```
int dequeue(Queue *q) {
if (q->front) {
int data = q->front->data;
Node *temp = q->front;
q->front = q->front->next;
free(temp);
if (!q->front) {
q->rear = NULL;
}
return data;
} else {
printf("Queue is empty!\n");
return -1;
}
}
```
5. 队列的取数据元素
```
int getFront(Queue *q) {
if (q->front) {
return q->front->data;
} else {
printf("Queue is empty!\n");
return -1;
}
}
```
6. 链表的建立
```
Node* createList() {
Node *head, *tail;
int data;
head = tail = NULL;
printf("Enter data(-1 to end): ");
scanf("%d", &data);
while (data != -1) {
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (!head) {
head = tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
printf("Enter data(-1 to end): ");
scanf("%d", &data);
}
return head;
}
```
7. 链表的插入
```
Node* insertNode(Node *head, int data, int position) {
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (position == 1) {
newNode->next = head;
head = newNode;
} else {
Node *temp = head;
for (int i = 1; i < position - 1 && temp; i++) {
temp = temp->next;
}
if (!temp) {
printf("Invalid position!\n");
} else {
newNode->next = temp->next;
temp->next = newNode;
}
}
return head;
}
```
8. 链表的删除
```
Node* deleteNode(Node *head, int position) {
if (position == 1) {
Node *temp = head;
head = head->next;
free(temp);
} else {
Node *temp = head;
for (int i = 1; i < position - 1 && temp; i++) {
temp = temp->next;
}
if (!temp || !temp->next) {
printf("Invalid position!\n");
} else {
Node *deleteNode = temp->next;
temp->next = deleteNode->next;
free(deleteNode);
}
}
return head;
}
```
9. 链表的倒置
```
Node* reverseList(Node *head) {
Node *prev = NULL, *curr = head, *next = NULL;
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)