链表
来源:互联网 发布:https域名劫持原理 编辑:程序博客网 时间:2024/06/05 19:53
(1)//链表
structnode *p,*head;
p=NULL;
head=NULL;
head=createNode();
p=head;
while(p!=NULL)
{
printf("%d\t",p->value);
p=p->next;
}
printf("\n");
head=reverseNode(head);
p=head;
while(p!=NULL)
{
printf("%d\t",p->value);
p=p->next;
}
printf("\n");
while(head!=NULL)
{
printf("%d\t",head->value);
p=head;
head=head->next;
free(p);
p->next=NULL;
}
structnode * createNode()
{
structnode *head,*last,*temp;
int value=0;
head=last=NULL;
printf("please enter data\n");
while(scanf("%d",&value)==1)
{
temp=(structnode *)malloc(sizeof(structnode));
temp->value=value;
temp->next=NULL;
if(head==NULL)
{
head=last=temp;
}else{
last->next=temp;
last=temp;
}
}
return head;
}
structnode * reverseNode(structnode * head)
{
structnode *p,*q=NULL;
p=head;
while(p->next!=NULL)
{
q=p->next;
p->next=q->next;
q->next=head;
head=q;
}
return head;
}
structnode * reverseNode1(structnode * head)//逆序链表
{
if(head==NULL||head->next==NULL)
{
return head;
}
structnode *p,*q,*t;
p=head;
q=head->next;
head->next=NULL;
while(q)
{
t=q->next;
q->next=p;
p=q;
q=t;
}
head=p;
return head;
}
structnode * reverseNode2(structnode * head)//逆序链表
{
structnode *p,*q =NULL;
p=head->next;
while (p->next!=NULL) {
q=p->next;
p->next=q->next;
q->next=head->next;
head->next=q;
}
p->next=head;
head=p->next->next;
p->next->next=NULL;
return head;
}
(2)
/*
模拟链表
*/
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}node, *pnode;
pnode create()
{
int i,len,t;
pnode phead=(pnode)malloc(sizeof(node));
if(phead!=NULL)
{
pnode p=phead;
p->next=NULL;
printf("请输入要创建的节点个数!\n");
scanf("%d",&len);
for(i=0;i<len;++i)
{
printf("请输入第%d个节点的值:",i+1);
scanf("%d",&t);
pnode new=(pnode)malloc(sizeof(node));
if(new!=NULL)
{
new->data=t;
p->next=new;
new->next=NULL;
p=new;
}
}
}
return phead;
}
void selectnode(pnode phead)
{
pnode p=phead->next;
while(p!=NULL)
{
printf("%d\t",p->data);
p=p->next;
}
printf("\n");
}
int nodesize(pnode phead)
{
int i=0;
pnode p=phead;
while(p!=NULL)
{
p=p->next;
++i;
}
return i-1;
}
void add(pnode phead)
{
int d,a,i=0;
pnode p=phead;
pnode t;
int s=nodesize(phead);
printf("请输入要添加节点的数据!\n");
scanf("%d",&d);
printf("请输入要添加节点的位置!\n");
scanf("%d",&a);
if(a>=1&&a<=s+1)
{
while(i<a-1)
{
p=p->next;
++i;
}
pnode new=(pnode)malloc(sizeof(node));
new->data=d;
t=p->next;
p->next=new;
new->next=t;
}else
{
printf("输入的位置有误!\n");
}
}
void freenode(pnode phead)
{
pnode p=phead;
pnode pnext;
while(p->next!=NULL)
{
pnext=p->next;
free(p);
p->next=NULL;
p=pnext;
}
free(p);
p->next=NULL;
}
void delete(pnode phead)
{
pnode p=phead;
pnode t;
int dat;
int ad;
int i=0;
int s=nodesize(phead);
printf("请输入要删除的节点的位置!\n");
scanf("%d",&ad);
if(ad>=1&&ad<=s)
{
while(i<ad-1)
{
p=p->next;
++i;
}
t=p->next;
dat=t->data;
p->next=p->next->next;
free(t);
t->next=NULL;
printf("删除的节点的数据为:%d\n",dat);
}else{
printf("您输入的位置有误!\n");
}
}
int main()
{
pnode phead=create();
int c;
while(1)
{
printf("请选择以下操作!\n");
printf("1、添加节点\n");
printf("2、删除节点\n");
printf("3、查询链表\n");
printf("4、退出\n");
printf("请输入选项:\n");
scanf("%d",&c);
switch (c) {
case1:
add(phead);
break;
case2:
delete(phead);
break;
case3:
selectnode(phead);
break;
case4:
freenode(phead);
return -1;
break;
}
}
return0;
}
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- Android 5.0学习之使用Palette
- 牢骚
- CAS4添加验证码
- js正则表达式匹配空格
- Longest Substring Without Repeating Characters
- 链表
- Android BlueTooth通信
- LeetCode_144Binary Tree Preorder Traversal
- Nginx学习之十二-负载均衡-加权轮询策略剖析
- 什么是psr-0,psr-1,psr-2标准
- MongoDB与内存
- Nginx学习之十一-Nginx启动框架处理流程
- 整合:求最短路算法 floyd prim dijkstra Kruskal spfa算法简析
- JTable合并表头