C语言实现链表、栈、队列的基本操作

一、链表的基本操作

#include 
#include 
typedef struct LNode{
	int data;
	struct LNode *next;
}LNode,*LinkList;
//头插法 
LinkList create_list_head(int n){ 
     LinkList head = (LinkList)malloc(sizeof(LinkList)); //分配带有表头结点的单链线性表 
     head->next = NULL;   //建立一个带头结点的单链表 
     int i;
     for(i=n;i>0;i--){
     	LinkList p = (LinkList)malloc(sizeof(LinkList));  //生成新的结点
		scanf("%d",&(p->data));  //将输入的数据存入数据域中 
		p->next = head->next;    
		head->next = p;             //插入到表头中 
     }
  return head;
}
//尾插法 
LinkList create_list_tail(int n){
	LinkList head = (LinkList)malloc(sizeof(LinkList)); //分配带有表头结点的单链线性表 
     head->next = NULL;   //建立一个带头结点的单链表 
     int i;
     LinkList tail;       
     for(i=n;i>0;i--){
   	  LinkList p = (LinkList)malloc(sizeof(LinkList));  //生成新的结点
		scanf("%d",&(p->data));  //将输入的数据存入数据域中 
        if(head->next == NULL){  //插入头结点的第一个元素 
        	head->next = p;
        	tail = p; 
        }else{                   //插入后面的元素 
        	tail -> next = p;
            tail = p;
        } 
     }
     tail -> next = NULL;
  return head;
}

//元素节点总长度,个数不包括头结点  
int total_length(LinkList head){
	LinkList p = head->next;
	int count = 0;
	while(p){
		count++;
		p = p->next;
	}
	return count;
}
//获取第i个元素 ,个数包括头结点 
int get_element(LinkList head,int i,int *element){
	LinkList p = head -> next;
	int j = 0;
	while(p && jnext; ++j;
	}
	if(!p || j>i) return 0;
	*element = p->data;
	return 1;
}

//查找第一个值为element的元素,返回下标 
int location_element(LinkList head,int element){
	LinkList p = head -> next;
	int i = 1;
	while(p && p->data!=element){
	    p = p->next;
	    i++;
	}
	if(!p) return 0;
	return i;
}
//在带有头结点的单链表中第i个位置之前插入元素element ,个数不包括头结点 
int list_insert(LinkList head,int i,int element){
   LinkList p = head -> next;
   int j = 1;
   while(p && jnext; 
   	  j++;
   }
   if(p->next==NULL || j>i) return 0;
   LinkList newp = (LinkList)malloc(sizeof(LinkList));
   newp -> data = element;
   newp -> next = p->next;
   p->next = newp;
   return 1;
}

//删除第i个元素,并由element返回删除的值 ,个数不包括头结点 
int list_delete(LinkList head,int i,int *element){
	LinkList p = head -> next;
	int j = 1;
	while(p && jnext;
		j++;
	}
	if(p->next==NULL || j>i) return 0;
	LinkList q = p -> next;
	*element = q -> data; 
	p->next =  q -> next;
	free(q);
	return 1;
}

//已知第一个和第二个单链线性表的元素是按照值进行非递减排列,归并后新的单链表也按照值进行非递减排列 
LinkList list_merge(LinkList head1,LinkList head2){
	LinkList pa = head1 -> next; 
	LinkList pb = head2 -> next;
	LinkList pc,head3;
	head3 = pc = head1;   //用第一个链表的头结点作为lc的头结点 
	while(pa && pb){
		if(pa -> data <= pb -> data){
			pc -> next = pa;
			pc = pa;
			pa = pa -> next;
		}else{
			pc -> next = pb;
			pc = pb;
			pb = pb -> next;
		}
		pc -> next = pa?pa:pb; //插入剩余段 
		free(head2);  //释放第二个链表的头结点 
	}
	return head3;
}

void print(LinkList head){ 
    printf("数字链表内容为:\n"); 
	LinkList p = head->next; 
    while(p){
    	printf("%d ",p->data);
    	p = p->next;
    }
}
int main(){
	int n1,n2;
	LinkList head;
	scanf("%d",&n1);
	head = create_list_head(n1);
	//print(head);

	//print(head1);
	//printf("%d",total_length(head));
	//int element;
	//if(get_element(head,2,&element)){
	//    printf("%d",element);
	//}else{
	//	printf("不存在");
	//}
	int element;
	scanf("%d",&element);
	int location = location_element(head,element);
	if(location){
	   printf("位置为:%d",location);
	}
	//if(list_insert(head,3,9)){
	//		print(head);
	//}else{
    //	printf("不存在");	
	//}
	//int element1;
	//if(list_delete(head,4,&element1)){
	//	printf("删除的元素为:%d",element1);
	//	print(head);
	//}else{
	//	printf("删除失败");
	//}
	//scanf("%d",&n2);
	//LinkList head1 = create_list_tail(n2);
	//LinkList head3 = list_merge(head,head1);
	//print(head3);
	return 0;
}

二、栈的基本操作

#include 
#include 
#define STACKINCREMENT 10
typedef struct{
	int *base;
	int *top;
	int stacksize;
}SqStack;

//初始化栈 
int init_stack(SqStack &s,int n){
	s.base = (int *)malloc(sizeof(int));
	if(!s.base) return 0;
	s.top = s.base;
    s.stacksize = n;
    return 1;
}
//判断栈是否为空 
int empty_stack(SqStack s){
	if(s.top==s.base){
		return 1;
	}
	return 0;
}
//压栈 
int push_stack(SqStack &s,int e){
	 if (s.top - s.base >= s.stacksize)  {  
        s.base = (int *)realloc(s.base, (s.stacksize + STACKINCREMENT) * sizeof(int));  
        if(!s.base) return 0;
        s.top = s.base + s.stacksize;//栈底地址可能改变,重新定位栈顶元素  
        s.stacksize = s.stacksize + STACKINCREMENT;  
    }  
    *s.top = e;  
    s.top++; 
    return 1; 
}

//出栈
int pop_stack(SqStack &s,int *element) {
	if(empty_stack(s)) return 0;
     *element = *(s.top-1);
     s.top--;
    return 1;
}
//查询栈顶元素 
bool get_top(SqStack s,int *element){
   if(s.base == s.top) return false;	
   *element = *(s.top-1);
   return true;
}

//查询栈的长度
int get_length(SqStack s){
	int length = 0;
	while(s.top	!= s.base){
           length++;
           s.top--;
	}
	return length;
} 
int main(){
   SqStack s;
   int n;
   printf("输入栈大小:");
   scanf("%d",&n);
   init_stack(s,n);
   int i;
   for(i=1;i<=n;i++){
     printf("输入栈的第%d个元素\n",i);
     int e;
     scanf("%d",&e);
     push_stack(s,e); 
   }
   int element;
   if(get_top(s,&element)){
   	   printf("栈顶元素为:%d\n",element);
   }
   printf("栈的长度为:%d\n",get_length(s));
   int top_e;
   if(pop_stack(s,&top_e)){
   	   printf("pop栈顶元素:%d\n",top_e);
   }
   printf("pop元素后栈的长度为:%d\n",get_length(s));
}

三、队列的基本操作


经过本人样例测试,可能还有未完善的地方,有错误的地方希望批评指正!!


你可能感兴趣的:(算法学习)