数据结构实验之链表一:顺序建立链表
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void creat(struct node *head, int n){
struct node *tail = head, *p;
int x;
for(int i=0;i<n;i++){
p = (struct node *)malloc(sizeof(struct node));
scanf("%d", &p->data);
p->next = NULL;
tail->next = p;
tail = p;
}
}
void show(struct node* head){
struct node *p = head->next;
while(p){
printf("%d ",p->data);
p = p->next;
}
}
int main()
{
int n, x;
struct node *head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
scanf("%d", &n);
creat(head, n);
show(head);
return 0;
}
数据结构实验之链表二:逆序建立链表
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void creat(struct node *head, int n){
struct node *p;
int x;
for(int i=0;i<n;i++){
p = (struct node *)malloc(sizeof(struct node));
scanf("%d", &p->data);
p->next = head->next;
head->next = p;
}
}
void show(struct node* head){
struct node *p = head->next;
while(p){
printf("%d ",p->data);
p = p->next;
}
}
int main()
{
int n, x;
struct node *head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
scanf("%d", &n);
creat(head, n);
show(head);
return 0;
}
数据结构实验之链表七:单链表中重复元素的删除
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void creat(struct node *head, int n){
struct node *p;
int x;
for(int i=0;i<n;i++){
p = (struct node *)malloc(sizeof(struct node));
scanf("%d", &p->data);
p->next = head->next;
head->next = p;
}
}
void show(struct node* head){
struct node *p = head->next;
while(p->next){
printf("%d ",p->data);
p = p->next;
}
printf("%d\n",p->data);
}
void del(struct node *head, int n){
struct node *p, *t, *q;
p=head->next;
while(p!=NULL)//外循环以p开始
{
t=p;
q=t->next;// t指针总为q指针的前一位,方便释放内存及连接q的前后
while(q!=NULL)
{
if(p->data==q->data)
{
n--;
t->next=q->next;
free(q);
q=t->next;//继续后移
}
else
{
t=t->next;
q=q->next;//无相等的,同时后移
}
}
p=p->next; //下次循环
}
printf("%d\n", n);
show(head);
}
int main()
{
int n, x;
struct node *head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
scanf("%d", &n);
creat(head, n);
printf("%d\n",n);
show(head);
del(head, n);
return 0;
}
数据结构实验之链表九:双向链表
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next, *front;
};
void creat(struct node *head, int n){
struct node *p, *tail = head;
int x;
for(int i=0;i<n;i++){
p = (struct node *)malloc(sizeof(struct node));
scanf("%d", &p->data);
p->next=tail->next;
tail->next=p;
p->front=tail;
tail=p;
}
}
void show(struct node* head){
struct node *p = head->next;
while(p->next){
printf("%d ",p->data);
p = p->next;
}
printf("%d\n",p->data);
}
int main()
{
int n, x, m;
struct node *head = (struct node*)malloc(sizeof(struct node)), *p;
head->next = NULL;
head->front = NULL;
scanf("%d%d", &n, &m);
creat(head, n);
while(m--)
{
int key;
scanf("%d",&key);
p=head->next;
while(p)
{
if(p->data==key)
{
if(p->front!=head&&p->next)
{
printf("%d %d\n",p->front->data,p->next->data);
}
else if(p->next==NULL&&p)
{
printf("%d\n",p->front->data);
}
else if(p->front==head&&p)
{
printf("%d\n",p->next->data);
}
}
p=p->next;
}
}
return 0;
}
数据结构实验之链表三:链表的逆置
//将头结点与之后链表断开,然后逆序建表