链表
不带头结点单链表
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int ElemType;
struct Node
{
ElemType data;
struct Node* next;
};
typedef struct Node Node;
struct Node* head;
void Insert(int x){
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = x;
temp->next=head;
head=temp;
}
void Print(){
Node* temp = head;
printf("List:");
while (temp != NULL)
{
printf(" %d",temp->data);
temp = temp->next;
}
printf("\n");
}
int main(){
head = NULL;
printf("how many numbers?\n");
int n,i,x;
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("please enter your num\n");
scanf("%d",&x);
Insert(x);
Print();
}
}
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int ElemType;
struct Node
{
ElemType data;
struct Node* next;
};
typedef struct Node Node;
void Insert(Node** head,int x){
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = x;
temp->next=*head;
*head=temp;
}
void Print(Node* head){
printf("List:");
while (head != NULL)
{
printf(" %d",head->data);
head = head->next;
}
printf("\n");
}
int main(){
Node* head = NULL;
printf("how many numbers?\n");
int n,i,x;
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("please enter your num\n");
scanf("%d",&x);
Insert(&head,x);
Print(head);
}
}
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
struct Node
{
ElemType data;
struct Node* next;
};
typedef struct Node Node;
struct Node* head;
void Insert_Index(int data,int n){
Node* temp1 = (Node*)malloc(sizeof(Node));
temp1->data = data;
temp1->next = NULL;
if (n==1)
{
temp1->next = head;
head = temp1;
return;
}
if (n==2)
{
temp1->next = head->next;
head->next = temp1;
return;
}
Node* temp2 = head;
for (int i = 0; i < n-2; i++)
{
temp2 = temp2->next;
}
temp1->next = temp2->next;
temp2->next = temp1;
}
void Print(){
Node* temp = head;
printf("List:");
while (temp != NULL)
{
printf(" %d",temp->data);
temp = temp->next;
}
printf("\n");
}
int main(){
head = NULL;
Insert_Index(2,1);
Insert_Index(3,2);
Insert_Index(4,1);
Insert_Index(5,2);
Print();
}
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
struct Node
{
ElemType data;
struct Node *next;
};
typedef struct Node Node;
struct Node *head;
void Insert(int x)
{
Node *temp = (Node *)malloc(sizeof(Node));
temp->data = x;
temp->next = head;
head = temp;
}
void Delete(int n)
{
Node *temp1 = head;
if (n == 1)
{
head = temp1->next;
free(temp1);
return;
}
else
{
int i;
for (i = 0; i < n - 2; i++)
temp1 = temp1->next;
Node *temp2 = temp1->next;
temp1->next = temp2->next;
free(temp2);
}
}
void Print()
{
Node *temp = head;
printf("List:");
while (temp != NULL)
{
printf(" %d", temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
head = NULL;
Insert(2);
Insert(4);
Insert(6);
Insert(5);
Print();
int n;
printf("Enter a position\n");
scanf("%d", &n);
Delete(n);
Print();
}
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
struct Node
{
ElemType data;
struct Node *next;
};
typedef struct Node Node;
struct Node *head;
void Insert(int x)
{
Node *temp = (Node *)malloc(sizeof(Node));
temp->data = x;
temp->next = head;
head = temp;
}
void Reverse(){
Node *next,*prev,*current;
current = head;
prev = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
void DPrint(Node *head){
if (head == NULL)
return;
DPrint(head->next);
printf("%d ",head->data);
}
void DReverse(Node *p){
if (p->next == NULL)
{
head = p;
return;
}
DReverse(p->next);
Node *q = p->next;
q->next = p;
p->next = NULL;
}
void Print()
{
Node *temp = head;
printf("List:");
while (temp != NULL)
{
printf(" %d", temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
head = NULL;
Insert(2);
Insert(4);
Insert(6);
Insert(5);
Print();
Reverse();
Print();
}
双向链表
#include <stdio.h>
#include <stdlib.h>
#define ElemType int
typedef struct Dnode
{
ElemType data;
struct Dnode *prior, *next;
} Dnode;
struct Dnode *head;
Dnode *GetNewNode(int x)
{
Dnode *newNode = (Dnode *)malloc(sizeof(Dnode));
(*newNode).data = x;
newNode->next = NULL;
newNode->prior = NULL;
return newNode;
}
void InsertAtHead(int x)
{
Dnode *newNode = GetNewNode(x);
if (head == NULL)
{
head = newNode;
return;
}
head->prior = newNode;
newNode->next = head;
head = newNode;
}
void Print()
{
Dnode* temp = head;
printf("遍历:");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void ReversePrint()
{
Dnode* temp = head;
if (head == NULL)
{
return;
}
while (temp->next != NULL)
{
temp = temp->next;
}
printf("遍历:");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->prior;
}
printf("\n");
}
int main()
{
head = NULL;
InsertAtHead(2); Print(); ReversePrint();
InsertAtHead(4); Print(); ReversePrint();
InsertAtHead(6); Print(); ReversePrint();
}
栈
数组实现
#include<stdio.h>
#define MAX_SIZE 101
int A[MAX_SIZE];
int top = -1;
void Push(int x){
if (top == MAX_SIZE - 1)
{
printf("stack is full\n");
return;
}
top++;
A[top]=x;
}
void Pop(){
if (top == - 1)
{
printf("stack is empty\n");
return;
}
top--;
}
int Top(){
return A[top];
}
void Print(){
int i;
printf("Stack: ");
for (i = 0; i <= top; i++)
{
printf("%d ",A[i]);
}
printf("\n");
}
int main(){
Push(2); Print();
Push(5); Print();
Push(10); Print();
Pop(); Print();
Push(12); Print();
}
单链表实现
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* link;
};
typedef struct Node Node;
Node* top = NULL;
void Push(int x){
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = x;
temp->link = top;
top = temp;
}
void Pop(){
Node* temp;
if (top == NULL)
{
return;
}
temp = top;
top = top->link;
free(temp);
}
int Top(){
return top->data;
}
void Print(){
printf("Stack:\n");
Node* p = top;
while (p != NULL)
{
printf("%d ",p->data);
p = p->link;
}
printf("\n");
}
int main(){
Push(2); Print();
Push(5); Print();
Push(10); Print();
Pop(); Print();
Push(12); Print();
int A = Top();
printf("%d",A);
}
栈表逆置(C++)
#include
#include //stack标准库
#include
#include
using namespace std;
// class Stack
// {
// private:
// char A[101];
// int top;
// public:
// void Push(int x);
// void Pop();
// int Top();
// bool IsEmpty();
// };
typedef struct Node {
int data;
Node* next;
}Node;
Node* head=NULL;
void Push_Num(int x){
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = x;
temp->next = head;
head = temp;
}
void Reverse(char C[],int n){ //C++中数组C[] 也可以写为 *C
stack S;
//压栈
for (int i = 0; i < n; i++)
{
S.push(C[i]);
}
//弹栈
for (int i = 0; i < n; i++)
{
C[i] = S.top();
S.pop();
}
}
void Reverse(){
if (head == NULL)
{
return;
}
stack S;
Node* temp = head;
while (temp != NULL)
{
S.push(temp);
temp = temp->next;
}
temp = S.top();
head = temp; //头指针指向栈顶元素,即原栈的栈底元素
S.pop();
while (!S.empty())
{
temp->next=S.top(); //上一个栈顶元素 指向 弹出后的 下一个栈顶元素
S.pop();
temp=temp->next;
}
temp->next = NULL;
}
void Print(){
printf("Stack:\n");
Node* p = head;
while (p != NULL)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
int main(){
// char C[51];
// printf("Enter a String:\n");
// gets(C);
// Reverse(C,strlen(C));
// printf("OutPut = %s",C);
Push_Num(2); Print();
Push_Num(5); Print();
Push_Num(10); Print();
Push_Num(12); Print();
Reverse(); Print();
}