链表操作

来源:互联网 发布:淘宝怎么看自己宝贝排名 编辑:程序博客网 时间:2024/05/21 17:12

先进后出链表的建立:

#include<stdio.h>#include<stdlib.h>struct intnode{int data;struct intnode *next;};int main(void){struct intnode *p,*head=NULL;int n;printf("Input some numbers end of 0:\n");scanf("%d",&n);while(n){p=(struct intnode *)malloc(sizeof(struct intnode));p->data=n;p->next=head;head=p;scanf("%d",&n);}while(p!=NULL){printf("%d\t",p->data);p=p->next;}printf("\n");return 0;}


先进先出链表的建立:

#include<stdio.h>#include<stdlib.h>struct intnode{int data;struct intnode *next;};int main(void){struct intnode *p,*head=NULL,*tail;int n;printf("Please input numbers end of 0:\n");scanf("%d",&n);p=(struct intnode *)malloc(sizeof(struct intnode));//建立第一个节点 p->data=n;p->next=head;head=p;//将第一个节点作为头节点 tail=p;// 构建一个尾指针 scanf("%d",&n);while(n){p=(struct intnode *)malloc(sizeof(struct intnode));p->next=NULL;p->data=n;tail->next=p;//将新增的节点作为尾节点 tail=p;scanf("%d",&n);}p=head;while(p!=NULL){printf("%d\t",p->data);p=p->next;}printf("\n");return 0;}